最轻量的模板实现, Here Document
今天去Google了下Smarty, 发现对其批评甚多.
比如认为其太不轻量, 有100k+的核心库, 而其编译生成的缓存又过于臃肿.
很多人拿Smarty和PHPLIB对比, 后者是一个只有一个类的10+k的模板引擎, 不过貌似在PHP4时代就停止开发了.
后来看到人说phpwind是用的heredoc实现的模板,
就去搜了下heredoc, 其实在php manual里也有:
print <<<END This uses the "here document" syntax to output multiple lines with $variable interpolation. Note that the here document terminator must appear on a line with just a semicolon no extra whitespace! END;
这里的END是结束符, 当然你可以用其他的来代替, 比如phpwind使用的是EOT.
以上代码就实现了代码与变量的准分离, 而且没有用到破坏布局和显示效果的引号.
无疑这样的方式来实现”模板”比使用Smarty更轻量,
也比在HTML里嵌入大量的<? echo $variable ?>更直观简洁.
更多的一些细节可以看这里: http://www.tsingfeng.com/show-667-1.html