美文网首页
smarty模板引擎的学习

smarty模板引擎的学习

作者: 咸鱼前端 | 来源:发表于2019-07-29 14:33 被阅读0次

基本配置

$smarty = new Smarty();
//左定界符
$smarty->left_delimiter = "{";
//右定界符
$smarty->right_delimiter = "}";
//是否使用缓存,开发阶段不建议使用缓存,所以~false
// $smarty->caching = false;
//模板目录,就是保存模板的位置,写对了smarty才能找到 html的模板位置
$smarty->template_dir = './tpl';
//编译目录,保存编译后的tpl的地方 模板编译生成的文件
$smarty->compile_dir = './tpl_c';
//外部配置文件目录,这个配置文件我个人理解是放一些Smarty相关的常量的,之后会说。
$smarty->config_dir = './configs';
//开启缓存
$smarty->caching = true;
//缓存目录
$smarty->cache_dir = "./cache";
//缓存时间
$smarty->cache_lifetime = 120;

常用的两个方法

  • assign
// 第一个参数是变量名,第二个参数是参数的值
$smarty->assign("time", time());
  • display
// 调用哪个模板文件显示
$smarty->display("../tpl/test.tpl");

基本语法

  • 输出基本变量
{$articleTitle}
  • 输出一维数组
{$arr['title'] } or { $arr.title}
  • 输出二维数组
// array("articleContent"=>array("title"=>"学习","auhor"=>"lzq"));
{$arr['articleContent'].title}

变量调节器

  • 1.首字母大写 capitalize

实例:{$articleTitle|capitalize}

  • 2.字符串连接 cat

实例:{$articleTitle|cat : " 链接的字符串 " : "多个参数用冒号链接"}

  • 3.日期格式化 date_format

实例:{$time|date_format}
{$time|date_format : " %A, %B, %e, %Y %H:%M:%S "}

  • 4.为未赋值或为空的变量指定默认值default

{$articleTiele|default : "no title"}

  • 5.转码 escape

用于html转码,url转码,在没有转码的变量上转换单引号,十六进制转吗,十六进制美化,或者javascript转码。默认是html转码

  • 6.小写lower 大写 upper (将变量字符串变小(大)写)

实例:{articleTitle|lower} {articleTitle|upper}

  • 7.所有的换行符将被换成
    nl2br 功能同php的nl2br()的函数一样

实例:{$articleTitle|nl2br}

条件判断

条件修饰符 eq(==) neq(!=) gt(>) lt(<)
修饰符必须和常量或者变量用空格隔开

  • 基本句式
{if $name eq "Mr"}
Welcome Sir.
{elseif $name eq "Mrs"}
Welcome Madam.
{else}
Welcome {$name}
{/if}

循环语句

{* section循环函数 *}
{* name是作为一个循环的id值 *}
{section name=article loop=$articlelist}
{$articlelist[article].title}
<br>
{/section}
{$hr}
{* foreach循环函数 *}
{foreach $articlelist as $article}
    {if $article@index == 1}
        {break}
    {/if}
    {$article.title}
    <br>
{foreachelse}
当前没有文章
{/foreach}

文件引用

{* 文件引用,include中的属性会传递给被引入的文件,在引入文件中是不能使用的 *}
{include file="./header.tpl" title="标题" sitename="网站名称"}

对象赋值与使用

{* 对象赋值到模板中 *}
{$obj->method(array("苹果","熟了"))}

smarty中使用php内置函数

{* 在smarty使用php内置函数 *}
{"Y-m-d"|date:$time}
{$br}
{"strong"|str_replace:"handsome boy":$str}

注册自定义函数

// 先在php文件中注册函数
// f-test是注册变量名  test是需要注册的函数名
$smarty->registerPlugin("function","f_test","test");
// 再在smarty模板文件中使用上自定义函数
// 第一个是注册的函数名字 后面跟着的两个参数是代表函数的属性和值,在编译的时候会打包成一个数组,赋值给$params;
{f_test p1="香蕉" p2="熟了"}

自定义插件

  • 1.function
// 先按照规定写好函数体
function smarty_function_test($params)
{
    $width = $params['width'];
    $height = $params['height'];
    $area = $width * $height;
    return $area;
}
// 通过模板文件调用smarty函数
{test width=2200 height=400}
  • 2.modifier (类似于变量调节器的写法)
// 先按照规定写好变量调节器插件
function smarty_modifier_test($utime, $format)
{
    return date($format, $utime);
}
// 通过模板文件调用smarty变量调节器插件
{$time|test:"Y-m-d H:i:s"}
  • 3.block(类似于foreach的用法,用两个闭合标签组合)
// 先按照规定写好区块函数插件的方法
function smarty_block_blockTest($params, $content)
{
    $replace = $params['replace'];
    $maxnum = $params['maxnum'];
    if ($replace == 'true') {
        $content = str_replace(",", ",", $content);
        $content = str_replace("。", ".", $content);
    }
    $content = substr($content, 0, $maxnum);
    return $content;
}
// 通过模板文件调用smarty区块函数插件
{blockTest replace="true" maxnum=200}
{$str}
{/blockTest}

相关文章

  • 2018-05-11Smarty模板引擎

    Smarty(模板引擎) 一、什么是模板引擎? Smarty是一个php模板引擎。更准确的说,它分开了逻辑程序和外...

  • Smarty模板

    PHP模板引擎 该文档使用的是Smarty 3.1.33 Released版本 什么是PHP模板引擎? php模板...

  • Smarty 模板函数

    最近工作中用到 Smarty 模板引擎,整理了一些用到的模板函数。 假设 smarty 的定界符为 {}。 模板中...

  • smarty模板引擎的学习

    基本配置 常用的两个方法 assign display 基本语法 输出基本变量 输出一维数组 输出二维数组 变量调...

  • smarty模板引擎

    一、模板引擎的工作原理 1、实现HTML代码和PHP代码简单分离,完全去除视图文件中的PHP标记 2、常用PHP模...

  • Smarty模板引擎

    模板引擎的作用是什么 对PHP语言熟悉的程序员就会知道有个Smarty的名词,那么这个具体是什么呢?smarty是...

  • smarty模板引擎

    1)、模板引擎概念 ❖ 模板引擎的功能是实现逻辑与显示相分离,使程序设计者可以专注于程序功能的开发,使网页设计师专...

  • Smarty模板引擎

    Smarty

  • Smarty--(2)创建配置文件

    完成Smarty配置工作是应用Smarty模板引擎的关键。config.php 下面,我们来一一解读1.defin...

  • 微擎模板引擎

    $this->template('xxx')说明 类似smarty模板引擎 1、如果写在模块的前台方法functi...

网友评论

      本文标题:smarty模板引擎的学习

      本文链接:https://www.haomeiwen.com/subject/jgjjlctx.html