'components'=>array(
'viewRenderer' => array(
'class'=>'batman.protected.extensions.SmartyViewRender',
// 这里为Smarty支持的属性
'config' => array (
'left_delimiter' => "{#",
'right_delimiter' => "#}",
'template_dir' => APP_DIR . "/views/",
'config_dir' => APP_DIR . "/views/conf/",
'debugging' => false,
'compile_dir' => 'D:/temp/runtime',
)
)
Yii::setPathOfAlias('batman', dirname(__FILE__));
Yii::import("batman.protected.vendors.*");
define('APP_DIR', dirname(__FILE__).'/protected/');
<?php
class SmartyViewRender extends CApplicationComponent implements IViewRenderer {
public $fileExtension = '.html';
private $_smarty = null;
public $config = array();
public function init() {
$smartyPath = Yii::getPathOfAlias('batman.protected.vendors.smarty');
Yii::$classMap['Smarty'] = $smartyPath . '/Smarty.class.php';
Yii::$classMap['Smarty_Internal_Data'] = $smartyPath . '/sysplugins/smarty_internal_data.php';
$this->_smarty = new Smarty();
// configure smarty
if (is_array ( $this->config )) {
foreach ( $this->config as $key => $value ) {
if ($key {0} != '_') { // not setting semi-private properties
$this->_smarty->$key = $value;
}
}
}
Yii::registerAutoloader('smartyAutoload');
}
public function renderFile($context, $file, $data, $return) {
foreach ($data as $key => $value)
$this->_smarty->assign($key, $value);
$return = $this->_smarty->fetch($file);
if ($return)
return $return;
else
echo $return;
}
}
<?php
class HelloController extends Controller {
public function actionWorld() {
$this->render('world', array('content'=>'hello world'));
}
}
<body>
{#$content#}
</body>
public function index()
{
$this->load->library('smarty/Ci_smarty', '', 'smarty');
$this->smarty->assign("title","恭喜你smarty安装成功!");
$this->smarty->assign("body","欢迎使用smarty模板引擎");
$arr = array(1=>'zhang',2=>'xing',3=>'wang');
$this->smarty->assign("myarray",$arr);
$this->smarty->display('index_2.html');
}
<?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);
}
}
<?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);
}
}
[common]
application.directory = APP_PATH "/application"
application.dispatcher.catchException = TRUE
application.view.ext="tpl"
[smarty : common]
;configures for smarty
smarty.left_delimiter = "{#"
smarty.right_delimiter = "#}"
smarty.template_dir = APP_PATH "/application/views/"
smarty.compile_dir = '/data1/www/cache/'
smarty.cache_dir = '/data1/www/cache/'
[product : smarty]
public function twoAction() {
$this->getView()->assign('content', 'hello World');
}
<html>
<head>
<title>A Smarty Adapter Example</title>
</head>
<body>
{#$content#}
</body>
</html>
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有