美文网首页
Smarty模板

Smarty模板

作者: duans_ | 来源:发表于2018-10-20 18:35 被阅读33次

PHP模板引擎

该文档使用的是Smarty 3.1.33 Released版本

什么是PHP模板引擎?

  • php模板引擎是将php代码和前端代码剥离的工具,
  • 一般是时使用php语言进行编写
  • 模板引擎文件中大部分代码是前端的代码,只有一部分涉及到php的代码会用一些特殊的标签替代
  • 学习模板引擎其实就是在学习这些特殊的标记标签

什么是Smarty?

  • smarty是一个使用php编写的php模板引擎,是众多php的模板和引擎中使用最为广泛的一种.

Smarty的配置

smart配置左右定界符

  • smarty默认的定界符为{}
  • 可以通过以下代码配置自定义定界符为{{}}
$smarty = new Smarty();
$smarty->left_delimiter = '{{';
$smarty->right_delimiter      = '}}';

smarty自定义模板文件目录

  • 默认情况下,模板文件路径为./templates,模板编译文件路径为./templates_c,缓存文件目录为./cache
  • 可以通过以下代码自定义模板路径 , 编译文件路径, 缓存路径
//设置模板文件目录
$smarty->setTemplateDir($dir) 
//$smarty->getTemplateDir();$template_dir;
// 设置编译目录
$smarty->setCompileDir('./templates_c');
//$smarty->getCompleDir();$compile_dir;
// 设置缓存目录
$smarty->setCacheDir('./cache');
//$smarty->getCacheDir();$cache_dir;

// 方法连用
$smarty->setTemplateDir('./templates')
       ->setCompileDir('./templates_c')
       ->setCacheDir('./cache');

Smarty的使用

内置函数(模板页面常用标签)

{foreach}

  • 作用: 循环遍历数组
  • 使用示例
  {$data = [1,2,3,4,5]}
  {foreach $data as $value}
    {$key}-{$value}
  {/foreach}

{php}

  • 作用: 用于书写原生php代码
  • 示例代码
{php}
$arr=array('name'=>'zs','age'=>19);
foreach($arr as $key=>$val){
    echo $key.'='.$val;
}
{/php}

{include}

  • 作用: 在当前模板文件中包含另外一个模板文件
  • 示例代码:
    1. 包含文件
    // 引入page_header.tpl文件
    {include file='page_header.tpl'}
    
    1. 传递变量
    • a.tpl
    {include 'b.tpl' title='我是a模板传递过来的变量'}
    
    • b.tpl
    <h1>{$title}</h1>
    

{if} {elseif} {else}

  • 作用: 逻辑判断
  • 示例代码
{if isset($name) && $name == 'Blog'}
     {* do something *}
{elseif $name == $foo}
    {* do something *}
{/if}

{if is_array($foo) && count($foo) > 0}
    {* do a foreach loop *}
{/if}

{literal}

  • 作用: 防止代码被解析(写在这个标签内部的代码不会被smarty解析,一般用于防止js代码被smarty解析)
  • 示例代码
<script>
   {literal}
     function myBar {alert('Bar!');}
   {/literal}
</script>

{**}

  • 作用: 书写注释
  • 示例代码
{*这是一段smarty注释*}

{assign}

  • 作用: 用于在模板运行时,赋值变量
  • 示例代码
{assign var="name" value="Bob"}
//{$name}会输出'Bob'
{$name}

变量修饰符

  • 变量修饰符就是在模板页面的变量后面,写一个|,在|的后面写一个函数名,该函数可以使系统函数,也可以是自定义函数
  • 使用:将函数的参数和函数名隔开,如果有多个参数,也是使用:分割

常用系统变量修饰符

default
  • 作用:给变量设置默认值
  • 示例代码
{$name|default='暂无姓名'}
truncate
  • 作用: 截取字符串
  • 示例代码
{assign var='title' value='这是一个很长的Title,长到会撑破你的网页结构,你怎么办?'}
//这是一个很长的T
{$title|truncate:8:}
//这是一个很长的标T...
{$title|truncate:8:"..."}
//第三个参数表示按照单词截取,按照指定长度截取的同时,会保证单词的完整性不被破坏
//这是一个很长的标Title...
{$title|truncate:8:"...":true}
strip_tags
  • 作用: 删除(过滤)字符串中标签
  • 示例代码
{assign var='content' value='<h1>我是h1标题</h1>'}
{$articleTitle|strip_tags}
nl2br
  • 作用: 将字符串中的\n替换成<br/>
  • 示例代码
//php页面
<?php
$smarty->assign('title',
                "Sun or rain expected\ntoday, dark tonight"
                );
