1.下载Twig源码 https://github.com/twigphp/Twig/tags

2.复制lib文件夹下核心目录到CI框架的 common 目录

3.在CI框架创建扩展文件,路径: ./application/libraries/Twig.php 扩展代码:

<?php
·  /**
·   * Created by PhpStorm.
·   * User: Swain
·   * Date: 2016/6/8 
·   */
·  require BASEPATH."common/Twig/Autoloader.php";
·  class twig
·  {
·  	public $twig;
·   
·  	public $config;
·   
·  	private $data = array();
·   
·  	/**
·  	 * 读取配置文件twig.php并初始化设置
·  	 *
·  	 */
·  	public function __construct($config)
·  	{
·  		$config_default = array(
·  			'cache_dir' => false, //开启缓存
·  			'debug' => false, //开启调试模式(dump函数可用)
·  			'auto_reload' => true,
·  			'extension' => '.tpl', //默认后缀名
·  		);
·  		$this->config = array_merge($config_default, $config);
·  		Twig_Autoloader::register ();
·  		$loader = new Twig_Loader_Filesystem ($this->config['template_dir']);
·  		$this->twig = new Twig_Environment ($loader, array (
·  			'cache' => $this->config['cache_dir'],
·  			'debug' => $this->config['debug'],
·  			'auto_reload' => $this->config['auto_reload'],
·  		) );
·  		$CI = & get_instance ();
·  		$CI->load->helper(array('url'));
·  		$this->twig->addFunction(new Twig_SimpleFunction('site_url', 'site_url'));
·  		$this->twig->addFunction(new Twig_SimpleFunction('base_url', 'base_url'));
·  	}
·   
·  	/**
·  	 * 给变量赋值
·  	 *
·  	 * @param string|array $var
·  	 * @param string $value
·  	 */
·  	public function assign($var, $value = NULL)
·  	{
·  		if(is_array($var)) {
·  			foreach($var as $key => $val) {
·  				$this->data[$key] = $val;
·  			}
·  		} else {
·  			$this->data[$var] = $value;
·  		}
·  	}
·   
·  	/**
·  	 * 模版渲染
·  	 *
·  	 * @param string $template 模板名
·  	 * @param array $data 变量数组
·  	 * @param string $return true返回 false直接输出页面
·  	 * @return string
·  	 */
·  	public function render($template, $data = array(), $return = FALSE)
·  	{
·  		$template = $this->twig->loadTemplate ( $this->getTemplateName($template) );
·  		$data = array_merge($this->data, $data);
·  		if ($return === TRUE) {
·  			return $template->render ( $data );
·  		} else {
·  			return $template->display ( $data );
·  		}
·  	}
·   
·  	/**
·  	 * 获取模版名
·  	 *
·  	 * @param string $template
·  	 */
·  	public function getTemplateName($template)
·  	{
·  		$default_ext_len = strlen($this->config['extension']);
·  		if(substr($template, -$default_ext_len) != $this->config['extension']) {
·  			$template .= $this->config['extension'];
·  		}
·  		return $template;
·  	}
·   
·  	/**
·  	 * 字符串渲染
·  	 *
·  	 * @param string $string 需要渲染的字符串
·  	 * @param array $data 变量数组
·  	 * @param string $return true返回 false直接输出页面
·  	 * @return string
·  	 */
·  	public function parse($string, $data = array(), $return = FALSE)
·  	{
·  		$string = $this->twig->loadTemplate ( $string );
·  		$data = array_merge($this->data, $data);
·  		if ($return === TRUE) {
·  			return $string->render ( $data );
·  		} else {
·  			return $string->display ( $data );
·  		}
·  	}
·  }

4.在CI框架创建配制文件,路径: ./application/config/twig.php 配制代码:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
// 默认扩展名
$config['extension'] = ".twig";
// 默认模版路径
$config['template_dir'] = APPPATH . "views/";
// 缓存目录
$config['cache_dir'] = APPPATH . "cache/twig/";
// 是否开启调试模式
$config['debug'] = false;
// 自动刷新
$config['auto_reload'] = true;

5.设置自动加载 ./application/config/autoload.php 把扩展及配制自动加载

$autoload['libraries'] = array('Twig');

$autoload['config']   = array('twig');

Twig模板的使用

1.设置模板引擎自动加载后。控制器中加入下面语句调用模板

$loader = new Twig_Loader_Filesystem('application/path');//模板路径

$twig = new Twig_Environment($loader, array(

'cache' => '/path/compilation_cache',  //设置缓存,建议调试阶段关闭(缓存太//强大删不掉的那种)

));

$template = $twig->loadTemplate('test.twig');  //载入模板名称

$template->display($data);//变量数据传入模板展示

开发文档:http://twig.sensiolabs.org/documentation



Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