search
HomeBackend DevelopmentPHP TutorialDetailed explanation of usage and examples of built-in functions of PHP template engine Smarty

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、==、!=、>、=. 使用这些修饰词时必须和变量或常量用空格格开。

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


条件修饰符 作用描述
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 商是否是奇数
&&
||
() 括号改变优先级

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

php怎么设置implode没有分隔符php怎么设置implode没有分隔符Apr 18, 2022 pm 05:39 PM

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment