Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to get started with smarty

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to get started with smarty, the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have a certain understanding of the relevant knowledge after reading this article.

Getting started with smarty

Smarty

Definition: an open source template engine

The template engine is created to separate the user interface from the business data. It can generate documents in a specific format, and the template engine for websites will generate a standard HTML document.

Function

Separate the site's data from the site's interface implementation (php and html code)

Cache page

download

Www.smarty.net

Use

1. Introduction of smarty class library

two。 Instantiate smarty object

3. Initialization parameter

Template_dir template storage directory

Compile_dir compilation directory

4. Allocation variable

5. Parsing template

Comment {* this is the content of the comment *}

Ignore smarty parsing {literal} {/ literal}

Example:

/ / the first step is to move to the smarty class

Require'. / libs/Smarty.class.php'

/ / step 2 instantiate the object

$s = new Smarty

/ / step 3 initialization

$s-> template_dir ='. / View'

$s-> compile_dir ='. / View_c'

$pdo = new PDO ('mysql:host=localhost;dbname=pass;charset=utf8','root','')

$stmt = $pdo- > query ('select * from news')

$res = $stmt- > fetchAll (PDO::FETCH_ASSOC)

/ / var_dump ($res)

/ / step 4: assign variables

$s-> assign ('title',' News Management system?')

/ / $s-> assign ('name',' what?')

$s-> assign ('res',$res)

/ / step 5 parse the template

$s-> display ('add.html')

/ / create view files to store and change web pages, view_c conversion files

Add.html

{extends file='index.html'}

{block name='title'}

News add page

{/ block}

{block name='content'}

Release the news

Title:

Keywords:

Author:

Content:

{/ block}

Index.html

{block name='title'}

News management system

{/ block}

{literal}

Table,td {font-family: Microsoft Acer; text-align:center;}

H4 {font-family: Microsoft Yahi;}

{/ literal}

{include file='menu.html'}

{block name='content'}

Browse the news

News ID News headlines News keywords author News content Operation

{foreach $res as $v}

{/ foreach}

{/ block}

Menu.html

Body {font-family: Microsoft Acer;}

News management system

Browse the news |

Release the news

# # smarty

/ / the first step is to move to the smarty class

Require'. / libs/Smarty.class.php'

/ / part II instantiates the object

$s = new Smarty

/ / initialization of part III

/ / template directory initialization, template storage directory

$s-> template_dir ='. / View'

/ / compile directory

$s-> compile_dir ='. / View_c'

$pdo = new PDO ('mysql:host=localhost;dbname = pass;charset = utf8','root','')

$stmt = $pdo- > query ('select * from stu')

$res = $stmt- > fetchAll (PDO::FETCH_ASSOC)

/ / step 4: assign variables

$s-> assign ('a template of title','smart')

$s-> assing ('name','mingzi')

$s-> assing ('res','$res')

/ / step 5 analyze the template

$s-> display ('2.html')

2.html change

{$title}

{$name}

{foreach $res as $v}

{$v ['id']}

{$v ['name']}

{/ foreach}

{literal}

{/ literal}

{include file='menu.html'} introduction

{block name = 'content'} s

Mysqli

/ / Import file /

Define ('HOST','localhost')

Define ('USER','root')

Define ('PWD','')

Define ('DBNAME','pass')

Define ('UTF','utf8')

$link = @ mysqli_connect (HOST,USER,PWD) or die ('connection failed')

Mysqli_select_db ($link,DBNAME); / / Select a database

Mysqli_set_charset ($link,UTF); / / character set

$sql = 'select * from news order by id')

$result = mysqli_query ($link,$sql)

/ / query result auxiliary function

Mysqli_num_rows ($result) > 0

Mysqli_fetch_assoc ($result) gets the associative array

Mysqli_close ($link) / / shut down the database

This is the end of the quick start on how to carry out smarty. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report