smarty模板,smarty模板引擎分页怎么写

http://www.itjxue.com  2023-01-23 03:54  来源:未知  点击次数: 

在smarty模板中创建多级目录怎么写代码

Smarty以其诸多的优点成为模板的首选,那么下面是我的一点亲自体会,供更多phper分享

注意:这里Smarty要求web服务器运行php4.0.6和以上版本. Smarty要求4个目录,默认下命名为:tempalates, templates_c, configs and cache。每个都是可以自定义的,可以修改Smarty类属性: $template_dir, $compile_dir, $config_dir, and $cache_dir respectively

Smarty的 $compile_dir 和$cache_dir必须可写

1.首先我们需要从Smarty库,大家可以Google一下从Smarty官网下载,一般不超过1M,这里以Smarty-2.6.24为例

2.下载完车后解压Smarty压缩文件,然后只取出libs文件夹就可以了,当然demo文件夹是一些例子,对大家学习Smarty很有帮助,这里我们暂且将libs文件夹放入网站根目录下,如htdocs/下,然后将其改名为Smarty(这个可以改成自己风格的名称)

3.按照以下代码配置文件

通常将这个文件作为被包含的文件这里我们将这个文件名定为Smarty.inc.php,我们只要在使用时包含这个文件即可?php//首先包含Smarty类文件

include_once('Smarty/Smarty.class.php');

//实例化Smarty类文件

$smarty=new Smarty();

//设置配置目录,可以不设置

//注意一下文件夹需要自己创建,并且可以改名

//$smarty-config_dir=

//$smarty-cache_dir="./caches";//设置缓存目录

//$smarty-caching=true;//关闭缓存,调试中建议关闭 默认为关闭即设置成false

$smarty-cache_lifetime=60;//单位为秒 设置缓存时间

$smarty-template_dir="./templates";//设置模版目录

$smarty-compile_dir="./templates_c";//设置编译目录必选

$smarty-cache_dir="./smarty_cache";//缓存文件夹可选为减轻压力

//设置开始结束边界默认为{} 但容易与javascript冲突

$smarty-left_delimiter="{";

4.演示一下Smarty模板的使用

新建一个php文件 文件名为helloworld.php 代码如下?php//包含smarty配置文件

include 'smarty.inc.php';

//将变量name赋值为helloworld

$smarty-assign('name','Hello world!');

//在模板下的helloworld.html文件显示注意这里必须对应的是模板目录下的helloworld.html换成别的文件名不行,必须和php的文件对应

$smarty-display('helloworld.html');?设置helloworld.html文件html{$name}!--输出到浏览器页面--/html注意:两个文件名必须相同除扩展名!还要将smarty.inc.php 和helloworld.php放于同一目录下

5.下来就可以参考Smarty手册和demo尝试了,一起加油phper!

PHP中的smarty模板的smarty_block如何使用

你可以研究研究 Block Functions块函数 void smarty_block_ name (array $params, mixed $content, object $smarty) Block functions are functions of the form: {func} .. {/func}. In other words, they enclose a template block and operate on the contents of this block. Block functions take precedence over custom functions of the same name, that is, you cannot have both custom function {func} and block function {func} .. {/func}.

块函数的形式是这样的:{func} .. {/func}。换句话说,他们用标记圈起一个块,然后对这个块的内容进行操作。块函数优先于同名的传统函数,即你不能同时有通明的传统函数{func}和块函数{func} .. {/func}。 By default your function implementation is called twice by Smarty: once for the opening tag, and once for the closing tag (see $repeat below how to change this).

默认地你的函数执行被Smarty调用两次:一次是在开始标记,另一次是在结束标记(参考下面的$repeat怎样改变这种情况) Only the opening tag of the block function may have attributes. All attributes passed to template functions from the template are contained in the $params as an associative array. You can either access those values directly, e.g. $params['start'] or use extract($params) to import them into the symbol table. The opening tag attributes are also accessible to your function when processing the closing tag.

