Home php教程 php手册 PHP 中使用 Smarty 之六:Smarty 内建函数

PHP 中使用 Smarty 之六:Smarty 内建函数

Jun 13, 2016 am 10:45 AM
php smarty use function yes template user of Bring your own language

 

 

 

 

 

 

    Smarty 的内建函数:Smarty自带一些内建函数,内建函数是模板语言的一部分,用户不能创建名称和内建函数一样的自定义函数,也不能修改内建函数。

        下面对Smarty 中的内建函数进行说明,并加以实例:

         实例中使用到的Smarty 模板引擎初始化文件init.inc.php 和主文件index.php

init.inc.php

 

 

    define('ROOT_PATH', dirname(__FILE__));     //设置网站根目录 

    require ROOT_PATH.'/libs/Smarty.class.php'; //加载Smarty 模板引擎 

    $_tpl = new Smarty();                       //创建一个实例对象 

    $_tpl->template_dir = ROOT_PATH.'/tpl/';    //重新指定模板目录 

    $_tpl->compile_dir = ROOT_PATH.'./com/';    //重新指定编译目录 

    $_tpl->left_delimiter = '

    $_tpl->right_delimiter = '}>';              //重新指定右定界符 

?> 

 

index.php

 

 

 

    require 'init.inc.php';        //引入模板初始化文件 

    global $_tpl; 

    $_tpl->display('index.tpl');   //引入模板 

?>   

 

 


 

        1、capture

属性 类型 是否必须 缺省值 描述
name string no default 数据采集区域名称
assign string No n/a 数据采集区域在哪分配给变量name[待考]

        capture函数的作用是捕获模板输出的数据并将其存储到一个变量里,而不是把它们输出到页面,任何在 {capture name="foo"}和{/capture}之间的数据将被存储到变量$foo中,该变量由name属性指定,在模板中通过 $smarty.capture.foo 访问该变量。如果没有指定 name 属性,函数默认将使用 "default" 作为参数,{capture}必须成对出现,即以{/capture}作为结尾,该函数不能嵌套使用。请看下面的实例模板文件。

/tpl/index.tpl

 

 

 

 

 

Capture 

 

 

 

      

      

         这里是capture 函数里面的内容,默认是不显示的。 

      

      

      

      

 

   

    2、config_load

 

属性 类型 是否必须 缺省值 描述
file string Yes n/a 待包含的配置文件的名称
section string No n/a 配置文件中待加载部分的名称
scope string no local 加载数据的作用域,取值必须为local, parent 或 global. local 说明该变量的作用域为当前模板. parent 说明该变量的作用域为当前模板和当前模板的父模板(调用当前模板的模板). global 说明该变量的作用域为所有模板.
global boolean No No 说明加载的变量是否全局可见,等同于 scope=parent. 注意: 当指定了 scope 属性时,可以设置该属性,但模板忽略该属性值而以 scope 属性为准。

        config_load 函数用于从配置文件中加载变量,关于 config_load 函数的使用,请看我的《PHP 中使用 Smarty 之二:配置文件在模板变量中的使用。

        3、include

 

属性 类型 是否必须 缺省值 描述
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、div 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 求模
div by 是否能被整除
even by 商是否是偶数
odd by 商是否是奇数
&&
||
() 括号改变优先级

    


 

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

/tpl/index.tpl

 

 

 

 

 

 

 

ldelim 和rdelim 

 

 

 

     

    funcname 是Smarty 中的一个函数。 

 

     

     

 

 

 

        6、literal

 

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

/tpl/index.tpl

 

 

 

 

 

literal 

 

 

 

     

     

   

 

          

 

     

     

     

 

 

 

       7、php

 

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

/tpl/index.tpl

 

 

 

 

 

php 

 

 

 

     

     

        echo date("Y-m-d H:i:s"); 

     

     

     

 


        8、strip

 

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

 

        好了, Smarty 模板引擎中的内建函数先总结这么多,关于内建函数中两个最重要的函数(foreach,foreachelse、section,sectionelse)的使用,将在下面的进行整理。

 

 

摘自:Lee.的专栏

Statement of this Website
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

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 ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles