├── .hgtags ├── README.markdown ├── config └── parser.php ├── controllers └── dwoo_test.php ├── libraries └── MY_Parser.php └── views └── test.php /.hgtags: -------------------------------------------------------------------------------- 1 | a38a6a663766559ff9f89c23de490e4ea4fec776 v2.0 2 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | CodeIgniter-Dwoo 2 | ================ 3 | 4 | Version: 2.0 5 | 6 | Dwoo is a PHP based templating engine aimed as a replacement for Smarty 2.x using similar and alternative syntax. 7 | CodeIgniter-Dwoo is a simple implementation for using Dwoo in your CodeIgniter view files. 8 | 9 | 10 | Requirements 11 | ------------ 12 | 13 | 1. PHP 5.2+ 14 | 2. CodeIgniter 1.6.x - 2.0-dev 15 | 3. Dwoo 1.2-dev (grab a copy from [GitHub](http://github.com/Seldaek/Dwoo/archives/master)) 16 | 17 | Requirements for Dwoo alone are: 18 | 19 | * SPL and PCRE extensions (for php versions prior to 5.3.0) 20 | * mbstring extension for some string manipulation plugins (especially if you intend to use UTF-8) 21 | 22 | 23 | Installation 24 | ------------ 25 | 26 | 1. [Download Dwoo](http://github.com/Seldaek/Dwoo/archives/master) and copy the main /dwoo/ folder into your application/libraries/ folder. 27 | 28 | 2. Copy libraries/MY_Parser.php to your application/libraries/ folder. 29 | 30 | 3. Copy config/parser.php to your application/config/ folder. 31 | 32 | 4. Create the folders: 33 | 34 | application/cache 35 | application/cache/dwoo 36 | application/cache/dwoo/compiled 37 | 38 | 5. Set the new folders file permissions to be write-able: 39 | 40 | chmod -R 777 application/cache 41 | 42 | 43 | Usage 44 | ----- 45 | 46 | $this->load->library('parser'); 47 | 48 | $this->parser->parse('dwoo_test', array('message' => 'OH HAI!')); -------------------------------------------------------------------------------- /config/parser.php: -------------------------------------------------------------------------------- 1 | load->library('parser'); 9 | } 10 | 11 | function index() 12 | { 13 | 14 | $this->parser->parse('test', array('message' => 'Hello World!')); 15 | 16 | } 17 | } 18 | 19 | ?> -------------------------------------------------------------------------------- /libraries/MY_Parser.php: -------------------------------------------------------------------------------- 1 | initialize($config); 29 | } 30 | 31 | $this->_ci = & get_instance(); 32 | $this->_dwoo = self::spawn(); 33 | } 34 | 35 | // -------------------------------------------------------------------- 36 | 37 | /** 38 | * Initialize preferences 39 | * 40 | * @access public 41 | * @param array 42 | * @return void 43 | */ 44 | function initialize($config = array()) 45 | { 46 | foreach ($config as $key => $val) 47 | { 48 | $this->{'_' . $key} = $val; 49 | } 50 | } 51 | 52 | // -------------------------------------------------------------------- 53 | 54 | function spawn() 55 | { 56 | // Main Dwoo object 57 | $dwoo = new Dwoo; 58 | 59 | // The directory where compiled templates are located 60 | $dwoo->setCompileDir($this->_parser_compile_dir); 61 | $dwoo->setCacheDir($this->_parser_cache_dir); 62 | $dwoo->setCacheTime($this->_parser_cache_time); 63 | 64 | // Security 65 | $security = new MY_Security_Policy; 66 | 67 | $security->setPhpHandling($this->_parser_allow_php_tags); 68 | $security->allowPhpFunction($this->_parser_allowed_php_functions); 69 | 70 | $dwoo->setSecurityPolicy($security); 71 | 72 | return $dwoo; 73 | } 74 | 75 | // -------------------------------------------------------------------- 76 | 77 | /** 78 | * Parse a view file 79 | * 80 | * Parses pseudo-variables contained in the specified template, 81 | * replacing them with the data in the second param 82 | * 83 | * @access public 84 | * @param string 85 | * @param array 86 | * @param bool 87 | * @return string 88 | */ 89 | function parse($template, $data = array(), $return = FALSE, $is_include = FALSE) 90 | { 91 | $string = $this->_ci->load->view($template, $data, TRUE); 92 | 93 | return $this->_parse($string, $data, $return, $is_include); 94 | } 95 | 96 | // -------------------------------------------------------------------- 97 | 98 | /** 99 | * String parse 100 | * 101 | * Parses pseudo-variables contained in the string content, 102 | * replacing them with the data in the second param 103 | * 104 | * @access public 105 | * @param string 106 | * @param array 107 | * @param bool 108 | * @return string 109 | */ 110 | function string_parse($string, $data = array(), $return = FALSE, $is_include = FALSE) 111 | { 112 | return $this->_parse($string, $data, $return, $is_include); 113 | } 114 | 115 | function parse_string($string, $data = array(), $return = FALSE, $is_include = FALSE) 116 | { 117 | return $this->_parse($string, $data, $return, $is_include); 118 | } 119 | 120 | // -------------------------------------------------------------------- 121 | 122 | /** 123 | * Parse 124 | * 125 | * Parses pseudo-variables contained in the specified template, 126 | * replacing them with the data in the second param 127 | * 128 | * @access public 129 | * @param string 130 | * @param array 131 | * @param bool 132 | * @return string 133 | */ 134 | function _parse($string, $data, $return = FALSE, $is_include = FALSE) 135 | { 136 | // Start benchmark 137 | $this->_ci->benchmark->mark('dwoo_parse_start'); 138 | 139 | // Convert from object to array 140 | if ( ! is_array($data)) 141 | { 142 | $data = (array) $data; 143 | } 144 | 145 | $data = array_merge($data, $this->_ci->load->get_vars()); 146 | 147 | foreach ($this->_parser_assign_refs as $ref) 148 | { 149 | $data[$ref] = & $this->_ci->{$ref}; 150 | } 151 | 152 | // -------------------------------------------------------------------- 153 | // Convert elapsed time and memory usage for dwoo compatibility 154 | 155 | if (CI_VERSION < 2) 156 | { 157 | $string = str_replace(array('{elapsed_time}', '{memory_usage}'), array('[elapsed_time]', '[memory_usage]'), $string); 158 | } 159 | 160 | // -------------------------------------------------------------------- 161 | // Object containing data 162 | $dwoo_data = new Dwoo_Data; 163 | $dwoo_data->setData($data); 164 | 165 | try 166 | { 167 | // Object of the template 168 | $tpl = new Dwoo_Template_String($string); 169 | 170 | $dwoo = $is_include ? self::spawn() : $this->_dwoo; 171 | 172 | // render the template 173 | $parsed_string = $dwoo->get($tpl, $dwoo_data); 174 | } 175 | catch (Exception $e) 176 | { 177 | show_error($e); 178 | } 179 | 180 | // -------------------------------------------------------------------- 181 | // Parse out the elapsed time and memory usage, 182 | // then swap the pseudo-variables with the data 183 | 184 | $elapsed = $this->_ci->benchmark->elapsed_time('total_execution_time_start', 'total_execution_time_end'); 185 | 186 | if (CI_VERSION < 2) 187 | { 188 | $memory = ( ! function_exists('memory_get_usage')) ? '0' : round(memory_get_usage() / 1024 / 1024, 2) . 'MB'; 189 | 190 | $string = str_replace(array('[elapsed_time]', '[memory_usage]'), array($elapsed, $memory), $string); 191 | } 192 | 193 | // Finish benchmark 194 | $this->_ci->benchmark->mark('dwoo_parse_end'); 195 | 196 | // Return results or not ? 197 | if ( ! $return) 198 | { 199 | $this->_ci->output->append_output($parsed_string); 200 | return; 201 | } 202 | 203 | return $parsed_string; 204 | } 205 | 206 | // -------------------------------------------------------------------- 207 | } 208 | 209 | /** 210 | *class MY_Security_Policy extends Dwoo_Security_Policy { 211 | * 212 | * public function callMethod(Dwoo_Core $dwoo, $obj, $method, $args) 213 | * { 214 | * return call_user_func_array(array($obj, $method), $args); 215 | * } 216 | * 217 | * public function isMethodAllowed() 218 | * { 219 | * return TRUE; 220 | * } 221 | *} 222 | **/ 223 | 224 | // END MY_Parser Class 225 | 226 | /* End of file MY_Parser.php */ 227 | /* Location: ./application/libraries/MY_Parser.php */ -------------------------------------------------------------------------------- /views/test.php: -------------------------------------------------------------------------------- 1 | 2 |
3 |