转自:https://blog.csdn.net/swain_yj/article/details/51693906

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

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

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

 


 
  1. <?php

  2. · /**

  3. · * Created by PhpStorm.

  4. · * User: Swain

  5. · * Date: 2016/6/8

  6. · */

  7. · require BASEPATH."common/Twig/Autoloader.php";

  8. · class twig

  9. · {

  10. · public $twig;

  11. ·  

  12. · public $config;

  13. ·  

  14. · private $data = array();

  15. ·  

  16. · /**

  17. · * 读取配置文件twig.php并初始化设置

  18. · *

  19. · */

  20. · public function __construct($config)

  21. · {

  22. · $config_default = array(

  23. · 'cache_dir' => false, //开启缓存

  24. · 'debug' => false, //开启调试模式(dump函数可用)

  25. · 'auto_reload' => true,

  26. · 'extension' => '.tpl', //默认后缀名

  27. · );

  28. · $this->config = array_merge($config_default, $config);

  29. · Twig_Autoloader::register ();

  30. · $loader = new Twig_Loader_Filesystem ($this->config['template_dir']);

  31. · $this->twig = new Twig_Environment ($loader, array (

  32. · 'cache' => $this->config['cache_dir'],

  33. · 'debug' => $this->config['debug'],

  34. · 'auto_reload' => $this->config['auto_reload'],

  35. · ) );

  36. · $CI = & get_instance ();

  37. · $CI->load->helper(array('url'));

  38. · $this->twig->addFunction(new Twig_SimpleFunction('site_url', 'site_url'));

  39. · $this->twig->addFunction(new Twig_SimpleFunction('base_url', 'base_url'));

  40. · }

  41. ·  

  42. · /**

  43. · * 给变量赋值

  44. · *

  45. · * @param string|array $var

  46. · * @param string $value

  47. · */

  48. · public function assign($var, $value = NULL)

  49. · {

  50. · if(is_array($var)) {

  51. · foreach($var as $key => $val) {

  52. · $this->data[$key] = $val;

  53. · }

  54. · } else {

  55. · $this->data[$var] = $value;

  56. · }

  57. · }

  58. ·  

  59. · /**

  60. · * 模版渲染

  61. · *

  62. · * @param string $template 模板名

  63. · * @param array $data 变量数组

  64. · * @param string $return true返回 false直接输出页面

  65. · * @return string

  66. · */

  67. · public function render($template, $data = array(), $return = FALSE)

  68. · {

  69. · $template = $this->twig->loadTemplate ( $this->getTemplateName($template) );

  70. · $data = array_merge($this->data, $data);

  71. · if ($return === TRUE) {

  72. · return $template->render ( $data );

  73. · } else {

  74. · return $template->display ( $data );

  75. · }

  76. · }

  77. ·  

  78. · /**

  79. · * 获取模版名

  80. · *

  81. · * @param string $template

  82. · */

  83. · public function getTemplateName($template)

  84. · {

  85. · $default_ext_len = strlen($this->config['extension']);

  86. · if(substr($template, -$default_ext_len) != $this->config['extension']) {

  87. · $template .= $this->config['extension'];

  88. · }

  89. · return $template;

  90. · }

  91. ·  

  92. · /**

  93. · * 字符串渲染

  94. · *

  95. · * @param string $string 需要渲染的字符串

  96. · * @param array $data 变量数组

  97. · * @param string $return true返回 false直接输出页面

  98. · * @return string

  99. · */

  100. · public function parse($string, $data = array(), $return = FALSE)

  101. · {

  102. · $string = $this->twig->loadTemplate ( $string );

  103. · $data = array_merge($this->data, $data);

  104. · if ($return === TRUE) {

  105. · return $string->render ( $data );

  106. · } else {

  107. · return $string->display ( $data );

  108. · }

  109. · }

  110. · }

 

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

 


 
  1. <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

  2. // 默认扩展名

  3. $config['extension'] = ".twig";

  4. // 默认模版路径

  5. $config['template_dir'] = APPPATH . "views/";

  6. // 缓存目录

  7. $config['cache_dir'] = APPPATH . "cache/twig/";

  8. // 是否开启调试模式

  9. $config['debug'] = false;

  10. // 自动刷新

  11. $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

10-05 13:08