?>
//模板页面
{$title|nl2br}
lower
  • 作用: 将字母全部转成小写
  • 示例代码
{assign var='title' value="THIS IS A TITLE"}
{$title|lower}
upper
  • 作用: 将字母全部转成大写
  • 示例代码
{assign var='title' value="this is a title"}
{$title|upper}

自定义变量修饰符

  • 模板中使用系统或者自定义函数作为变量修饰符
  • 除了以上系统提供的变量修饰符之外,php系统函数或者一个自定义函数也可以作为变量修饰符;默认变量作为函数的第一个参数,如过函数有多个参数,使用:分割
{assign var='title' value='This is as title'}
//全部转成大写
{$title|strtoupper}
//全部转成小写
{$title|strtolower}
//计算字符串长度
{$title|strlen}

自定义函数(了解)

html_options

  • 准备数据
<?php
$smarty->assign('myOptions', array(
                                1800 => 'Joe Schmoe',
                                9904 => 'Jack Smith',
                                2003 => 'Charlie Brown')
                                );
$smarty->assign('mySelect', 9904);
?>
  • 模板调用
/*
    name:为select标签最终的name
    options:下拉框的选项
    selected:默认选中项
*/ 
{html_options name=foo options=$myOptions selected=$mySelect}
  • 输出
<select name="foo">
<option value="1800">Joe Schmoe</option>
<option value="9904" selected="selected">Jack Smith</option>
<option value="2003">Charlie Brown</option>
</select>

{html_table}

  • 数据
<?php
$smarty->assign( 'data', array(1,2,3,4,5,6,7,8,9) );
$smarty->assign( 'tr', array('bgcolor="#eeeeee"','bgcolor="#dddddd"') );
$smarty->display('index.tpl');
?>
  • 模板
{html_table loop=$data cols="first,second,third,fourth" tr_attr=$tr}
  • 输出
<table border="1">
<thead>
<tr>
<th>first</th><th>second</th><th>third</th><th>fourth</th>
</tr>
</thead>
<tbody>
<tr bgcolor="#eeeeee"><td>1</td><td>2</td><td>3</td><td>4</td></tr>
<tr bgcolor="#dddddd"><td>5</td><td>6</td><td>7</td><td>8</td></tr>
<tr bgcolor="#eeeeee"><td>9</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td></tr>
</tbody>
</table>

fetch

{* assign the fetched contents to a template variable *}
{fetch file='http://www.baidu.com/' assign='weather'}
<div id="weather">{$weather}</div>

缓存(了解)

开启缓存

<?php
require('Smarty.class.php');
$smarty = new Smarty;

// 使用$smarty->cacheLifetime()可以更精确定义缓存时间

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

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

设置缓存时间

<?php
require('Smarty.class.php');
$smarty = new Smarty;

// 让每个缓存的过期时间都可以在display执行前单独设置。
$smarty->setCaching(Smarty::CACHING_LIFETIME_SAVED);

// 设置index.tpl的过期时间为5分钟
$smarty->setCacheLifetime(300);
$smarty->display('index.tpl');

// 设置home.tpl的过期时间为1小时
$smarty->setCacheLifetime(3600);
$smarty->display('home.tpl');

// 注意:当$caching设置了Smarty::CACHING_LIFETIME_SAVED后,
// 下面的$cache_lifetime将不会起效。
// home.tpl已经设置了过期时间为1小时,
// 所以不会再遵循下面的$cache_lifetime值,
// home.tpl的过期时间还是1小时。
$smarty->setCacheLifetime(30); // 30 秒
$smarty->display('home.tpl');
?>

删除缓存

<?php
require('Smarty.class.php');
$smarty = new Smarty;

$smarty->setCaching(Smarty::CACHING_LIFETIME_CURRENT);

if(!$smarty->isCached('index.tpl')) {
    // 找不到缓存,这里进行一些赋值操作
    $contents = get_database_contents();
    $smarty->assign($contents);
}

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

附录

Smarty官网地址
Smarty官方文档

相关文章

  • 关于fis框架中fis3-smarty语法总结(一)

    目录 什么是smarty fis3-smarty模板语法 基础模板框架语法html、head、style、widg...

  • Smarty 模板函数

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

  • 2018-05-11Smarty模板引擎

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

  • Smarty模板

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

  • Smarty--(3)实战前的演习

    (1) 创建Smarty模板文件在\Smarty\templates文件夹下建立一个index.Html,获取Sm...

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

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

  • Smarty模板学习总结

    smarty语法 主要分为,变量、函数的使用。不建议在smarty模板中使用较为复杂的逻辑。如有需要请封装好插件或...

  • smarty模板引擎

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

  • Smarty模板引擎

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

  • smarty模板引擎

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

网友评论

      本文标题:Smarty模板

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