×

phpsmarty 程序 入门

phpsmarty(php简单smarty入门程序实例)

admin admin 发表于2022-09-06 00:57:35 浏览160 评论0

抢沙发发表评论

本文目录

php简单smarty入门程序实例


本文实例讲述了php简单smarty入门程序。分享给大家供大家参考。具体如下:
首先要有3个文件夹configs、templates、templates_c,在configs文件夹中有一个配置文件:test.conf,代码:
title
=
Welcome
to
Smarty!
cutoff_size
=
40
[setup]
bold
=
true
templates中有模板文件:test.htm:
《html》
《head》
《title》Smarty
Test《/title》
《/head》
《body》
《H1》Hello,
{$Name}《/H1》
《/body》
《/html》
php文件代码:
《?php
require
’libs/Smarty.class.php’;
//包含Smarty类库文件
$smarty
=
new
Smarty;
//创建一个新的Smarty对象
$smarty-》assign(“Name“,“Simon“);
//对模版中的变量赋值
$smarty-》display(’test.htm’);
//显示页面
?》
运行后显示的页面代码:
《html》
《head》
《title》Smarty
Test《/title》
《/head》
《body》
《H1》Hello,
Simon《/H1》
《/body》
《/html》
运行之后,还在templates_c文件夹中生成一个php文件:
《?php
/*
Smarty
version
2.6.22,
created
on
2009-03-19
13:20:00
compiled
from
test.htm
*/
?》
《html》
《head》
《title》Smarty
Test《/title》
《/head》
《body》
《H1》Hello,
《?php
echo
$this-》_tpl_vars[’Name’];
?》
《/H1》
《/body》
《/html》
这个文件就是浏览所显示出来的效果。
希望本文所述对大家的php程序设计有所帮助。

php中smarty 模板结构


smarty模板的控制结构 if语句控制块常见的if语句写法:》》 if语句在smarty中的应用
# {if $name == “Fred“ || $name == “Wilma“}
{* 和上面的例子一样,“or“和“||“没有区别 *}
# ...
{* 如果条件成立则输出这个区块的代码 *}
# {/if}
{* 是条件控制的关闭标记,if必须成对出现* foreach的遍历: 主要是应用在一维数组中. {foreach}要与{/foreach}成对使用,它有四个参数,其中form和item两个是必要的。foreach可以使用的全部参数如表16-4所示。 表16-4 foreach可以使用的选项参数参 数 名描 述类 型默 认 值form待循环数组的名称,该属性决定循环的次数,必要参数数组变量无item确定当前元素的变量名称,必要参数字符串无key当前处理元素的键名,可选参数字符串无name该循环的名称,用于访问该循环,这个名是任意的,可选参数字符串无 foreach来遍历一维数组 foreach来遍历二维数组 也可以在模板中嵌套使用foreach遍历二维数组,但必须保证嵌套中的foreach名称唯一。此外,在使用foreach遍历数组时与下标无关,所以在模板中关联数组和索引数组都可以使用foreach遍历。 二维数组的遍历 1. 《?php
2. require “libs/Smarty.class.php“;
//包含Smarty类库
3. $smarty = new Smarty();
//创建Smarty类的对象
4. $contact=array(
//声明一个保存三个联系人信息的二维数组
5. array(’name’=》’高某’,’fax’=》’1234’,’email’=
》’gao@lampbrother.net’,’phone’=》’4321’),
6. array(’name’=》’洛某’,’fax’=》’4567’,’email’=
》’luo@lampbrother.net’,’phone’=》’7654’),
7. array(’name’=》’峰某’,’fax’=》’8910’,’email’=
》’feng@lampbrother.net’,’phone’=》’0198’)
8. );
9. $smarty-》assign(’contact’, $contact);
//将关联数组$contact分配到模板中使用
10. $smarty-》display(’index.tpl’);
//查找模板替换并输出
11. ?》
在进行输出时:
进行遍历的方案 # {foreach from=$contact item=row}
{* 外层foreach遍历数组$contact *}
# 《tr》
{* 输出表格的行开始标记 *}
# {foreach from=$row item=col}
{* 内层foreach遍历数组$row *}
# 《td》{$col}《/td》
{* 以表格形式输出数组中的每个数据 *}
# {/foreach}
{* 内层foreach区块结束标记 *}
# 《/tr》
{* 输出表格的行结束标记 *}
# {/foreach}
{* 外层foreach区域的结束标记 *}
说明: 这里的遍历是对整个二维数组来进行遍历. foreachelse在进行遍历数组时的应用:foreach标记提供了一个扩展标记foreachelse,这个语句在from变量没有值的时候被执行,就是在数组为空时foreachelse标记可以生成某个候选结果。在模板中foreachelse标记不能独自使用,一定要与foreach一起使用。而且foreachelse不需要结束标记,它嵌入在foreach中,与elseif嵌入在if语句中很类似。 foreach为二维数组 1. {foreach key=key item=value from=$array}
{* 使用foreach遍历数组$array中的键和值 *}
2. {$key} =》 {$item} 《br》
{* 在模板中输出数组$array中元素的键和值对 *}
3. {foreachelse}
{* foreachelse在数组$array没有值的时候被执行*}
4. 《p》数组$array中没有任何值《/p》
{* 如果看到这条语句,说明数组中没有任何数据*}
5. {/foreach}
{* foreach需要成对出现,是foreach的结束标记 *} section的循环遍历section来循环遍历二维数组二维数组的遍历 说明:这是一个二维数组的定义
$contact=array( //声明一个保存三个联系人信息的二维数组
array(’name’=》’高某’,’fax’=》’1234’,’email’=》’gao@lampbrother.net’,’phone’=》’4321’),
array(’name’=》’洛某’,’fax’=》’4567’,’email’=》’luo@lampbrother.net’,’phone’=》’7654’),
array(’name’=》’峰某’,’fax’=》’8910’,’email’=》’feng@lampbrother.net’,’phone’=》’0198’)
);
$smarty-》assign(’contact’, $contact); //将关联数组$contact分配到模板中使用
说明:使用section来进行遍历,其中对于是关联数组的数组访问,使用“.“号形式来访问
{section name=line loop=$contact} {* 使用section遍历数组$contact *}
《tr》 {* 输出表格的行开始标记 *}
《td》{$contact[line].name}《/td》 {* 输出数组第二维中下标为name的元素值 *}
《td》{$contact[line].fax}《/td》 {* 输出数组第二维中下标为fax的元素值*}
《td》{$contact[line].email}《/td》 {* 输出数组第二维中下标为email的元素值*}
《td》{$contact[line].phone}《/td》 {* 输出数组第二维中下标为phone的元素值*}
《/tr》 {* 输出表格的行结束标记 *}
{/section} {* section区域的结束标记 *}

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; } }
-入门

