What is Smarty?
Why use it?
Use Cases and Work Flow
Syntax Comparison
Template Inheritance
Best Practices
Crash Course
You may use the Smarty logo according to the trademark notice.
For sponsorship, advertising, news or other inquiries, contact us at:
这是基础安装的进阶,请先阅读基础安装文章。
稍微更灵活的方式是使用扩展类来安装Smarty和初始化。 代替反复地定义路径,赋同样的值等等,我们可以把这些操作放在一个地方进行。
我们新建一个目录/php/includes/guestbook/
,并新建一个
setup.php
文件。
在下面的例子中,我们假设/php/includes
目录已经在include_path
中。
确定你已经进行这个配置,或者使用绝对路径。
Example 2.10. /php/includes/guestbook/setup.php
<?php // load Smarty library require('Smarty.class.php'); // The setup.php file is a good place to load // required application library files, and you // can do that right here. An example: // require('guestbook/guestbook.lib.php'); class Smarty_GuestBook extends Smarty { function __construct() { // Class Constructor. // These automatically get set with each new instance. parent::__construct(); $this->setTemplateDir('/web/www.example.com/guestbook/templates/'); $this->setCompileDir('/web/www.example.com/guestbook/templates_c/'); $this->setConfigDir('/web/www.example.com/guestbook/configs/'); $this->setCacheDir('/web/www.example.com/guestbook/cache/'); $this->caching = Smarty::CACHING_LIFETIME_CURRENT; $this->assign('app_name', 'Guest Book'); } } ?>
在index.php
文件中使用setup.php
:
Example 2.11. /web/www.example.com/guestbook/htdocs/index.php
<?php require('guestbook/setup.php'); $smarty = new Smarty_GuestBook(); $smarty->assign('name','Ned'); $smarty->display('index.tpl'); ?>
现在你可以看到这是非常简单就可以实例化一个Smarty的对象,
仅调用Smarty_GuestBook()
就可以自动初始化程序。