什么是 Smarty?
为什么要使用它?
使用案例和工作流
语法比较
模板继承
最佳实践
速成课
您可以使用 PHP 可以访问的任何可能来源检索模板:数据库、套接字、文件等。您可以通过编写资源插件函数并将其注册到 Smarty 中来实现此目的。
有关应提供函数的详细信息,请参阅 资源插件 部分。
请注意,您无法覆盖内置的 file:
资源,但您可以通过以其他资源名称进行注册,提供以某种方式从文件系统获取模板的资源。
示例 16.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'}