最基本的php的smarty的配置问题,快疯了


这里是

$smarty-》left_delimiter =“{%“;

$smarty-》right_delimiter =“%}“;

边界符带了个%,所以后面也必须带%

《html》
《body》
《b》{%$title%}《/b》
《/body》
《/html》

不过smarty边界符一般这样设置:

$smarty-》left_delimiter =“《!--{“;
$smarty-》right_delimiter =“}--》“;

php与smarty有什么关系


smarty是一个使用PHP写出来的模板引擎,是目前业界最著名的PHP模板引擎之一。模板引擎就是将显示层分离出来。我们之前做的网站,如果在需一套更漂亮的界面的时候,我们会发现简直是致命的。其工作量可能相当于重写整个系统了。而模板引擎的出现帮我们解决了这一难题。使我们的网站拥有更好的可维护性。简单的讲,目的就是要使PHP程序员同美工分离,使程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。
-程序

PHP模板中smarty_block函数的用法


Smarty末班引擎中提供了三种插件支持,分别是block(块),function(函数),modifier(调节器),用户可以自己扩展。

  1. block:是一种非常灵活的高级插件,这种插件在模板中使用时需要成对出现,Smarty内置的block插件例如section,foreach等,使用格式为:

    {section name=“customer“ loop=“$data“}

    《li》内容《/li》

    {/section}

  2. function:他的作用类似于函数,在模板中使用无需成对出现,系统内置的如include,格式为:{include file=“web/index.tpl“}。

  3. modifier:调节器是用于对变量进行修饰的,内置的调节器如:truncate(截取字符长度),date_format(格式化时间),使用格式为:

    {$nowtime|date_format:“%Y-%m-%d“}

在来分析下你的问题:

-----------------------------------------------------------------------

你提问中的这个就应当属于block插件,其中blockname是个插件名,此插件不包含任何参数。

{blockname}《!--插件开始标签--》
没有缓存的:{$smarty.now}《!--插件输入的内容,Smarty.now为全局函数,意思是输出当前时间--》
{/blockname}《!--插件结束标签--》
-入门

smarty模板引擎有什么用,php中怎么用


smarty是一个使用PHP写出来的模板PHP模板引擎.它提供了逻辑与外在html内容的分离.
作用:就是要使用PHP程序员同美工分离,使用的程序员改变程序的逻辑内容不会影响到美工的页面设计,美工重新修改页面不会影响到程序的程序逻辑,这在多人合作的项目中显的尤为重要。
具体使用方法是,先将smarty核心文件引入,然后做配置,然后赋值变量到模板,最后到模板进行解析就可以了。
参考教程:http://leadtodream.blog.163.com/blog/static/18520043920151711534369/
-程序

PHP 中 smarty 要怎么 配置


首先要加载smarty类:Smarty.class.php
然后实例化这类:如$smarty=new Smarty()
主要的几个基本配置:
$smarty-》template_dir //设置模板目录
$smarty -》 compile_dir //设置编译目录
$smarty -》 config_dir //设置配置文件目录
$smarty-》 caching //设置缓存状态
$smarty-》 cache_dir //设置缓存的路径
$smarty -》 debugging //是否允许调试
$smarty -》 left_delimiter //设置左定界符
$smarty-》 right_delimiter //设置右定界符
$smarty -》 cache_lifetime //设置缓存的时间
使用一般就assign(); display();
-入门

php中smarty怎么赋值变量到模板


$smarty-》assign(’title’,“这是值“);
$smarty-》display(“moban.tpl“);
上边就是对title赋值,然后使用到模版moban.tpl上,你就可以在moban.tpl使用title这个变量。
-程序

2018年,PHP的smarty还有必要学吗


没必要,现在用smarty的真心不多,很落伍,速度慢
smarty是用来做前后段代码分离的
其实我个人觉得很鸡肋,对于现在前段来说,后端部分代码也是能看懂的
稍微有点经验的都不在乎这些 效率还低,没啥突出的
我们公司就不用这个,之前接触的公司用这个的真心不多
-入门