利用PHP引擎smarty(3)
利用PHP模板引擎smarty
(资料图片)
#$sections参数可以不写,默认值为null,$smarty->configLoad("test.conf")则只使用全局的键值,而不能使用标签下的键值
#在模版下通过{config_load file="test.conf" section="china" scope="global"}语句调用配置文件
#section属性可以不写,默认是null,scope属性必须写{config_load file="test.conf" scope="global"}
#section属性可赋三种值
#local 只有当前模版可以使用该配置文件
#parent 只有当前模版引入该配置文件语句后包含的模版中,或在php源文件中smarty对象调用该配置文件后调用的模版中可以使用该配置文件中的键值
#global 测试效果和parent相同
#在模版中通过{#language#}来使用键值,也可以通过{$smarty.config.language}来访问配置文件键值
#PHP源文件中可以使用$smarty->getConfigVars("language")或$smarty->getConfigVariable("language")来获取键值,$smarty->getConfigVars("language")获取的还可能是数组
*/
/*
tpl文件中常用函数
tpl文件:
{capture name="testCapture"}
{include file="f1.tpl"}
{/capture}
{if true}
{$smarty.capture.testCapture}
{/if}
{if $name == "wang"}
Welcome wang.
{elseif $name == "zhang"}
Welcome zhang.
{else}
Welcome, whatever you are.
{/if}
{*操作符可以是 ==,>= 等也可以是 eq,ne等*}
{for $x=0; $x {$x} {/for} {*for循环,类似PHP代码*} {$x=0} {while $x {$x++} {/while} {*While循环,也类似PHP代码。*} {foreach name="testForeach" from=$testArr key=arId item=arVal} {$arId}对应的值为:{$arVal} {$smarty.foreach.testForeach.index} ? {$smarty.foreach.testForeach.iteration} ? {$smarty.foreach.testForeach.first} {$smarty.foreach.testForeach.last} {$smarty.foreach.testForeach.total} {foreachelse} $testArr is null {/foreach} {*也可以如下两种类PHP格式*} {foreach $testArr as $n} {$n} {/foreach} {foreach $testArr as $key=>$n} {$key} {/foreach} {$sectionArr = [0=>"a",4=>"b","c","d","e",6,7,8,9,10,11,12,13,14,15,16]} {section name="testSection" loop=$sectionArr start=0 step=4 max=6 show=true} {$smarty.section.testSection.index}- {$sectionArr[testSection]}- {$smarty.section.testSection.iteration}- {sectionelse} $sectionArr is null {/section} */ /* tpl模板文件: {literal} function a(){ alert("this is script"); } a(); /script
词条内容仅供参考,如果您需要解决具体问题
(尤其在法律、医学等领域),建议您咨询相关领域专业人士。