Home  >  Article  >  Backend Development  >  Detailed explanation of usage and examples of built-in functions of PHP template engine Smarty

Detailed explanation of usage and examples of built-in functions of PHP template engine Smarty

墨辰丷
墨辰丷Original
2018-06-04 09:25:381355browse

This article mainly introduces the usage of the built-in functions of the PHP template engine Smarty, and analyzes the common built-in functions in smarty in the form of examples, definitions and usage methods. Friends in need can refer to

Smarty Built-in functions: Smarty comes with some built-in functions. Built-in functions are part of the template language. Users cannot create custom functions with the same name as built-in functions, nor can they modify built-in functions.

The following is a description of the built-in functions in Smarty and examples:

The Smarty template engine initialization file init.inc.php and the main file index used in the example .php

init.inc.php

<?php
  define(&#39;ROOT_PATH&#39;, dirname(__FILE__)); //设置网站根目录
  require ROOT_PATH.&#39;/libs/Smarty.class.php&#39;; //加载 Smarty 模板引擎
  $_tpl = new Smarty(); //创建一个实例对象
  $_tpl->template_dir = ROOT_PATH.&#39;/tpl/&#39;; //重新指定模板目录
  $_tpl->compile_dir = ROOT_PATH.&#39;./com/&#39;; //重新指定编译目录
  $_tpl->left_delimiter = &#39;<{&#39;; //重新指定左定界符
  $_tpl->right_delimiter = &#39;}>&#39;; //重新指定右定界符
?>

index.php

<?php
  require &#39;init.inc.php&#39;; //引入模板初始化文件
  global $_tpl;
  $_tpl->display(&#39;index.tpl&#39;); //引入模板
?>

1, capture


#AttributeTypeWhether it is requiredDefault valueDescription##nameassign/tpl/index.tpl
string no defaultData collection area name
string No n/aWhere is the data collection area allocated to the variable name[to be tested]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Capture</title>
</head>
<body>
   <!-- 定义capture -->
   <{capture name="foo"}>
     这里是 capture 函数里面的内容,默认是不显示的。
   <{/capture}>
   <!-- 调用capture,使用的是 Smarty 中的保留变量{$smarty.capture} -->
   <{$smarty.capture.foo}>
</body>
</html>

2、config_load


Attributefilesectionscopeglobal

config_load 函数用于从配置文件中加载变量,关于 config_load 函数的使用,可参考前面一篇《PHP模板引擎Smarty之配置文件在模板变量中的使用方法示例》。

3、include


Type Is it required Default value Description
string Yes n/aThe name of the configuration file to be included
string No n/aThe name of the part to be loaded in the configuration file
string no local#The scope of loading data, the value must be local, parent or global. local Description of this variable The scope of is the current template. parent indicates that the scope of the variable is the current template and the parent template of the current template (the template that calls the current template). global indicates that the scope of the variable is all templates.
boolean No NoIndicates whether the loaded variable is global Visible, equivalent to scope=parent. Note: When the scope attribute is specified, this attribute can be set, but the template ignores the attribute value and takes the scope attribute as the criterion.
属性 类型 是否必须 缺省值 描述
file string Yes n/a 待包含的模板文件名
assign string No n/a 该属性指定一个变量保存待包含模板的输出
[var ...] [var type] No n/a 传递给待包含模板的本地参数,只在待包含模板中有效

include 函数用于在当前模板中包含其它模板, 当前模板中的变量在被包含的模板中可用. 必须指定 file 属性,该属性指明模板资源的位置。如果设置了 assign 属性,该属性对应的变量名用于保存待包含模板的输出,这样待包含模板的输出就不会直接显示了。请看下面的示例:

/tpl/index.tpl

{include file="header.tpl"}
{* body of template goes here *}
{include file="footer.tpl"}

4、if,elseif,else

Smarty 中的 if 语句和 php 中的 if 语句一样灵活易用,并增加了几个特性以适宜模板引擎. if 必须于 /if 成对出现. 可以使用 else 和 elseif 子句。

可以使用以下条件修饰词:eq、ne、neq、gt、lt、lte、le、gte、ge、is even、is odd、is not even、is not odd、not、mod、p by、even by、odd by、==、!=、>、d2714fbb0e49a95306c2048bc19e4f2b=. 使用这些修饰词时必须和变量或常量用空格格开。

下面对这些修饰符表示的意思进行说明:


条件修饰符 作用描述
eq ==
ne !=
neq !=
gt >
lt ff7ec474ecc2ec105b900d573d35826d=
ge >=
is even 是否偶数
is odd 是否奇数
is not even 是否不是偶数
is not odd    是否不是奇数
not !=
mod 求模
p by 是否能被整除
even by 商是否是偶数
odd by 商是否是奇数
&&
||
() 括号改变优先级

5、ldelim 和 rdelim

用于输出分隔符,也就是大括号 "{" 和 "}". 模板引擎总是尝试解释大括号内的内容,因此如果需要输出大括号,请使用此方法。请看下面的示例:

/tpl/index.tpl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>ldelim 和 rdelim</title>
</head>
<body>
  <{ldelim}>funcname<{rdelim}> 是 Smarty 中的一个函数。
  <!-- 执行结果: <{funcname}> 是 Smarty 中的一个函数。 -->
</body>
</html>

6、literal

literal 标签区域内的数据将被当作文本处理,此时模板将忽略其内部的所有字符信息. 该特性用于显示有可能包含大括号等字符信息的 javascript 脚本. 当这些信息处于 {literal}{/literal} 标签中时,模板引擎将不分析它们,而直接显示,其实按照我的所有例子中的标签风格(因为在 init.inc.php 初始化文件中已经重新设置了左定界符和右定界符),而不是 Smarty 的默认风格,基本上不会产生这种情况。关于该函数的使用,请看下面的示例

/tpl/index.tpl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>literal</title>
</head>
<body>
  <{literal}>
  <script language=javascript>
     <!--
       window.alert(new Date());
     -->
  </script>
  <{/literal}>
</body>
</html>

7、php

php 标签允许在模板中直接嵌入 php 脚本,此标签会把标签内部的内容当成 PHP 脚本进行解析执行。请看下面的示例

/tpl/index.tpl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>php</title>
</head>
<body>
  <{php}>
    echo date("Y-m-d H:i:s");
  <{/php}>
  <!-- 执行结果: 2011-10-24 04:35:03 -->
</body>
</html>

8、strip

Web 开发者多次遇到空格和回车影响HTML输出的情形,为了得到特定的结果,因此你不得不在模板里运行所有的标签. 通常在难以理解或难以处理的模板中遇到此问题。Smarty 在显示前将除区任何位于 {strip}{/strip} 标记中数据的首尾空格和回车. 这样可以保证模板容易理解且不用担心多余的空格导致问题。

好了, Smarty 模板引擎中的内建函数先总结这么多,关于内建函数中两个最重要的函数(foreach,foreachelse、section,sectionelse)的使用,可参考前面一篇《PHP模板引擎Smarty内建函数foreach,foreachelse用法分析》

总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php用户登录之cookie信息安全的用法及实例详解

PHP 返回13位时间戳的实现方法

php调用自己java程序的方法及实例详解

The above is the detailed content of Detailed explanation of usage and examples of built-in functions of PHP template engine Smarty. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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