yii,CI,yaf框架+smarty模板使用方法(2)

class Home extends CI_Controller { public function index() { $data['todo_list'] = array('Clean House', 'Call Mom', 'Run Errands'); $data['title'] = "恭喜你smarty安装成功!"; $data['body'] = "欢迎使用smarty模板引"; $arr = array(1=>'zhang',2=>'xing',3=>'wang'); $data['myarray'] = $arr; $this->load->view('index_2', $data); } }

在views下面建一个index_2.html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <script src='https://www.jb51.net/<!--{$base_url}-->js/jquery.min.js' type='text/javascript' ></script> <link href="https://www.jb51.net/<!--{$base_url}-->css/login.css" type="text/css" /> <title>smarty安装测试</title> </head> <body> <h1>{#$title#}</h1> <p>{#$body#}</p> <ul> {#foreach from=$myarray item=v#} <li>{#$v#}</li> {#/foreach#} </ul> </body> </html>

好了,可以试试你的成果了。

三、yaf框架+smarty模板

yaf是利用引导文件Bootstrap.php来加载smarty。

3.1,使用Bootstrap

在index.php中用

复制代码 代码如下:

$app->bootstrap()->run();

引入Bootstrap.php文件

3.2,在application/Bootstrap.php文件中导入smarty。

<?php class Bootstrap extends Yaf_Bootstrap_Abstract { public function _initSmarty(Yaf_Dispatcher $dispatcher) { $smarty = new Smarty_Adapter(null, Yaf_Application::app()->getConfig()->smarty); Yaf_Dispatcher::getInstance()->setView($smarty); } }

3.3,添加Smarty_Adapter类

将smarty解压后放到application/library文件夹下,重命名为Smarty。在Smarty下新建Adapter.php,确保Smarty.class.php在Smarty/libs/下。Adapter.php内容:

<?php Yaf_Loader::import( "Smarty/libs/Smarty.class.php"); Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_templatecompilerbase.php"); Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_templatelexer.php"); Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_templateparser.php"); Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_compilebase.php"); Yaf_Loader::import( "Smarty/libs/sysplugins/smarty_internal_write_file.php"); class Smarty_Adapter implements Yaf_View_Interface { /** * Smarty object * @var Smarty */ public $_smarty; /** * Constructor * * @param string $tmplPath * @param array $extraParams * @return void */ public function __construct($tmplPath = null, $extraParams = array()) { $this->_smarty = new Smarty; if (null !== $tmplPath) { $this->setScriptPath($tmplPath); } foreach ($extraParams as $key => $value) { $this->_smarty->$key = $value; } } /** * Return the template engine object * * @return Smarty */ public function getEngine() { return $this->_smarty; } /** * Set the path to the templates * * @param string $path The directory to set as the path. * @return void */ public function setScriptPath($path) { if (is_readable($path)) { $this->_smarty->template_dir = $path; return; } throw new Exception('Invalid path provided'); } /** * Retrieve the current template directory * * @return string */ public function getScriptPath() { return $this->_smarty->template_dir; } /** * Alias for setScriptPath * * @param string $path * @param string $prefix Unused * @return void */ public function setBasePath($path, $prefix = 'Zend_View') { return $this->setScriptPath($path); } /** * Alias for setScriptPath * * @param string $path * @param string $prefix Unused * @return void */ public function addBasePath($path, $prefix = 'Zend_View') { return $this->setScriptPath($path); } /** * Assign a variable to the template * * @param string $key The variable name. * @param mixed $val The variable value. * @return void */ public function __set($key, $val) { $this->_smarty->assign($key, $val); } /** * Allows testing with empty() and isset() to work * * @param string $key * @return boolean */ public function __isset($key) { return (null !== $this->_smarty->get_template_vars($key)); } /** * Allows unset() on object properties to work * * @param string $key * @return void */ public function __unset($key) { $this->_smarty->clear_assign($key); } /** * Assign variables to the template * * Allows setting a specific key to the specified value, OR passing * an array of key => value pairs to set en masse. * * @see __set() * @param string|array $spec The assignment strategy to use (key or * array of key => value pairs) * @param mixed $value (Optional) If assigning a named variable, * use this as the value. * @return void */ public function assign($spec, $value = null) { if (is_array($spec)) { $this->_smarty->assign($spec); return; } $this->_smarty->assign($spec, $value); } /** * Clear all assigned variables * * Clears all variables assigned to Zend_View either via * {@link assign()} or property overloading * ({@link __get()}/{@link __set()}). * * @return void */ public function clearVars() { $this->_smarty->clear_all_assign(); } /** * Processes a template and returns the output. * * @param string $name The template to process. * @return string The output. */ public function render($name, $value = NULL) { return $this->_smarty->fetch($name); } public function display($name, $value = NULL) { echo $this->_smarty->fetch($name); } }

3.4,smarty配置文件。

再来看看我们的conf/application.ini文件

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/a66abbea4a67e71ebea56405afb0ad4a.html