块函数仅开始标记可以有属性。所有从模板传替给模板函数的属性被囊括与一个集合数组参数中。你可以直接获取其值,例如:$params['start']或者是用extract($params)将它们导入符号表中。当处理结束标记时,开始标记的属性对你的函数也是可用的。 The value of $content variable depends on whether your function is called for the opening or closing tag. In case of the opening tag, it will be null, and in case of the closing tag it will be the contents of the template block. Note that the template block will have already been processed by Smarty, so all you will receive is the template output, not the template source.

变量 $content 的值取决于是否因开始标记或结束标记调用你的函数。假如是开始标记,它会是空的,如果是结束标记,它会是模板块的内容。请注意模板块已经被Smarty处理,所以你接收到的结果是输出后的模板而不是原样模板。 The parameter $repeat is passed by reference to the function implementation and provides a possibility for it to control how many times the block is displayed. By default $repeat is true at the first call of the block-function (the block opening tag) and false on all subsequent calls to the block function (the block's closing tag). Each time the function implementation returns with $repeat being true, the contents between {func} .. {/func} are evaluated and the function implementation is called again with the new block contents in the parameter $content .

参数 $repeat 通过参考引用传递给函数执行过程并为其提供一个可能值来控制显示块多少遍。默认情况下在首次调用块函数(块开始标记)时变量 $repeat 是真,在随后的所有块函数调用中其始终是假。每当函数执行返回的 $repeat 是真时,在{func} .. {/func}之间的内容再次求值,函数执行接收一个新块参数 $content 内容值被再次调用。 If you have nested block functions, it's possible to find out what the parent block function is by accessing $smarty-_tag_stack variable. Just do a var_dump() on it and the structure should be apparent.

如果你嵌套了块函数,通过访问变量$smarty-_tag_stack 找出父块函数是可能的。仅仅对块函数运行一下var_dump(),函数结构就会一目了然了。 See also: register_block(), unregister_block(). Example 16-5. block function ?php /* * Smarty plugin * ------------------------------------------------------------- * File: block.translate.php * Type: block * Name: translate * Purpose: translate a block of text * ------------------------------------------------------------- */ function smarty_block_translate($params, $content, $smarty) { if (isset($content)) { $lang = $params['lang']; // do some intelligent translation thing here with $content return $translation; } }

smart 模板是什么意思,怎么用?

一、什么是smarty?smarty是一个使用PHP写出来的模板PHP模板引擎,它提供了逻辑与外在内容的分离,简单的讲,目的就是要使用PHP程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。二、smarty优点:1. 速度:采用smarty编写的程序可以获得最大速度的提高,这一点是相对于其它的模板引擎技术而言的。2. 编译型:采用smarty编写的程序在运行时要编译成一个非模板技术的PHP文件,这个文件采用了PHP与HTML混合的方式,在下一次访问模板时将WEB请求直接转换到这个文件中,而不再进行模板重新编译(在源程序没有改动的情况下)3. 缓存技术:smarty选用的一种缓存技术,它可以将用户最终看到的HTML文件缓存成一个静态的HTML页,当设定smarty的cache属性为true时,在smarty设定的cachetime期内将用户的WEB请求直接转换到这个静态的HTML文件中来,这相当于调用一个静态的HTML文件。4. 插件技术:smarty可以自定义插件。插件实际就是一些自定义的函数。5. 模板中可以使用if/elseif/else/endif。在模板文件使用判断语句可以非常方便的对模板进行格式重排。三、不适合使用smarty的地方:1. 需要实时更新的内容。例如像股票显示,它需要经常对数据进行更新,这类型的程序使用smarty会使模板处理速度变慢。2. 小项目。小项目因为项目简单而美工与程序员兼于一人的项目,使用smarty会丧失php开发迅速的优点。四、安装smarty类:安装smarty的环境:php版本4.06以上版本。安装smarty方法非常简单,从 中下载smarty.t...将LIB中所有文件拷入comm目录,完成基本安装.

(责任编辑:IT教学网)

更多

推荐Discuz!建站文章