• 学习Smarty3要点记录+顶级页面模板代码:


    !!! 顶级页面模板代码

    http://gbtan.iteye.com/blog/681262

    1. 在模板tpl中定义{2+3*4},会计算成14;
    2. {$x + $y}必须紧挨着大括号,+之间有无空格关系不大;
    3. 模板tpl中可定义{$str = strlen($hello)},输出{$a};{$a = strlen("$hello")}可以加引号,还可以加{}
    4. 模板tpl中定义多维数组{assign var=foo value=array(1, array(3,4), 7)},也可以{assign var=foo value=array[1, [3,4], 7]},还可以{assign var=foo value=[0=>1, 1=>[3,4], 2=>7]},调用方式{$foo[1][1]},或者{$foo.1.1},输出4;
    5. Smarty3模板tpl中支持动态的变量名{assign var=foo_3 value=[0=>1, 1=>[3,4], 2=>7, 8, 12, 79]},执行{$foo_{$x}[1][1]},输出4;
    6. 支持对象链:

    {$object->method1($x)->method2($y)}

    在Php中定义如下:

    class Test {

    function f1()

        {

            echo "调用方法1.<br/>";

                  return $this;

        }

        function f2()

        {

            echo "调用方法2.<br/>";

    }

    }

    $t = new Test;

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

    模板tpl中可以这样引用:

    {$t->f1()->f2()}

    1. for语句:

    {for $i=0; $i<$x; $i++}

    hello, {$i}<br/>

    {/for}

    也可以

    {for $i = 0 to 3 step 1}

    hello, {$i}<br/>

    {/for}

    {for $i = 0 to 3}

    hello, {$i@iteration}, {$i@total}, {$i@first}, {$i@last}<br/>

    {/for}

    {foreach $myarray as $var}...{/foreach}

    {while $foo}...{/while}

    1. 函数递归:

    {function name=menu level=0}

      <ul class="level{$level}">

      {foreach $data as $entry}

        {if is_array($entry)}

          <li>{$entry@key}</li>

           {menu data=$entry level=$level+1}

        {else}

          <li>{$entry}</li>

        {/if}

      {/foreach}

      </ul>

    {/function}

    {$menu = ['item1','item2','item3' => ['item3-1','item3-2','item3-3' =>

    ['item3-3-1','item3-3-2']],'item4']}

    {menu data=$menu}

      输出:

        * item1

        * item2

        * item3

              o item3-1

              o item3-2

              o item3-3

                    + item3-3-1

                    + item3-3-2

    * item4

    1. 代码块不缓存:

    {nocache}...{/nocache}之间的代码永远不会缓存

    1. 模板tpl中:

    {$smarty.current_dir}输出模板当前目录

    11.字符串直接用于smarty中:

     $smarty->display('string:This is my template, {$foo}!'); // php

    {include file="string:This is my template, {$foo}!"} // template

    均是直接显示出string来。

    1. 限定变量存储:

    $tpl = $smarty->createTemplate('index.tpl',$smarty);

    或$tpl = $smarty->createTemplate('index.tpl')

    $tpl->assign('hello',"hello world!");

    //引用模板文件

    $tpl->display('index.tpl');

    1. 可以将在一个tpl中定义的变量的范围放大,以在其他的tpl文件中使用:

    {assign var=foo value='bar'}           // no scope is specified, the default 'local'

    {$foo='bar'}                        // same, local scope

    {assign var=foo value='bar' scope='local'} // same, local scope

    {assign var=foo value='bar' scope='parent'}

    // Values will be available to the parent object

    {$foo='bar' scope='parent'}       // (normally the calling template)

    {assign var=foo value='bar' scope='root'} 

     // Values will be exported up to the root object, so they can

    {$foo='bar' scope='root'}   // be seen from all templates using the same root.

    {assign var=foo value='bar' scope='global'}

    // Values will be exported to global variable storage,

    {$foo='bar' scope='global'}    // they are available to any and all templates.

    1. 模板继承:

    parent.tpl:

    <html>

      <head>

        <title>{block name='title'}My site name{/block}</title>

      </head>

      <body>

        <h1>{block name='page-title'}Default page title{/block}</h1>

        <div id="content">

          {block name='content'}

            Default content

          {/block}

        </div>

      </body>

    </html>

    child.tpl:

    {extends file='parent.tpl'}

    {block name='title'}

    Child title

    {/block}

    grandchild.tpl:

    {extends file='child.tpl'}

    {block name='title'}Home - {$smarty.block.parent}{/block}

    {block name='page-title'}My home{/block}

    {block name='content'}

      {foreach $images as $img}

        <img src="{$img.url}" alt="{$img.description}" />

      {/foreach}

    {/block}

    We redefined all the blocks here, however in the title block we used {$smarty.block.parent},which tells Smarty to insert the default content from the parent template in its place.The content block was overriden to display the image files, and page-title has also be overriden to display a completely different title.

    If we render grandchild.tpl we will get this:

    <html>

      <head>

        <title>Home - Child title</title>

      </head>

      <body>

        <h1>My home</h1>

        <div id="content">

          <img src="/example.jpg" alt="image" />

          <img src="/example2.jpg" alt="image" />

          <img src="/example3.jpg" alt="image" />

        </div>

      </body>

    </html>

  • 相关阅读:
    伪句柄
    WM_NCACTIVE
    怎么让模态对话框最小化时,主窗口也最小化
    CMap和CArray中ARG_
    看着嫦娥2号上去,心里激动不已
    Qt中使两个部件同步,这里为spin和slider
    memcpy 和strcpy的区别
    如何在linux内核中读写文件
    Could not find *****/adb.exe!的解决方法(android sdk太新了?**#¥)
    加入了HTML特点的Qt,出来的效果不错哟
  • 原文地址:https://www.cnblogs.com/carl2380/p/2512055.html
Copyright © 2020-2023  润新知