<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>最优良人 &#187; smarty</title>
	<atom:link href="http://www.zui88.com/view-tag/smarty/feed" rel="self" type="application/rss+xml" />
	<link>http://www.zui88.com/blog</link>
	<description>中山php&#124;最优网络</description>
	<lastBuildDate>Mon, 13 May 2013 04:56:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.4</generator>
		<item>
		<title>smarty模版使用php标签，如何获取模版变量</title>
		<link>http://www.zui88.com/blog/view-409.html</link>
		<comments>http://www.zui88.com/blog/view-409.html#comments</comments>
		<pubDate>Sat, 22 Sep 2012 03:54:23 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=409</guid>
		<description><![CDATA[已经assign一个模版变量$assign,由于要做特殊的循环输出，使用for循环，因此使用到了php标签，但是php语句和模版语句的变量作用域是不同的，因此不能直接获取到 {{php}} for($i=0;$i&#60;count($assign);$i=$i+2){ echo ' &#60;ul&#62; &#60;li&#62; &#60;span class="zz_pic"&#62;&#60;a href="'._url('picture',array('col_key'=&#62;'cert','pic_id'=&#62;$assign[$i][pic_id])).'" title=""&#62;&#60;img src="uploads/thumb_'.$assign[$i][pic].'" alt=""&#62;&#60;/a&#62;&#60;/span&#62; &#60;span class="zz_title"&#62;&#60;a href="'._url('picture',array('col_key'=&#62;'cert','pic_id'=&#62;$assign[$i][pic_id])).'" title=""&#62;'.$assign[$i][title].'&#60;/a&#62;&#60;/span&#62; &#60;/li&#62; &#60;li&#62; &#60;span class="zz_pic"&#62;&#60;a href="'._url('picture',array('col_key'=&#62;'cert','pic_id'=&#62;$assign[$i+1][pic_id])).'" title=""&#62;&#60;img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""&#62;&#60;/a&#62;&#60;/span&#62; &#60;span class="zz_title"&#62;&#60;a href="'._url('picture',array('col_key'=&#62;'cert','pic_id'=&#62;$assign[$i+1][pic_id])).'" title=""&#62;'.$assign[$i+1][title].'&#60;/a&#62;&#60;/span&#62; &#60;/li&#62;i&#62; &#60;/ul&#62;';} {{/php}} 解决的方法是：模版变量全部存在smarty的一个对象里面；只要在for之前进行赋值：$assign = $this-&#62;_tpl_vars[assign]; {{php}} $assign = $this-&#62;_tpl_vars[assign]; for($i=0;$i&#60;count($assign);$i=$i+2){ echo ' &#60;ul&#62; &#60;li&#62; &#60;span class="zz_pic"&#62;&#60;a href="'._url('picture',array('col_key'=&#62;'cert','pic_id'=&#62;$assign[$i][pic_id])).'" title=""&#62;&#60;img src="uploads/thumb_'.$assign[$i][pic].'" alt=""&#62;&#60;/a&#62;&#60;/span&#62; &#60;span class="zz_title"&#62;&#60;a href="'._url('picture',array('col_key'=&#62;'cert','pic_id'=&#62;$assign[$i][pic_id])).'" title=""&#62;'.$assign[$i][title].'&#60;/a&#62;&#60;/span&#62; &#60;/li&#62; [...]]]></description>
			<content:encoded><![CDATA[<p>已经assign一个模版变量$assign,由于要做特殊的循环输出，使用for循环，因此使用到了php标签，但是php语句和模版语句的变量作用域是不同的，因此不能直接获取到</p>
<p>  {{php}}</p>
<p>for($i=0;$i&lt;count($assign);$i=$i+2){<br />
echo '<br />
&lt;ul&gt;<br />
&lt;li&gt; &lt;span class="zz_pic"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i][pic_id])).'" title=""&gt;&lt;img src="uploads/thumb_'.$assign[$i][pic].'" alt=""&gt;&lt;/a&gt;&lt;/span&gt; &lt;span class="zz_title"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i][pic_id])).'" title=""&gt;'.$assign[$i][title].'&lt;/a&gt;&lt;/span&gt; &lt;/li&gt;<br />
&lt;li&gt; &lt;span class="zz_pic"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i+1][pic_id])).'" title=""&gt;&lt;img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""&gt;&lt;/a&gt;&lt;/span&gt; &lt;span class="zz_title"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i+1][pic_id])).'" title=""&gt;'.$assign[$i+1][title].'&lt;/a&gt;&lt;/span&gt; &lt;/li&gt;i&gt;</p>
<p>&lt;/ul&gt;';}<br />
{{/php}}</p>
<p>解决的方法是：模版变量全部存在smarty的一个对象里面；只要在for之前进行赋值：$assign = $this-&gt;_tpl_vars[assign];</p>
<p>{{php}}<br />
$assign = $this-&gt;_tpl_vars[assign];<br />
for($i=0;$i&lt;count($assign);$i=$i+2){<br />
echo '<br />
&lt;ul&gt;<br />
&lt;li&gt; &lt;span class="zz_pic"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i][pic_id])).'" title=""&gt;&lt;img src="uploads/thumb_'.$assign[$i][pic].'" alt=""&gt;&lt;/a&gt;&lt;/span&gt; &lt;span class="zz_title"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i][pic_id])).'" title=""&gt;'.$assign[$i][title].'&lt;/a&gt;&lt;/span&gt; &lt;/li&gt;<br />
&lt;li&gt; &lt;span class="zz_pic"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i+1][pic_id])).'" title=""&gt;&lt;img src="uploads/thumb_'.$assign[$i+1][pic].'" alt=""&gt;&lt;/a&gt;&lt;/span&gt; &lt;span class="zz_title"&gt;&lt;a href="'._url('picture',array('col_key'=&gt;'cert','pic_id'=&gt;$assign[$i+1][pic_id])).'" title=""&gt;'.$assign[$i+1][title].'&lt;/a&gt;&lt;/span&gt; &lt;/li&gt;i&gt;</p>
<p>&lt;/ul&gt;';}<br />
{{/php}}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-409.html/feed</wfw:commentRss>
		<slash:comments>502</slash:comments>
		</item>
		<item>
		<title>好用的smarty标签：capture，literal，fetch</title>
		<link>http://www.zui88.com/blog/view-407.html</link>
		<comments>http://www.zui88.com/blog/view-407.html#comments</comments>
		<pubDate>Sat, 22 Sep 2012 03:16:24 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=407</guid>
		<description><![CDATA[1，capture标签 capture的中文意思是抓取，它的作用是抓取模板输出的数据，当我们需要它的时候，调用它，以得到抓取数据的目的。例子： {capture name=test} &#60;img src=”testimg.jpg”&#62; {/capture} &#60;div class=”image”&#62; {$smarty.capture.test} &#60;/div&#62; 说明： 在{capture name=”test”}和{/capture}之间的内容被存储到变量$test中，该变量由name属性指定.在模板中通过 $smarty.capture.test 访问该变量.如果没有指定name 属性,函数默认将使用”default” 作为参数,这一点很jquery中的clone 2，config_load标签 config_load可以直接将文件中的内容读取出来，这样可以省掉assign这一步。 test.csv: pageTitle = ”config_load_test” bodyBgColor = ”#eeeeee” img = ”girl.jpg” width=”100″ height=”100″ index.tpl: {config_load file=”test.csv”} &#60;html&#62; &#60;title&#62;{#pageTitle#}&#60;/title&#62; &#60;body bgcolor=”{#bodyBgColor#}”&#62; &#60;img src=”{#img#}” width=”{#width#}” height=”{#height#}”&#62; &#60;/body&#62; &#60;/html&#62; 上述过程中如果出现这样的问题Warning: Smarty error: unable to read resource, 请查看一下，你的test.csv是不是放在smarty的配置目录中，默认配置目录是configs /** * The directory where config files are located. * * @var string */ var $config_dir      =  ’configs’; 3，literal标签的使用 做web开发，难免会写一些JS，jquery代码。js和jquery里面都会｛｝这样的符号，smarty会不会把它理解成php的变量呢？如果你不加literal标签的话，smarty肯定会把它理解变量了，加了就不会，例如： {literal} function getAbsLeft(e){ var l=e.offsetLeft; while(e=e.offsetParent)l+=e.offsetLeft; return l; } function getAbsTop(e){ var t=e.offsetTop; [...]]]></description>
			<content:encoded><![CDATA[<div id="article_content" class="article_content">
<p><span><strong>1，capture标签</strong></p>
<p>capture的中文意思是抓取，它的作用是抓取模板输出的数据，当我们需要它的时候，调用它，以得到抓取数据的目的。例子：</p>
<div>
<ol>
<li>{capture name=test}</li>
<li>&lt;img src=”testimg.jpg”&gt;</li>
<li>{/capture}</li>
<li>&lt;div class=”image”&gt;</li>
<li>{$smarty.capture.test}</li>
<li>&lt;/div&gt;</li>
</ol>
</div>
<p>说明：<br />
在{capture name=”test”}和{/capture}之间的内容被存储到变量$test中，该变量由name属性指定.在模板中通过  $smarty.capture.test 访问该变量.如果没有指定name 属性,函数默认将使用”default”  作为参数,这一点很jquery中的clone</p>
<p><strong>2，config_load标签</strong></p>
<p>config_load可以直接将文件中的内容读取出来，这样可以省掉assign这一步。</p>
<div>
<ol>
<li>test.csv:</li>
<li>pageTitle = ”config_load_test”</li>
<li>bodyBgColor = ”#eeeeee”</li>
<li>img = ”girl.jpg”</li>
<li>width=”100″</li>
<li>height=”100″</li>
<li>index.tpl:</li>
<li>{config_load file=”test.csv”}</li>
<li>&lt;html&gt;</li>
<li>&lt;title&gt;{#pageTitle#}&lt;/title&gt;</li>
<li>&lt;body bgcolor=”{#bodyBgColor#}”&gt;</li>
<li>&lt;img src=”{#img#}” width=”{#width#}” height=”{#height#}”&gt;</li>
<li>&lt;/body&gt;</li>
<li>&lt;/html&gt;</li>
</ol>
</div>
<p><strong>上述过程中如果出现这样的问题Warning: Smarty error: unable to read resource,  请查看一下，你的test.csv是不是放在smarty的配置目录中，默认配置目录是configs</strong></p>
<div>
<ol>
<li>/**</li>
<li>* The directory where config files are located.</li>
<li>*</li>
<li>* @var string</li>
<li>*/</li>
<li>var $config_dir      =  ’configs’;</li>
</ol>
</div>
<p><strong>3，literal标签的使用</strong></p>
<p>做web开发，难免会写一些JS，jquery代码。js和jquery里面都会｛｝这样的符号，smarty会不会把它理解成php的变量呢？如果你不加literal标签的话，smarty肯定会把它理解变量了，加了就不会，例如：</p>
<div>
<ol>
<li>{literal}</li>
<li>function getAbsLeft(e){</li>
<li>var l=e.offsetLeft;</li>
<li>while(e=e.offsetParent)l+=e.offsetLeft;</li>
<li>return l;</li>
<li>}</li>
<li>function getAbsTop(e){</li>
<li>var t=e.offsetTop;</li>
<li>while(e=e.offsetParent)t+=e.offsetTop;</li>
<li>return t;</li>
<li>}</li>
<li>{/literal}</li>
</ol>
</div>
<p><strong>4，php标签</strong></p>
<p>当你习惯了assign后，你有没有想过，在模板文件里面直接写php代码呢，我想有的时候你肯定很想吧。例如：</p>
<div>
<ol>
<li>{php}</li>
<li>global $result;</li>
<li>foreach($result as $key=&gt;$value){</li>
<li>echo ”key=$key,value=&gt;$value&lt;br&gt;”;</li>
<li>}</li>
<li>{/php}</li>
</ol>
</div>
<p><strong>5，strip标签</strong></p>
<p>strip标签去除标签内的空格和回车，这一点我觉得，做手机开发的朋友肯定用的到，因为全角空格有可能会导致整个页面错乱，甚至是一个空白页面。手机屏幕小，估计用smarty的可能性也比较小。</p>
<div>
<ol>
<li>{strip}</li>
<li>&lt;div&gt;</li>
<li>&lt;font color=”red”&gt;strip&lt;/font&gt;</li>
<li>&lt;/div&gt;</li>
<li>{/strip}</li>
</ol>
</div>
<p><strong>6,fetch标签</strong></p>
<p>fetch标签根php的file_get_contents挺想的，都可以把文件中的内容读出来，并且是个字符串的形势</p>
<div>
<ol>
<li>{fetch file=”./aaaa.txt” assign=”result”}</li>
<li>{if is_array($result)}</li>
<li>&lt;b&gt;is array&lt;/b&gt;</li>
<li>{else if}</li>
<li>&lt;b&gt;not array&lt;/b&gt;</li>
<li>{/if}</li>
</ol>
</div>
<p></span></p>
<p>&nbsp;</p>
</div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-407.html/feed</wfw:commentRss>
		<slash:comments>481</slash:comments>
		</item>
		<item>
		<title>smarty 利用@ 在模版完整打印多维数组</title>
		<link>http://www.zui88.com/blog/view-388.html</link>
		<comments>http://www.zui88.com/blog/view-388.html#comments</comments>
		<pubDate>Sat, 21 Jul 2012 01:41:11 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=388</guid>
		<description><![CDATA[有时候我们希望直接在模版上打印数组变量以供调试，打印的方式可以用php自带的print_r或者是自己写的调试函数，如debug(). 如果直接这样打印多维数组 {{$var&#124;print_r}}，在模版看到的结果会是遍历后的所有的value，不会显示完整的数组结构，正确的方法是在函数前加个@，意思是把变量作为整体去对待 {{$var&#124;@print_r}}]]></description>
			<content:encoded><![CDATA[<p>有时候我们希望直接在模版上打印数组变量以供调试，打印的方式可以用php自带的print_r或者是自己写的调试函数，如debug().</p>
<p>如果直接这样打印多维数组 {{$var|print_r}}，在模版看到的结果会是遍历后的所有的value，不会显示完整的数组结构，正确的方法是在函数前加个@，意思是把变量作为整体去对待</p>
<p>{{$var|@print_r}}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-388.html/feed</wfw:commentRss>
		<slash:comments>613</slash:comments>
		</item>
		<item>
		<title>smarty模版函数含多参数的使用规则</title>
		<link>http://www.zui88.com/blog/view-322.html</link>
		<comments>http://www.zui88.com/blog/view-322.html#comments</comments>
		<pubDate>Wed, 01 Feb 2012 09:38:50 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=322</guid>
		<description><![CDATA[&#160; 模板中调用变量时，当只有一个参数是，就直接{$str1&#124;函数名}，当有函数有两个参数时，{第一个参数&#124;函数名:第二个参数}，当有三个参数时，{第一个参数&#124;函数名:第二个参数:第三个参数}，，当有4，5，，，参数时，以此类推。 smarty在模板上可以直接使用php自带的函数，甚至可以使用自定义的函数。 smarty使用date函数的用法是{{'Y-m-d'&#124;date:$var}} &#160;]]></description>
			<content:encoded><![CDATA[<div class="entry-content">
<p>&nbsp;</p>
<p>模板中调用变量时，当只有一个参数是，就直接{$str1|函数名}，当有函数有两个参数时，{第一个参数|函数名:第二个参数}，当有三个参数时，{第一个参数|函数名:第二个参数:第三个参数}，，当有4，5，，，参数时，以此类推。</p>
<p>smarty在模板上可以直接使用php自带的函数，甚至可以使用自定义的函数。</p>
<p>smarty使用date函数的用法是{{'Y-m-d'|date:<img src="file:///C:/DOCUME%7E1/ADMINI%7E1/LOCALS%7E1/Temp/moz-screenshot-3.png" alt="" /><img src="file:///C:/DOCUME%7E1/ADMINI%7E1/LOCALS%7E1/Temp/moz-screenshot-4.png" alt="" />$var}}</p>
</div>
<p>&nbsp;</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-322.html/feed</wfw:commentRss>
		<slash:comments>359</slash:comments>
		</item>
		<item>
		<title>Smarty模版中，数组的键名是一个变量的值，如何显示该键名对应的值</title>
		<link>http://www.zui88.com/blog/view-261.html</link>
		<comments>http://www.zui88.com/blog/view-261.html#comments</comments>
		<pubDate>Thu, 08 Sep 2011 08:45:20 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=261</guid>
		<description><![CDATA[题目有点绕口，大概的意思是 php已经赋给模版一个数组，数组的信息如下： $config= array( 1=&#62;'中山', 2=&#62;'石岐' ); 数据库存储地区的字段记录的是该数组的键名，如1，现在要在模版上显示：中山。 如果这样写会报错： {{$config.$row.region}} 模版上的正确的写法是：{{$config[$row.region]}} 今天遇到的问题还更复杂一点，数据库字段存储的是一些配置的序列化，所以在调取地区信息时还需要进行反序列化处理，中间必须有一个赋值的过程： {{assign var=param value=$l.params&#124;unserialize}} 然后$param.region就可以取得1这个值了]]></description>
			<content:encoded><![CDATA[<p>题目有点绕口，大概的意思是</p>
<p>php已经赋给模版一个数组，数组的信息如下：</p>
<p>$config= array(</p>
<p>1=&gt;'中山',</p>
<p>2=&gt;'石岐'</p>
<p>);</p>
<p>数据库存储地区的字段记录的是该数组的键名，如1，现在要在模版上显示：中山。</p>
<p>如果这样写会报错：</p>
<p>{{$config.$row.region}}</p>
<p>模版上的正确的写法是：{{$config[$row.region]}}</p>
<p>今天遇到的问题还更复杂一点，数据库字段存储的是一些配置的序列化，所以在调取地区信息时还需要进行反序列化处理，中间必须有一个赋值的过程：</p>
<p>{{assign var=param value=$l.params|unserialize}}</p>
<p>然后$param.region就可以取得1这个值了</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-261.html/feed</wfw:commentRss>
		<slash:comments>210</slash:comments>
		</item>
		<item>
		<title>smarty使用date函数</title>
		<link>http://www.zui88.com/blog/view-147.html</link>
		<comments>http://www.zui88.com/blog/view-147.html#comments</comments>
		<pubDate>Fri, 19 Aug 2011 10:04:40 +0000</pubDate>
		<dc:creator>lin</dc:creator>
				<category><![CDATA[后端程序]]></category>
		<category><![CDATA[smarty]]></category>

		<guid isPermaLink="false">http://www.zui88.com/blog/?p=147</guid>
		<description><![CDATA[smarty在模板上可以直接使用php自带的函数，甚至可以使用自定义的函数，使用的方法是： 模板中调用变量时，当只有一个参数是，就直接{$str1&#124;函数名}，当有函数有两个参数时，{第一个参数&#124;函数名:第二个参数}，当有三个参数时，{第一个参数&#124;函数名:第二个参数:第三个参数}，，当有4，5，，，参数时，以此类推。 smarty使用date函数的用法是{{'Y-m-d'&#124;date:$var}}]]></description>
			<content:encoded><![CDATA[<p>smarty在模板上可以直接使用php自带的函数，甚至可以使用自定义的函数，使用的方法是：</p>
<p>模板中调用变量时，当只有一个参数是，就直接{$str1|函数名}，当有函数有两个参数时，{第一个参数|函数名:第二个参数}，当有三个参数时，{第一个参数|函数名:第二个参数:第三个参数}，，当有4，5，，，参数时，以此类推。</p>
<p>smarty使用date函数的用法是{{'Y-m-d'|date:<img src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/moz-screenshot-3.png" alt="" /><img src="file:///C:/DOCUME~1/ADMINI~1/LOCALS~1/Temp/moz-screenshot-4.png" alt="" />$var}}</p>
]]></content:encoded>
			<wfw:commentRss>http://www.zui88.com/blog/view-147.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
