<?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>Blog do Beraldo &#187; smarty</title>
	<atom:link href="http://www.rberaldo.com.br/blog/tag/smarty/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.rberaldo.com.br/blog</link>
	<description>Programação, Música, Entretenimento e o que mais me vier à mente</description>
	<lastBuildDate>Wed, 25 Jan 2012 14:23:50 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	
		<item>
		<title>Recursos Interessantes do Smarty</title>
		<link>http://www.rberaldo.com.br/blog/recursos-interessantes-do-smarty/</link>
		<comments>http://www.rberaldo.com.br/blog/recursos-interessantes-do-smarty/#comments</comments>
		<pubDate>Fri, 15 May 2009 19:58:00 +0000</pubDate>
		<dc:creator>Beraldo</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programação]]></category>
		<category><![CDATA[gerenciador de templates]]></category>
		<category><![CDATA[smarty]]></category>
		<category><![CDATA[templates]]></category>

		<guid isPermaLink="false">http://www.rberaldo.com.br/blog/2009/05/15/recursos-interessantes-do-smarty/</guid>
		<description><![CDATA[O Smarty é um dos principais gerenciadores de templates para o PHP. Neste artigo são mostrados alguns recursos interessantes dessa ferramenta.]]></description>
			<content:encoded><![CDATA[<p>Hoje falarei sobre alguns recursos interessantes do gerenciador de templates Smarty (<a href="http://www.smarty.net" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net?referer=');">http://www.smarty.net</a>).</p>
<p>Assumo que o leitor já conhece a sintaxe básica do Smarty, como rodar um template, qual estrutura de diretórios utilizar e como alterar o include_path para facilitar as configurações do gerenciador. Se não souber, veja esta seção: <a href="http://www.smarty.net/manual/pt_BR/installation.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/installation.php?referer=');">http://www.smarty.net/manual/pt_BR/installation.php</a></p>
<p><span id="more-17"></span></p>
<p><a href="#dateformat">1. Formatação de Datas</a><br />
<a href="#defaultvalues">2. Definindo Valores Padrões</a><br />
<a href="#numberformat">3. Formatando Números</a><br />
<a href="#truncatetext">4. Truncando Textos</a><br />
<a href="#savetext">5. Salvando textos em Variáveis</a><br />
<a href="#loops">6. Loops (Estruturas de Repetição)</a><br />
<a href="#counters">7. Contadores</a><br />
<a href="#altvalues">8. Alternando Valores em um Loop</a><br />
<a href="#popform">9. Populando Elemenos de Formulários</a><br />
<a href="#tables">10. Gerando Tabelas HTML</a><br />
<a href="#hideemail">11. Escondendo dos Bots de Spam Endereços de E-Mail</a></p>
<h3><a name="dateformat">1. Formatação de Datas</a></h3>
<p>O Smarty possui um modificador de variável chamado <strong>date_format</strong>. Ele se assemelha à função <strong>strftime()</strong>, do PHP. </p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'data'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'2009-05-15'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #000088;">$data</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$data</span><span style="color: #339933;">|</span>date_format<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$data</span><span style="color: #339933;">|</span>date_format<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span>/%m/%Y&quot;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<pre style="border: dotted 1px #666">
2009-05-15
May 15, 2009
15/05/2009
</pre>
<p>Veja mais detalhes e todos os especificadores de formatação em <a href="http://www.smarty.net/manual/pt_BR/language.modifier.date.format.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.modifier.date.format.php?referer=');">http://www.smarty.net/manual/pt_BR/language.modifier.date.format.php</a></p>
<h3><a name="defaultvalues">2. Definindo Valores Padrões</a></h3>
<p>Para verificar se um valor é diferente de NULL (vazio), costuma-se fazer uma condicional (if). Por exemplko:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{if <span style="color: #006699; font-weight: bold;">$foto</span> != ''}<span style="color: #006699; font-weight: bold;">{$foto}</span>{else}semfoto.gif{/if}&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Porém, isto pode ser simplificado para:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>img src<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;{<span style="color: #006699; font-weight: bold;">$foto</span>|default:&quot;</span>semfoto<span style="color: #339933;">.</span>gif<span style="color: #0000ff;">&quot;}&quot;</span> <span style="color: #339933;">/&gt;</span></pre></td></tr></table></div>

<p>Mais informações: <a href="http://www.smarty.net/manual/pt_BR/language.modifier.default.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.modifier.default.php?referer=');">http://www.smarty.net/manual/pt_BR/language.modifier.default.php</a></p>
<h3><a name="numberformat">3. Formatando Números</a></h3>
<p>Podemos usar <strong>string_format</strong> para formatar um número, usando as mesmas regras da função <strong>printf()</strong>.</p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'numero'</span><span style="color: #339933;">,</span> <span style="color:#800080;">42.757428942</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #000088;">$numero</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$numero</span><span style="color: #339933;">|</span>string_format<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%.2f</span>&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$numero</span><span style="color: #339933;">|</span>string_format<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%07.3f</span>&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$numero</span><span style="color: #339933;">|</span>string_format<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%d</span>&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$numero</span><span style="color: #339933;">|</span>string_format<span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;<span style="color: #009933; font-weight: bold;">%05d</span>&quot;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<pre style="border: dotted 1px #666">
42.757428942
42.76
042.757
42
00042
</pre>
<p>Mais detalhes: <a href="http://www.smarty.net/manual/pt_BR/language.modifier.string.format.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.modifier.string.format.php?referer=');">http://www.smarty.net/manual/pt_BR/language.modifier.string.format.php</a></p>
<h3><a name="truncatetext">4. Truncando Textos</a></h3>
<p>Muitas vezes deseja-se truncar um texto para uma certa quantidade de caracteres ou palavras. Geralmente usa-se <strong>substr()</strong>. O inconveniente dessa função é que ela corta palavras. Há como contornar isso somente com PHP, mas o Smarty facilita ainda mais nesse caso.</p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'texto'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in enim est. Nam placerat adipiscing pretium. Sed at est odio. Vestibulum metus sapien, tincidunt et viverra eget, bibendum molestie justo.'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #000088;">$texto</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$texto</span><span style="color: #339933;">|</span>truncate<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$texto</span><span style="color: #339933;">|</span>truncate<span style="color: #339933;">:</span><span style="color: #cc66cc;">87</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$texto</span><span style="color: #339933;">|</span>truncate<span style="color: #339933;">:</span><span style="color: #cc66cc;">87</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;(continua)&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$texto</span><span style="color: #339933;">|</span>truncate<span style="color: #339933;">:</span><span style="color: #cc66cc;">87</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;...&quot;</span><span style="color: #339933;">:</span><span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<pre style="border: dotted 1px #666">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in enim est. Nam placerat adipiscing pretium. Sed at est odio. Vestibulum metus sapien, tincidunt et viverra eget, bibendum molestie justo.
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in enim est....
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in enim est. Nam...
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in enim est.(continua)
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi in enim est. Nam plac...
</pre>
<p>Mais informações <a href="http://www.smarty.net/manual/pt_BR/language.modifier.truncate.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.modifier.truncate.php?referer=');">http://www.smarty.net/manual/pt_BR/language.modifier.truncate.php</a></p>
<h3><a name="savetext">5. Salvando textos em Variáveis</a></h3>
<p>É possível salvar um texto gerado pelo Smarty em uma variável. Uma aplicação para isso é numa paginação, por exemplo, onde a barra com os números das páginas é exibida mais de uma vez.</p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paginas'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">6</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>capture name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;barra_pag&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #b1b100;">foreach</span> from<span style="color: #339933;">=</span><span style="color: #000088;">$paginas</span> item<span style="color: #339933;">=</span>i<span style="color: #009900;">&#125;</span>
 <span style="color: #009900;">&#123;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#125;</span> 
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>capture<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$smarty</span><span style="color: #339933;">.</span>capture<span style="color: #339933;">.</span>barra_pag<span style="color: #009900;">&#125;</span>
&nbsp;
conteú<span style="color: #b1b100;">do</span> da página
&nbsp;
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$smarty</span><span style="color: #339933;">.</span>capture<span style="color: #339933;">.</span>barra_pag<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<pre style="border: dotted 1px #666">
1 2 3 4 5 6

conteúdo da página

1 2 3 4 5 6
</pre>
<h3><a name="loops">6. Loops (Estruturas de Repetição)</a></h3>
<p>As duas estruturas de repetição do Smarty são <strong>foreach</strong> e <strong>section</strong>. A principal diferença entra as duas é que <strong>section</strong> permite modificar o incremento do contador, como num loop <strong>while</strong>.</p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'array'</span><span style="color: #339933;">,</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">20</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span><span style="color: #b1b100;">foreach</span> from<span style="color: #339933;">=</span><span style="color: #000088;">$array</span> item<span style="color: #339933;">=</span>i <span style="color: #990000;">key</span><span style="color: #339933;">=</span>k<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$k</span><span style="color: #009900;">&#125;</span> <span style="color: #339933;">=&gt;</span> <span style="color: #009900;">&#123;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #009900;">&#123;</span>section name<span style="color: #339933;">=</span>cont loop<span style="color: #339933;">=</span><span style="color: #000088;">$array</span> step<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span>cont<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>section<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#123;</span>section name<span style="color: #339933;">=</span>cont loop<span style="color: #339933;">=</span><span style="color: #000088;">$array</span> step<span style="color: #339933;">=</span><span style="color: #cc66cc;">2</span> start<span style="color: #339933;">=</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #000088;">$array</span><span style="color: #009900;">&#91;</span>cont<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span>section<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<div class="codeBox">
<pre style="border: dotted 1px #666">
0 => 1
1 => 2
2 => 3
3 => 4
4 => 5
5 => 6
6 => 7
7 => 8
8 => 9
9 => 10
10 => 11
11 => 12
12 => 13
13 => 14
14 => 15
15 => 16
16 => 17
17 => 18
18 => 19
19 => 20

1
3
5
7
9
11
13
15
17
19

6
8
10
12
14
16
18
20
</pre>
</div>
<p>Mais informações:<br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.section.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.section.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.section.php</a><br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.foreach.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.foreach.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.foreach.php</a></p>
<h3><a name="counters">7. Contadores</a></h3>
<p>O Smarty tem um recurso próprio para usar contadores dentro de templates. É o <strong>counter</strong>.</p>
<p>Este exemplo não requer a exibição do script PHP. Basta dar um display() no tenplate, não sendo necessário atribuir valor a variáveis de templates.</p>
<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>counter start<span style="color: #339933;">=</span><span style="color: #cc66cc;">0</span> skip<span style="color: #339933;">=</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#123;</span>counter start<span style="color: #339933;">=</span><span style="color: #cc66cc;">7</span> skip<span style="color: #339933;">=</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#123;</span>counter start<span style="color: #339933;">=</span><span style="color: #cc66cc;">77</span> skip<span style="color: #339933;">=</span><span style="color: #cc66cc;">7</span> direction<span style="color: #339933;">=</span>down<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>counter<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<pre style="border: dotted 1px #666">
0
1
2

7
11
15
19

77
70
63
56
49
</pre>
<h3><a name="altvalues">8. Alternando Valores em um Loop</a></h3>
<p>Uma dúvida muito frequente em fóruns é como alternar cores de linhas de tabelas. Há diversas formas de solucionar esse problema apenas com PHP, mas, novamentem o Smarty facilita o trabalho. Usaremos a função <strong>cycle</strong>.</p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'array'</span><span style="color: #339933;">,</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">10</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>table border<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span> width<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;40&quot;</span> cellpadding<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;2&quot;</span> cellspacing<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;1&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span><span style="color: #b1b100;">foreach</span> from<span style="color: #339933;">=</span><span style="color: #000088;">$array</span> item<span style="color: #339933;">=</span>i<span style="color: #009900;">&#125;</span>
  <span style="color: #339933;">&lt;</span>tr style<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;background: {cycle values=&quot;</span><span style="color: #666666; font-style: italic;">#ccc,#666&quot;}&quot;&gt;
</span>    <span style="color: #339933;">&lt;</span>td<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#123;</span><span style="color: #000088;">$i</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">&lt;/</span>td<span style="color: #339933;">&gt;</span>
  <span style="color: #339933;">&lt;/</span>tr<span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span><span style="color: #339933;">/</span><span style="color: #b1b100;">foreach</span><span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>table<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Saída:</p>
<pre style="border: dotted 1px #666">
<table border="1" width="40" cellpadding="2" cellspacing="1">
<tr style="background: #ccc">
<td>1</td>
</tr>
<tr style="background: #666">
<td>2</td>
</tr>
<tr style="background: #ccc">
<td>3</td>
</tr>
<tr style="background: #666">
<td>4</td>
</tr>
<tr style="background: #ccc">
<td>5</td>
</tr>
<tr style="background: #666">
<td>6</td>
</tr>
<tr style="background: #ccc">
<td>7</td>
</tr>
<tr style="background: #666">
<td>8</td>
</tr>
<tr style="background: #ccc">
<td>9</td>
</tr>
<tr style="background: #666">
<td>10</td>
</tr>
</table>
</pre>
<h3><a name="popform">9. Populando Elemenos de Formulários</a></h3>
<p>Há três elementos de formulário que geralmente são populados com valores personalizados, geralmente provenientes de bases de dados: <strong><em>checkboxes</em></strong>, <strong><em>radios</em></strong> e <strong><em>options</em></strong>. O Smarty possui funções específicas para esses três elementos.</p>
<p><strong>Populando <em>checkboxes</em></strong></p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'check_id'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'v1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v3'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v4'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v5'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'check_value'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'valor 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 3'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 4'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 5'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'check_selected'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>html_checkboxes name<span style="color: #339933;">=</span>opcoes values<span style="color: #339933;">=</span><span style="color: #000088;">$check_id</span> output<span style="color: #339933;">=</span><span style="color: #000088;">$check_value</span> selected<span style="color: #339933;">=</span><span style="color: #000088;">$check_selected</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Populando <em>radios</em></strong></p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'radio_id'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'v1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v3'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v4'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v5'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'radio_value'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'valor 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 3'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 4'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 5'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'radio_selected'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>html_radios name<span style="color: #339933;">=</span>opcoes values<span style="color: #339933;">=</span><span style="color: #000088;">$radio_id</span> output<span style="color: #339933;">=</span><span style="color: #000088;">$radio_value</span> checked<span style="color: #339933;">=</span><span style="color: #000088;">$radio_selected</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p><strong>Populando <em>options</em></strong></p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'op_id'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'v1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v3'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v4'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v5'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'op_value'</span><span style="color: #339933;">,</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'valor 1'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 2'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 3'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 4'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'valor 5'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'op_selected'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'v3'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #339933;">&lt;</span>select name<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;opcoes&quot;</span><span style="color: #339933;">&gt;</span>
<span style="color: #009900;">&#123;</span>html_options  values<span style="color: #339933;">=</span><span style="color: #000088;">$op_id</span> output<span style="color: #339933;">=</span><span style="color: #000088;">$op_value</span> selected<span style="color: #339933;">=</span><span style="color: #000088;">$op_selected</span><span style="color: #009900;">&#125;</span>
<span style="color: #339933;">&lt;/</span>select<span style="color: #339933;">&gt;</span></pre></td></tr></table></div>

<p>Saídas das três funcções:</p>
<div class="codeBox">
<pre style="border: dotted 1px #666">
<label>
<input type="checkbox" name="opcoes[]" value="v1" />valor 1</label>
<label>
<input type="checkbox" name="opcoes[]" value="v2" />valor 2</label>
<label>
<input type="checkbox" name="opcoes[]" value="v3" checked="checked" />valor 3</label>

<label>
<input type="checkbox" name="opcoes[]" value="v4" />valor 4</label>
<label>
<input type="checkbox" name="opcoes[]" value="v5" />valor 5</label>

<label>
<input type="radio" name="opcoes" value="v1" />valor 1</label>
<label>
<input type="radio" name="opcoes" value="v2" />valor 2</label>
<label>
<input type="radio" name="opcoes" value="v3" checked="checked" />valor 3</label>
<label>
<input type="radio" name="opcoes" value="v4" />valor 4</label>
<label>
<input type="radio" name="opcoes" value="v5" />valor 5</label>
<select name="opcoes">
<option label="valor 1" value="v1">valor 1</option>
<option label="valor 2" value="v2">valor 2</option>
<option label="valor 3" value="v3" selected="selected">valor 3</option>
<option label="valor 4" value="v4">valor 4</option>
<option label="valor 5" value="v5">valor 5</option>
</select>
</pre>
</div>
<p>Há dois casos particulares para a criação de <em>options</em>: para data e para hora. O Smarty também tem funções específicas para isso.</p>
<p>Para esse caso, não é necessário definir variáveis no script PHP. Basta dar um display() no template.</p>
<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>html_select_date start_year<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;-100&quot;</span> month_format<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;%m&quot;</span> field_order<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;DMY&quot;</span> field_separator<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;/&quot;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<div class="codeBox">
<pre style="border: dotted 1px #666">
<select name="Date_Day">
<option label="01" value="1">01</option>
<option label="02" value="2">02</option>
<option label="03" value="3">03</option>
<option label="04" value="4">04</option>
<option label="05" value="5">05</option>
<option label="06" value="6">06</option>
<option label="07" value="7">07</option>
<option label="08" value="8">08</option>
<option label="09" value="9">09</option>
<option label="10" value="10">10</option>
<option label="11" value="11">11</option>
<option label="12" value="12">12</option>
<option label="13" value="13">13</option>
<option label="14" value="14">14</option>
<option label="15" value="15" selected="selected">15</option>
<option label="16" value="16">16</option>
<option label="17" value="17">17</option>
<option label="18" value="18">18</option>
<option label="19" value="19">19</option>
<option label="20" value="20">20</option>
<option label="21" value="21">21</option>
<option label="22" value="22">22</option>
<option label="23" value="23">23</option>
<option label="24" value="24">24</option>
<option label="25" value="25">25</option>
<option label="26" value="26">26</option>
<option label="27" value="27">27</option>
<option label="28" value="28">28</option>
<option label="29" value="29">29</option>
<option label="30" value="30">30</option>
<option label="31" value="31">31</option>
</select>

/
<select name="Date_Month">
<option label="01" value="01">01</option>
<option label="02" value="02">02</option>
<option label="03" value="03">03</option>
<option label="04" value="04">04</option>
<option label="05" value="05" selected="selected">05</option>
<option label="06" value="06">06</option>
<option label="07" value="07">07</option>
<option label="08" value="08">08</option>
<option label="09" value="09">09</option>
<option label="10" value="10">10</option>
<option label="11" value="11">11</option>
<option label="12" value="12">12</option>
</select>

/
<select name="Date_Year">
<option label="1909" value="1909">1909</option>
<option label="1910" value="1910">1910</option>
<option label="1911" value="1911">1911</option>
<option label="1912" value="1912">1912</option>
<option label="1913" value="1913">1913</option>
<option label="1914" value="1914">1914</option>
<option label="1915" value="1915">1915</option>
<option label="1916" value="1916">1916</option>
<option label="1917" value="1917">1917</option>
<option label="1918" value="1918">1918</option>
<option label="1919" value="1919">1919</option>
<option label="1920" value="1920">1920</option>
<option label="1921" value="1921">1921</option>
<option label="1922" value="1922">1922</option>
<option label="1923" value="1923">1923</option>
<option label="1924" value="1924">1924</option>
<option label="1925" value="1925">1925</option>
<option label="1926" value="1926">1926</option>
<option label="1927" value="1927">1927</option>
<option label="1928" value="1928">1928</option>
<option label="1929" value="1929">1929</option>
<option label="1930" value="1930">1930</option>
<option label="1931" value="1931">1931</option>
<option label="1932" value="1932">1932</option>
<option label="1933" value="1933">1933</option>
<option label="1934" value="1934">1934</option>
<option label="1935" value="1935">1935</option>
<option label="1936" value="1936">1936</option>
<option label="1937" value="1937">1937</option>
<option label="1938" value="1938">1938</option>
<option label="1939" value="1939">1939</option>
<option label="1940" value="1940">1940</option>
<option label="1941" value="1941">1941</option>
<option label="1942" value="1942">1942</option>
<option label="1943" value="1943">1943</option>
<option label="1944" value="1944">1944</option>
<option label="1945" value="1945">1945</option>
<option label="1946" value="1946">1946</option>
<option label="1947" value="1947">1947</option>
<option label="1948" value="1948">1948</option>
<option label="1949" value="1949">1949</option>
<option label="1950" value="1950">1950</option>
<option label="1951" value="1951">1951</option>
<option label="1952" value="1952">1952</option>
<option label="1953" value="1953">1953</option>
<option label="1954" value="1954">1954</option>
<option label="1955" value="1955">1955</option>
<option label="1956" value="1956">1956</option>
<option label="1957" value="1957">1957</option>
<option label="1958" value="1958">1958</option>
<option label="1959" value="1959">1959</option>
<option label="1960" value="1960">1960</option>
<option label="1961" value="1961">1961</option>
<option label="1962" value="1962">1962</option>
<option label="1963" value="1963">1963</option>
<option label="1964" value="1964">1964</option>
<option label="1965" value="1965">1965</option>
<option label="1966" value="1966">1966</option>
<option label="1967" value="1967">1967</option>
<option label="1968" value="1968">1968</option>
<option label="1969" value="1969">1969</option>
<option label="1970" value="1970">1970</option>
<option label="1971" value="1971">1971</option>
<option label="1972" value="1972">1972</option>
<option label="1973" value="1973">1973</option>
<option label="1974" value="1974">1974</option>
<option label="1975" value="1975">1975</option>
<option label="1976" value="1976">1976</option>
<option label="1977" value="1977">1977</option>
<option label="1978" value="1978">1978</option>
<option label="1979" value="1979">1979</option>
<option label="1980" value="1980">1980</option>
<option label="1981" value="1981">1981</option>
<option label="1982" value="1982">1982</option>
<option label="1983" value="1983">1983</option>
<option label="1984" value="1984">1984</option>
<option label="1985" value="1985">1985</option>
<option label="1986" value="1986">1986</option>
<option label="1987" value="1987">1987</option>
<option label="1988" value="1988">1988</option>
<option label="1989" value="1989">1989</option>
<option label="1990" value="1990">1990</option>
<option label="1991" value="1991">1991</option>
<option label="1992" value="1992">1992</option>
<option label="1993" value="1993">1993</option>
<option label="1994" value="1994">1994</option>
<option label="1995" value="1995">1995</option>
<option label="1996" value="1996">1996</option>
<option label="1997" value="1997">1997</option>
<option label="1998" value="1998">1998</option>
<option label="1999" value="1999">1999</option>
<option label="2000" value="2000">2000</option>
<option label="2001" value="2001">2001</option>
<option label="2002" value="2002">2002</option>
<option label="2003" value="2003">2003</option>
<option label="2004" value="2004">2004</option>
<option label="2005" value="2005">2005</option>
<option label="2006" value="2006">2006</option>
<option label="2007" value="2007">2007</option>
<option label="2008" value="2008">2008</option>
<option label="2009" value="2009" selected="selected">2009</option>
</select>
</pre>
</div>
<p><strong>html_select_time</strong> fica para vocês. =P</p>
<p>Mais informações:<br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.html.checkboxes.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.html.checkboxes.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.html.checkboxes.php</a><br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.html.radios.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.html.radios.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.html.radios.php</a><br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.html.options.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.html.options.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.html.options.php</a><br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.html.select.date.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.html.select.date.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.html.select.date.php</a><br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.html.select.time.php" target="_blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.html.select.time.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.html.select.time.php</a></p>
<h3><a name="tables">10. Gerando Tabelas HTML</a></h3>
<p>É possível gerar tabelas HTML a partir de arrays usando <strong>html_table</strong>.</p>
<p>index.php</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$Smarty</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">assign</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'info'</span><span style="color: #339933;">,</span> <span style="color: #990000;">range</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">,</span> <span style="color: #cc66cc;">30</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>html_table loop<span style="color: #339933;">=</span><span style="color: #000088;">$info</span> cols<span style="color: #339933;">=</span><span style="color: #cc66cc;">5</span> table_attr<span style="color: #339933;">=</span><span style="color: #0000ff;">'border=&quot;2&quot;'</span> tr_attr<span style="color: #339933;">=</span><span style="color: #0000ff;">'style=&quot;background: #ccc&quot;'</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Mais informações:<br />
<a href="http://www.smarty.net/manual/pt_BR/language.function.html.table.php" target=_"blank" onclick="urchinTracker('/outgoing/www.smarty.net/manual/pt_BR/language.function.html.table.php?referer=');">http://www.smarty.net/manual/pt_BR/language.function.html.table.php</a></p>
<h3><a name="hideemail">11. Escondendo dos Bots de Spam Endereços de E-Mail</a></h3>
<p>Colocar endereços de e-mail em páginas HTML acarreta uma grande dor de cabeça: os bots de spam logo aparecem para indexar o e-mail em suas listas de destinatários. Uma boa maneira de esconder dos bots os endereços é codificando-os em hexadecimal, por exemplo, ou em JavaScript. O Smarty possui a função <strong>mailto</strong>.</p>
<p>index.tpl</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>mailto address<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email@site.com&quot;</span><span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#123;</span>mailto address<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;email@site.com&quot;</span> encode<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;hex&quot;</span><span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Saída:</p>
<pre>
<a href="mailto:email@site.com" >email@site.com</a>
<a href="&#109;&#97;&#105;&#108;&#116;&#111;&#58;%65%6d%61%69%6c@%73%69%74%65.%63%6f%6d" >&#x65;&#x6d;&#x61;&#x69;&#x6c;&#x40;&#x73;&#x69;&#x74;&#x65;&#x2e;&#x63;&#x6f;&#x6d;</a>
</pre>
<p>Descobri esse recurso recentemente. Até então eu usava um programa em C que convertia um caractere em hexadecimal. Tinha que fazer isso para cada e-mail que colocasse no site. Agora não preciso mais. =D</p>
<p>Espero que o material seja útil. </p>
<p>Abraços!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.rberaldo.com.br/blog/recursos-interessantes-do-smarty/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

