1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
---|
2 | |
---|
3 | /** |
---|
4 | * @name Twig |
---|
5 | * @package Plenty Parser |
---|
6 | * @subpackage Driver |
---|
7 | * @copyright 2011 |
---|
8 | * @author Dwayne Charrington |
---|
9 | * @license http://philsturgeon.co.uk/code/dbad-license |
---|
10 | * @version 1.0 |
---|
11 | */ |
---|
12 | |
---|
13 | class Pp_twig extends CI_Driver { |
---|
14 | |
---|
15 | protected $ci; |
---|
16 | |
---|
17 | protected $_twig; |
---|
18 | |
---|
19 | protected $_template; |
---|
20 | |
---|
21 | protected $_template_dir; |
---|
22 | protected $_cache_dir; |
---|
23 | protected $_debug; |
---|
24 | |
---|
25 | public function __construct() |
---|
26 | { |
---|
27 | $this->ci = get_instance(); |
---|
28 | |
---|
29 | ini_set('include_path', |
---|
30 | ini_get('include_path') . PATH_SEPARATOR . APPPATH . 'third_party/Twig'); |
---|
31 | |
---|
32 | require_once (string) "Autoloader" . EXT; |
---|
33 | |
---|
34 | Twig_Autoloader::register(); |
---|
35 | |
---|
36 | $loader = new Twig_Loader_Filesystem($this->_template_dir); |
---|
37 | |
---|
38 | $this->_twig = new Twig_Environment($loader, array( |
---|
39 | 'cache' => $this->_cache_dir, |
---|
40 | 'debug' => $this->_debug, |
---|
41 | )); |
---|
42 | |
---|
43 | // Check if a theme has been set and if there is, check it exists and add it to the path |
---|
44 | |
---|
45 | } |
---|
46 | |
---|
47 | /** |
---|
48 | * Override the default template location |
---|
49 | * |
---|
50 | * @param mixed $location |
---|
51 | * @returns void |
---|
52 | */ |
---|
53 | public function set_location($location) |
---|
54 | { |
---|
55 | $this->_template_dir = $location; |
---|
56 | } |
---|
57 | |
---|
58 | /** |
---|
59 | * Assign Var |
---|
60 | * Assign a variable for template view use |
---|
61 | * |
---|
62 | * @param mixed $name |
---|
63 | * @param mixed $val |
---|
64 | * @returns void |
---|
65 | */ |
---|
66 | public function assign_var($name, $val) |
---|
67 | { |
---|
68 | // If an empty variable name |
---|
69 | if (empty($name)) |
---|
70 | { |
---|
71 | show_error('Smarty assign var function expects a name and value for assigning variables'); |
---|
72 | } |
---|
73 | |
---|
74 | // Call Smarty assign function |
---|
75 | $this->_smarty->assign($name, $val); |
---|
76 | } |
---|
77 | |
---|
78 | /** |
---|
79 | * Load the template and return the data |
---|
80 | * |
---|
81 | * @param mixed $template |
---|
82 | * @param mixed $data |
---|
83 | * @returns string |
---|
84 | */ |
---|
85 | public function parse($template, $data = array(), $return = false) |
---|
86 | { |
---|
87 | // If we do not have a template extension, use the default |
---|
88 | if (stripos($template, '.') === false) |
---|
89 | { |
---|
90 | $template . config_item('parser.twig.extension'); |
---|
91 | } |
---|
92 | |
---|
93 | // Load the template |
---|
94 | $template = $this->_twig->loadTemplate($template); |
---|
95 | |
---|
96 | // If data supplied is an array |
---|
97 | if ( is_array($data) ) |
---|
98 | { |
---|
99 | $data = array_merge($data, $this->ci->load->get_vars()); |
---|
100 | } |
---|
101 | |
---|
102 | if ( $return === true ) |
---|
103 | { |
---|
104 | return $template->render($data); |
---|
105 | } |
---|
106 | else |
---|
107 | { |
---|
108 | return $template->display($data); |
---|
109 | } |
---|
110 | } |
---|
111 | |
---|
112 | /** |
---|
113 | * Parse String |
---|
114 | * Parse a string and return it as a string or display it |
---|
115 | * |
---|
116 | * @param mixed $string |
---|
117 | * @param mixed $data |
---|
118 | * @param mixed $return |
---|
119 | * @returns void |
---|
120 | */ |
---|
121 | public function parse_string($string, $data = array(), $return = false) |
---|
122 | { |
---|
123 | $string = $this->_twig->loadTemplate($string); |
---|
124 | |
---|
125 | if ( is_array($data) ) |
---|
126 | { |
---|
127 | $data = array_merge($data, $this->ci->load->get_vars()); |
---|
128 | } |
---|
129 | |
---|
130 | if ($return === true) |
---|
131 | { |
---|
132 | return $string->render($data); |
---|
133 | } |
---|
134 | else |
---|
135 | { |
---|
136 | return $string->display($data); |
---|
137 | } |
---|
138 | |
---|
139 | } |
---|
140 | |
---|
141 | /** |
---|
142 | * Register Plugin |
---|
143 | * Registers a plugin for use in a Twig template. |
---|
144 | * @param $name |
---|
145 | */ |
---|
146 | public function register_plugin($name) |
---|
147 | { |
---|
148 | $this->_twig->addFunction($name, new Twig_Function_Function($name)); |
---|
149 | } |
---|
150 | |
---|
151 | } |
---|