Home > Backend Development > PHP Tutorial > smarty_PHP tutorial

smarty_PHP tutorial

WBOY
Release: 2016-07-13 10:29:32
Original
1110 people have browsed it

模板引擎是用于把模板文件和数据内容合并在一起的程序,便于网站开发有利于代码分离和维护,了解一个模板最好知道其工作原理,以便于实现一通万通。

模板文件一般是HTML xml js等类型文件,如果不用模板引擎若要把数据显示在网页上,我们需要在php中输出HTML,而使用模板则只要把数据交给模板引擎程序即可,然后告诉它用哪个模板文件,自然就会把数据和页面结合以后返回或输出,模板至少有以下功能1.把数据提供给模板引擎的功能。2.指定模板的功能。3.输出结果的功能。一般来说为了方便程序员们使用模板引擎,开发者都会把它的功能在一定程度上封装起来,封装成一个类,实例化之后得到一个对象,即模板引擎对象,一个对象有其属性和方法,smarty对象的属性和方法在smarty手册上查找,首先说说其方法,assign 把数据提交给模板的方法。没有单独的指定模板文件的方法已经合并到输出的方法中,输出的方法有两个 display 直接输出  fetch 返回合并好的HTML代码,对于输出我们主要用到的是assign 因为我们显示的数据往往是多样性的,可能是一个量,可能是一个数组量也可能是多维数组,在不同的情况下应该如何正确提交给smarty是一个问题,提交后如何对应显示也是个问题,smarty引擎使用的解释方法是先把HTML文件转化为php文件,然后在赋值各个量,并且执行这个php文件,对应不同的数据格式,它有一套固定的书写方式,需要我们用这种书写方式在模板文件上做对应的标记,smarty默认使用的模板标记符是一对{},比如{$a}这个标记等效于echo $a;php中我们需要有对应的赋值过程,$smarty->assign("a","");如果我们有多个量进行赋值,一个个这样写就很麻烦,smarty为我们考虑了这一点,例如我们从数据库里读出一个文章,要显示在页面的内容有标题内容作者时间,数据结构大体是这样的

array([id]=>1,['title']=>"标题",…);

我们的模板需要有几个对应的标记,例如

{$title}

{$content}

Assigning values ​​one by one is too troublesome. The assign method supports direct assignment of arrays. $rows = is read from the database. Evidence,

$smarty->assign($rows);smarty will take the data index and automatically assign values ​​one by one. However, in order to avoid variable conflicts at this time, we hope to directly use the array form What about assignment, such as

$rows = Data read from the database,

$smarty->assign("rows",$rows);

If our tag in the template at this time is {$rows} , then when outputting we can only see array Just like php directly echo array is output in php The specific amount is echo $rows['title']; smarty The specified symbol is a period, {$rows.title},In this way it is similar to

echo $rows['title']

Each template has its corresponding writing rules. If you want to display a list of articles next, assume that mysql returns us 10 pieces of data, 10 pieces of data will all be displayed, and their indexes must be exactly the same. According to the programming idea, we know the result calculation process, assuming that it is displayed As follows

<span><</span><span>ul</span><span>></span>

<span><</span><span>li</span><span>></span>1111<span></</span><span>li</span><span>></span>

<span><</span><span>li</span><span>></span>222<span></</span><span>li</span><span>></span>

<span><</span><span>li</span><span>></span>333<span></</span><span>li</span><span>></span>

<span><</span><span>li</span><span>></span>4444<span></</span><span>li</span><span>></span>

<span></</span><span>ul</span><span>></span>
Copy after login

If this is what we want the output to look like

First of all, these are multiple quantities, of course they use arrays,

<span>$list</span>=<span>array</span><span>();

</span><span>While</span>(<span>$rows</span>=<span>数据){

</span><span>$list</span>[]=<span>$rows</span><span>;

}

</span><span>$smarty</span>->assign("list",<span>$list</span>);
Copy after login

First put the data into an array and then give it to smarty all at once, so that it is inside the list variable It is a two-dimensional array. If we get such a two-dimensional array and display all the values ​​in it, the best way is to loop the output. The same smarty provides us with Markers for loops, section and foreach

sectionTag format

{section name=The name of this loop loop=Data volume name}

{/section}

