Smarty是什么?
为什么使用它?
用例和工作流程
语法比较
模板继承
最佳实践
速成课程
资源插件旨在作为一种通用的方式,为 Smarty 提供模板源或 PHP 脚本组件。一些资源的示例:数据库、LDAP、共享内存、套接字,等等。
自定义资源可以放入 $plugins_dir 中的 resource.foobarxyz.php 文件,或在运行时使用 registerResource() 进行注册。在任何一种情况下,您都可以通过将资源名称添加到要寻址的模板前面来访问该资源:foobarxyz:yourtemplate.tpl。
如果不想通过 Smarty 编译器运行资源的模板,那么自定义资源可以扩展 Smarty_Resource_Uncompiled。然后资源处理程序必须实现函数 renderUncompiled(Smarty_Internal_Template $_template)。 $_template 引用当前模板,并且包含所有已分配的变量,实现者可以通过 $_template->smarty->getTemplateVars() 访问这些变量。这些资源只是将它们呈现的内容回显到输出流。如果 Smarty 实例进行了相应的配置,那么呈现的输出将被输出缓存。请参阅 libs/sysplugins/smarty_internal_resource_php.php 以获取示例。
如果不想将资源的已编译模板缓存在磁盘上,那么自定义资源可以扩展 Smarty_Resource_Recompiled。这些资源在每次访问时都会被编译。这可能是一笔不小的开销。请参阅 libs/sysplugins/smarty_internal_resource_eval.php 以获取示例。
示例 18.10。使用自定义资源
<?php
/**
* MySQL Resource
*
* Resource Implementation based on the Custom API to use
* MySQL as the storage resource for Smarty's templates and configs.
*
* Table definition:
* <pre>CREATE TABLE IF NOT EXISTS `templates` (
* `name` varchar(100) NOT NULL,
* `modified` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
* `source` text,
* PRIMARY KEY (`name`)
* ) ENGINE=InnoDB DEFAULT CHARSET=utf8;</pre>
*
* Demo data:
* <pre>INSERT INTO `templates` (`name`, `modified`, `source`) VALUES ('test.tpl', "2010-12-25 22:00:00", '{$x="hello world"}{$x}');</pre>
*
* @package Resource-examples
* @author Rodney Rehm
*/
class Smarty_Resource_Mysql extends Smarty_Resource_Custom {
// PDO instance
protected $db;
// prepared fetch() statement
protected $fetch;
// prepared fetchTimestamp() statement
protected $mtime;
public function __construct() {
try {
$this->db = new PDO("mysql:dbname=test;host=127.0.0.1", "smarty", "smarty");
} catch (PDOException $e) {
throw new SmartyException('Mysql Resource failed: ' . $e->getMessage());
}
$this->fetch = $this->db->prepare('SELECT modified, source FROM templates WHERE name = :name');
$this->mtime = $this->db->prepare('SELECT modified FROM templates WHERE name = :name');
}
/**
* Fetch a template and its modification time from database
*
* @param string $name template name
* @param string $source template source
* @param integer $mtime template modification timestamp (epoch)
* @return void
*/
protected function fetch($name, &$source, &$mtime)
{
$this->fetch->execute(array('name' => $name));
$row = $this->fetch->fetch();
$this->fetch->closeCursor();
if ($row) {
$source = $row['source'];
$mtime = strtotime($row['modified']);
} else {
$source = null;
$mtime = null;
}
}
/**
* Fetch a template's modification time from database
*
* @note implementing this method is optional. Only implement it if modification times can be accessed faster than loading the comple template source.
* @param string $name template name
* @return integer timestamp (epoch) the template was modified
*/
protected function fetchTimestamp($name) {
$this->mtime->execute(array('name' => $name));
$mtime = $this->mtime->fetchColumn();
$this->mtime->closeCursor();
return strtotime($mtime);
}
}
require_once 'libs/Smarty.class.php';
$smarty = new Smarty();
$smarty->registerResource('mysql', new Smarty_Resource_Mysql());
// using resource from php script
$smarty->display("mysql:index.tpl");
?>
以及在 Smarty 模板内部
{include file='mysql:extras/navigation.tpl'}