twig.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. /**
  3. * Fuel
  4. *
  5. * Fuel is a fast, lightweight, community driven PHP5 framework.
  6. *
  7. * @package Fuel
  8. * @version 1.5
  9. * @author Fuel Development Team
  10. * @license MIT License
  11. * @copyright 2010 - 2013 Fuel Development Team
  12. * @link http://fuelphp.com
  13. */
  14. namespace Parser;
  15. use Twig_Autoloader;
  16. use Twig_Environment;
  17. use Twig_Loader_Filesystem;
  18. use Twig_Lexer;
  19. class View_Twig extends \View
  20. {
  21. protected static $_parser;
  22. protected static $_parser_loader;
  23. protected static $_twig_lexer_conf;
  24. public static function _init()
  25. {
  26. parent::_init();
  27. Twig_Autoloader::register();
  28. }
  29. protected function process_file($file_override = false)
  30. {
  31. $file = $file_override ?: $this->file_name;
  32. $local_data = $this->get_data('local');
  33. $global_data = $this->get_data('global');
  34. // Extract View name/extension (ex. "template.twig")
  35. $view_name = pathinfo($file, PATHINFO_BASENAME);
  36. // Twig Loader
  37. $views_paths = \Config::get('parser.View_Twig.views_paths', array(APPPATH . 'views'));
  38. array_unshift($views_paths, pathinfo($file, PATHINFO_DIRNAME));
  39. static::$_parser_loader = new Twig_Loader_Filesystem($views_paths);
  40. if ( ! empty($global_data))
  41. {
  42. foreach ($global_data as $key => $value)
  43. {
  44. static::parser()->addGlobal($key, $value);
  45. }
  46. }
  47. else
  48. {
  49. // Init the parser if you have no global data
  50. static::parser();
  51. }
  52. $twig_lexer = new Twig_Lexer(static::$_parser, static::$_twig_lexer_conf);
  53. static::$_parser->setLexer($twig_lexer);
  54. try
  55. {
  56. return static::parser()->loadTemplate($view_name)->render($local_data);
  57. }
  58. catch (\Exception $e)
  59. {
  60. // Delete the output buffer & re-throw the exception
  61. ob_end_clean();
  62. throw $e;
  63. }
  64. }
  65. public $extension = 'twig';
  66. /**
  67. * Returns the Parser lib object
  68. *
  69. * @return Twig_Environment
  70. */
  71. public static function parser()
  72. {
  73. if ( ! empty(static::$_parser))
  74. {
  75. static::$_parser->setLoader(static::$_parser_loader);
  76. return static::$_parser;
  77. }
  78. // Twig Environment
  79. $twig_env_conf = \Config::get('parser.View_Twig.environment', array('optimizer' => -1));
  80. static::$_parser = new Twig_Environment(static::$_parser_loader, $twig_env_conf);
  81. foreach (\Config::get('parser.View_Twig.extensions') as $ext)
  82. {
  83. static::$_parser->addExtension(new $ext());
  84. }
  85. // Twig Lexer
  86. static::$_twig_lexer_conf = \Config::get('parser.View_Twig.delimiters', null);
  87. if (isset(static::$_twig_lexer_conf))
  88. {
  89. isset(static::$_twig_lexer_conf['tag_block'])
  90. and static::$_twig_lexer_conf['tag_block'] = array_values(static::$_twig_lexer_conf['tag_block']);
  91. isset(static::$_twig_lexer_conf['tag_comment'])
  92. and static::$_twig_lexer_conf['tag_comment'] = array_values(static::$_twig_lexer_conf['tag_comment']);
  93. isset(static::$_twig_lexer_conf['tag_variable'])
  94. and static::$_twig_lexer_conf['tag_variable'] = array_values(static::$_twig_lexer_conf['tag_variable']);
  95. }
  96. return static::$_parser;
  97. }
  98. }
  99. // end of file twig.php