{section name=i loop=$list}

  • {$list[i].title}
  • {/section}

    The above code looks a lot like the for loop, but here the i is not The $i in the for loop is just the name of this loop, $list[loop name]This way of writing can get one quantity from the array at a time. As mentioned just now, $list is a two-dimensional array, $list[i]What you get is still an array.

    Another way to write it is foreach The syntax is as follows:

    {foreach key=index item=value from=assignment variable}

    {$key}:{$item}

    {/foreach}

    {foreach item=v from=$list}

  • {$v.title}
  • {/foreach}

    循环list变量每个量赋值到v里,然后从变量v里面指定要显示的索引,除了循环标记外,它还给我们提供了一些常用的语法标记,例如包含文件,条件判断,我们知道HTML不能包含文件,比如网页头部,但smarty提供了{include}的标记,可以像php一样包含文件,例如{include file="文件路径"} 这个标记格式是固定的,而且这个路径必须在模板引擎指定的路径下,而条件判断的语法和php一样是if条件判断,语法如下

    {if变量==值或量}

    为真时显示的值

    {else}

    为假是显示的值

    {/if}

    也可以不写else只有为真时显示的内容,例如常见的一种情况是网页上有一个登陆口登录前显示的是表单登录后显示的是用户信息,假设一个量已经赋值给模板了比如 $username 用户登录这个量就有用户名没有登录这个量就是空的,我们可以这样写

    {if $username !=""}

    欢迎{$username}

    {else}

    请先登录

    {/if}

    我们只要在php把这个变量准备好,并赋值给smarty即可除了这些标记外还有的标记自行参考手册,

    其次就是变量调节器了,很多时候我们从数据库中得到的数据,都要小小的处理下才输出的,比如日期格式,只显示年月日即可再如输出的内容里的换行要换成
    才能在页面显示对应的样子,这个时候我们可以使用smarty自带的变量调节器,格式如下

    {要输出的变量|调节器名:参数}

    假如内容部分在显示的时候把所有的换行显示为
    只需要如下写法

    {$content|nl2br}

    日期的格式化可以用date_format 例如手册上

    index.php:
    
    <span>$smarty</span> = <span>new</span><span> Smarty;
    </span><span>$smarty</span>->assign('yesterday', <span>strtotime</span>('-1 day'<span>));
    </span><span>$smarty</span>->display('index.tpl'<span>);
    
    index</span>.tpl:<span>
    
    {</span><span>$smarty</span>.now|<span>date_format}
    {</span><span>$smarty</span>.now|date_format:"%A, %B %e, %Y"<span>}
    {</span><span>$smarty</span>.now|date_format:"%H:%M:%S"<span>}
    {</span><span>$yesterday</span>|<span>date_format}
    {</span><span>$yesterday</span>|date_format:"%A, %B %e, %Y"<span>}
    {</span><span>$yesterday</span>|date_format:"%H:%M:%S"<span>}
    
    OUTPUT</span>:<span>
    
    Feb </span>6, 2001<span>
    Tuesday</span>, February 6, 2001
    14:33:00<span>
    Feb </span>5, 2001<span>
    Monday</span>, February 5, 2001
    14:33:00 
    Copy after login

    实在不行我们可以用php处理好之后再进行赋值。

    下面写上配置

    <?<span>php
    </span><span>define</span>("ROOT",<span>str_replace</span>('\\','/',<span>dirname</span>(<span>__FILE__</span>)).'/');<span>//</span><span>定义根路径
    //加载smarty类</span>
    <span>require</span> ROOT.'lib/smarty.class.php'<span>;
    </span><span>$samrty</span> = <span>new</span> smarty();<span>//</span><span>实例化一个smarty类
    //配置各种目录</span>
    <span>$smarty</span> ->setTemplateDir(ROOT.'templates/'<span>)
            </span>->setCompileDir(ROOT.'templates_c'<span>)
            </span>->setPluginsDir(ROOT.'plugins/'<span>)
            </span>->setCacheDir(ROOT.'cache/'<span>)
            </span>->setConfigDir(ROOT.'configs/'<span>);
    </span><span>$smarty</span>->caching = <span>false</span>;<span>//</span><span>是否开启缓存</span>
    <span>$smarty</span>->left_delimiter = '<{';//<span>设置左右 防止和js css 等发生冲突
    </span><span>$smarty</span>->right_delimiter = '}>'<span>;
    </span>?>
    Copy after login

     

     

    www.bkjia.comtruehttp://www.bkjia.com/PHPjc/775947.htmlTechArticle模板引擎是用于把模板文件和数据内容合并在一起的程序,便于网站开发有利于代码分离和维护,了解一个模板最好知道其工作原理,以便...
    Related labels:
    source:php.cn
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template