1 | <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); |
---|
2 | |
---|
3 | /** |
---|
4 | * @name Smarty |
---|
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_smarty extends CI_Driver { |
---|
14 | |
---|
15 | protected $ci; |
---|
16 | |
---|
17 | protected $_smarty; |
---|
18 | |
---|
19 | public function __construct() |
---|
20 | { |
---|
21 | $this->ci = get_instance(); |
---|
22 | $this->ci->config->load('plentyparser'); |
---|
23 | |
---|
24 | // Require Smarty |
---|
25 | require_once APPPATH."third_party/Smarty/Smarty.class.php"; |
---|
26 | |
---|
27 | // Store the Smarty library |
---|
28 | $this->_smarty = new Smarty; |
---|
29 | |
---|
30 | // Smarty config options |
---|
31 | $this->_smarty->setTemplateDir(config_item('parser.smarty.location')); |
---|
32 | $this->_smarty->setCompileDir(config_item('parser.smarty.compile_dir')); |
---|
33 | $this->_smarty->setCacheDir(config_item('parser.smarty.cache_dir')); |
---|
34 | $this->_smarty->setConfigDir(config_item('parser.smarty.config_dir')); |
---|
35 | |
---|
36 | // Add helper directories as plugin directories |
---|
37 | $this->_smarty->addPluginsDir(FCPATH . 'system/helpers/'); |
---|
38 | $this->_smarty->addPluginsDir(APPPATH . 'helpers/'); |
---|
39 | |
---|
40 | // Delimiters |
---|
41 | $this->_smarty->left_delimiter = config_item("parser.smarty.left.delim"); |
---|
42 | $this->_smarty->right_delimiter = config_item("parser.smarty.right.delim"); |
---|
43 | |
---|
44 | // Cache life time in seconds |
---|
45 | $this->_smarty->cache_lifetime = config_item('parser.smarty.cache_lifetime'); |
---|
46 | |
---|
47 | // Should let us access Codeigniter stuff in views |
---|
48 | $this->assign_var("CI", $this->ci); |
---|
49 | |
---|
50 | // Codeigniter base constants as variables. |
---|
51 | $this->assign_var('APPPATH',APPPATH); |
---|
52 | $this->assign_var('BASEPATH',BASEPATH); |
---|
53 | $this->assign_var('FCPATH',FCPATH); |
---|
54 | |
---|
55 | // Disable Smarty security policy |
---|
56 | $this->_smarty->disableSecurity(); |
---|
57 | |
---|
58 | // Turn on/off debug |
---|
59 | $this->_smarty->debugging = config_item('parser.smarty.debug'); |
---|
60 | } |
---|
61 | |
---|
62 | /** |
---|
63 | * Call |
---|
64 | * able to call native Smarty methods |
---|
65 | * @returns void |
---|
66 | */ |
---|
67 | public function __call($method, $params=array()) |
---|
68 | { |
---|
69 | |
---|
70 | if(!method_exists($this, $method)) |
---|
71 | { |
---|
72 | call_user_func_array(array($this->_smarty, $method), $params); |
---|
73 | |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * Assign Var |
---|
79 | * Assign a variable for template view use |
---|
80 | * |
---|
81 | * @param mixed $name |
---|
82 | * @param mixed $val |
---|
83 | * @returns void |
---|
84 | */ |
---|
85 | public function assign_var($name, $val) |
---|
86 | { |
---|
87 | // If an empty variable name |
---|
88 | if (empty($name)) |
---|
89 | { |
---|
90 | show_error('Smarty assign var function expects a name and value for assigning variables'); |
---|
91 | } |
---|
92 | |
---|
93 | // Call Smarty assign function |
---|
94 | $this->_smarty->assign($name, $val); |
---|
95 | } |
---|
96 | |
---|
97 | /** |
---|
98 | * Parse |
---|
99 | * Display or return template contents |
---|
100 | * |
---|
101 | * @param mixed $template |
---|
102 | * @param array $data |
---|
103 | * @param mixed $return |
---|
104 | */ |
---|
105 | public function parse($template, $data = array(), $return = false) |
---|
106 | { |
---|
107 | // If we have variables to assign, lets assign them |
---|
108 | if ($data) |
---|
109 | { |
---|
110 | foreach ($data AS $key => $val) |
---|
111 | { |
---|
112 | $this->_smarty->assign($key, $val); |
---|
113 | } |
---|
114 | } |
---|
115 | |
---|
116 | // If we're returning the template contents |
---|
117 | if ($return === true) |
---|
118 | { |
---|
119 | return $this->_smarty->fetch($template); |
---|
120 | } |
---|
121 | else |
---|
122 | { |
---|
123 | $this->_smarty->display($template); |
---|
124 | } |
---|
125 | } |
---|
126 | |
---|
127 | /** |
---|
128 | * Parse string |
---|
129 | * Parses a string as a template and can be returned or displayed |
---|
130 | * |
---|
131 | * @param mixed $string |
---|
132 | * @param mixed $data |
---|
133 | */ |
---|
134 | public function parse_string($string, $data = array()) |
---|
135 | { |
---|
136 | return $this->_smarty->fetch('string:'.$string, $data); |
---|
137 | } |
---|
138 | |
---|
139 | |
---|
140 | /** |
---|
141 | * Registers plugin to be used in templates |
---|
142 | * |
---|
143 | * @param string $type plugin type |
---|
144 | * @param string $tag name of template tag |
---|
145 | * @param callback $callback PHP callback to register |
---|
146 | * @param boolean $cacheable if true (default) this fuction is cachable |
---|
147 | * @param array $cache_attr caching attributes if any |
---|
148 | * @throws SmartyException when the plugin tag is invalid |
---|
149 | */ |
---|
150 | public function register_plugin($type, $tag, $callback, $cacheable = true, $cache_attr = null) |
---|
151 | { |
---|
152 | return $this->_smarty->registerPlugin($type, $tag, $callback, $cacheable, $cache_attr); |
---|
153 | } |
---|
154 | |
---|
155 | } |
---|