snipt

Ctrl+h for KB shortcuts

HTML+PHP

tmdphp tmd_tpl 模板调用

// 模板中调用变量
<div id="logo">
    <h1><a href="#">{$site_name}</a></h1>
    <p>{$site_intro}</p>
</div>
 
// 调用数组
<h2 class="title"><a href="#">{$blog.title}</a></h2>
 
// 二维数组
<a href="{$blog.info.weibo}" target="_blank">{$blog.info.author}</a>
 
// 这些模板引擎的基本功能,在tmd_tpl中也是一样的。
 
// 但是循环和判断,tmd_tpl观点是直接用php代码。没必要创造个模板语言来实现。
<?php
foreach ($links as $name => $url) {
?>
  <li><a href="{$url}" target="_blank">{$name}</a></li>
<?php
}
?>
 
// 函数方面,对目前的tmd_tpl来说,是一个弱项。
// 还不支持 {$blog.content|nl2br} 这样子的格式。
 
{:nl2br( $blog['content'] )} // 只能这样
<?=nl2br( $blog['content'] )?> // 或者这样
// 都将自动转换为
<?php echo nl2br( $blog['content'] )?>
 
// 如果调用的是一些无返回值的函数
{~print_r( $blog )}
https://snipt.net/embed/fd8a35e43d15669201f637b4268a40f1/
/raw/fd8a35e43d15669201f637b4268a40f1/
fd8a35e43d15669201f637b4268a40f1
html+php
HTML+PHP
33
2019-08-22T17:16:39
True
False
False
Sep 21, 2012 at 03:14 AM
/api/public/snipt/47434/
tmdphp-tmd_tpl-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1"> 1</a> <a href="#L-2"> 2</a> <a href="#L-3"> 3</a> <a href="#L-4"> 4</a> <a href="#L-5"> 5</a> <a href="#L-6"> 6</a> <a href="#L-7"> 7</a> <a href="#L-8"> 8</a> <a href="#L-9"> 9</a> <a href="#L-10">10</a> <a href="#L-11">11</a> <a href="#L-12">12</a> <a href="#L-13">13</a> <a href="#L-14">14</a> <a href="#L-15">15</a> <a href="#L-16">16</a> <a href="#L-17">17</a> <a href="#L-18">18</a> <a href="#L-19">19</a> <a href="#L-20">20</a> <a href="#L-21">21</a> <a href="#L-22">22</a> <a href="#L-23">23</a> <a href="#L-24">24</a> <a href="#L-25">25</a> <a href="#L-26">26</a> <a href="#L-27">27</a> <a href="#L-28">28</a> <a href="#L-29">29</a> <a href="#L-30">30</a> <a href="#L-31">31</a> <a href="#L-32">32</a> <a href="#L-33">33</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a>// 模板中调用变量 </span><span id="L-2"><a name="L-2"></a><span class="p">&lt;</span><span class="nt">div</span> <span class="na">id</span><span class="o">=</span><span class="s">&quot;logo&quot;</span><span class="p">&gt;</span> </span><span id="L-3"><a name="L-3"></a> <span class="p">&lt;</span><span class="nt">h1</span><span class="p">&gt;&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;#&quot;</span><span class="p">&gt;</span>{$site_name}<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;/</span><span class="nt">h1</span><span class="p">&gt;</span> </span><span id="L-4"><a name="L-4"></a> <span class="p">&lt;</span><span class="nt">p</span><span class="p">&gt;</span>{$site_intro}<span class="p">&lt;/</span><span class="nt">p</span><span class="p">&gt;</span> </span><span id="L-5"><a name="L-5"></a><span class="p">&lt;/</span><span class="nt">div</span><span class="p">&gt;</span> </span><span id="L-6"><a name="L-6"></a> </span><span id="L-7"><a name="L-7"></a>// 调用数组 </span><span id="L-8"><a name="L-8"></a><span class="p">&lt;</span><span class="nt">h2</span> <span class="na">class</span><span class="o">=</span><span class="s">&quot;title&quot;</span><span class="p">&gt;&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;#&quot;</span><span class="p">&gt;</span>{$blog.title}<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;/</span><span class="nt">h2</span><span class="p">&gt;</span> </span><span id="L-9"><a name="L-9"></a> </span><span id="L-10"><a name="L-10"></a>// 二维数组 </span><span id="L-11"><a name="L-11"></a><span class="p">&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;{$blog.info.weibo}&quot;</span> <span class="na">target</span><span class="o">=</span><span class="s">&quot;_blank&quot;</span><span class="p">&gt;</span>{$blog.info.author}<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;</span> </span><span id="L-12"><a name="L-12"></a> </span><span id="L-13"><a name="L-13"></a>// 这些模板引擎的基本功能,在tmd_tpl中也是一样的。 </span><span id="L-14"><a name="L-14"></a> </span><span id="L-15"><a name="L-15"></a>// 但是循环和判断,tmd_tpl观点是直接用php代码。没必要创造个模板语言来实现。 </span><span id="L-16"><a name="L-16"></a><span class="cp">&lt;?php</span> </span><span id="L-17"><a name="L-17"></a><span class="k">foreach</span> <span class="p">(</span><span class="nv">$links</span> <span class="k">as</span> <span class="nv">$name</span> <span class="o">=&gt;</span> <span class="nv">$url</span><span class="p">)</span> <span class="p">{</span> </span><span id="L-18"><a name="L-18"></a><span class="cp">?&gt;</span> </span><span id="L-19"><a name="L-19"></a> <span class="p">&lt;</span><span class="nt">li</span><span class="p">&gt;&lt;</span><span class="nt">a</span> <span class="na">href</span><span class="o">=</span><span class="s">&quot;{$url}&quot;</span> <span class="na">target</span><span class="o">=</span><span class="s">&quot;_blank&quot;</span><span class="p">&gt;</span>{$name}<span class="p">&lt;/</span><span class="nt">a</span><span class="p">&gt;&lt;/</span><span class="nt">li</span><span class="p">&gt;</span> </span><span id="L-20"><a name="L-20"></a><span class="cp">&lt;?php</span> </span><span id="L-21"><a name="L-21"></a><span class="p">}</span> </span><span id="L-22"><a name="L-22"></a><span class="cp">?&gt;</span> </span><span id="L-23"><a name="L-23"></a> </span><span id="L-24"><a name="L-24"></a>// 函数方面,对目前的tmd_tpl来说,是一个弱项。 </span><span id="L-25"><a name="L-25"></a>// 还不支持 {$blog.content|nl2br} 这样子的格式。 </span><span id="L-26"><a name="L-26"></a> </span><span id="L-27"><a name="L-27"></a>{:nl2br( $blog[&#39;content&#39;] )} // 只能这样 </span><span id="L-28"><a name="L-28"></a><span class="cp">&lt;?</span><span class="o">=</span><span class="nb">nl2br</span><span class="p">(</span> <span class="nv">$blog</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span> <span class="p">)</span><span class="cp">?&gt;</span> // 或者这样 </span><span id="L-29"><a name="L-29"></a>// 都将自动转换为 </span><span id="L-30"><a name="L-30"></a><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nb">nl2br</span><span class="p">(</span> <span class="nv">$blog</span><span class="p">[</span><span class="s1">&#39;content&#39;</span><span class="p">]</span> <span class="p">)</span><span class="cp">?&gt;</span> </span><span id="L-31"><a name="L-31"></a> </span><span id="L-32"><a name="L-32"></a>// 如果调用的是一些无返回值的函数 </span><span id="L-33"><a name="L-33"></a>{~print_r( $blog )} </span></pre></div> </td></tr></table>
php, template, tmd_tpl, tmdphp, 模板引擎, 模板调用