/** * Parse the template * @param blob $source template source code * @param object $blocks `BlockSet` object * @return [type] [description] */ public static function parse($blocks, $data, $options) { $options += array('type' => 'html'); $cache = new Cache(); static::$_terminals = array_flip(Lexer::terminals()); static::$_blocks = $blocks; static::$_cacheFile = substr(str_ireplace('/', '_', static::$_blocks->templates(0)), 1); $key = static::$_cacheFile . $options['type']; $_pattern = "/{:(block) \"({:block})\"(?: \\[(.+)\\])?}(.*){\\1:}/msU"; static::$_templates = array_reverse($blocks->templates()); if ($cache->file(sha1($key))) { return sha1($key); } else { $i = 0; while ($i < ($count = count(static::$_templates))) { if ($i == $count) { break; } $source = static::_read(static::$_templates[$i]); $template = static::_replace($source, $i, $options); $i++; } } // final parent template handler return static::$_template; }
/** * Run the Lexer and gather the block objects * @param blob $source template contents * @param string $template path to template * @return object|string BlockSet object or path to cached template */ public static function run($template, $data = array(), array $options = array()) { /** * Lets check for cache, if exists then we'll return the cache file name * @var Cache */ $options += array('type' => 'html'); $_cache = new Cache(); $file = static::_template($template); $_cacheFile = sha1($file . $options['type']); if ($_isCached = $_cache->file($_cacheFile) and $_cache->cache()) { return $_isCached; } /** * Guess there was no cache file, lets lex this mother. */ $source = self::_read($template); $_blockSet = static::$_blockSet; $template = self::_template($template); // Set the final template, this gets reset on every iteration to the current template // landing, finally, on the last. static::$_blockSet->templates($template); foreach (static::$_terminals as $pattern => $terminal) { // attempt to match block/parent regex $_preg = $terminal == "T_BLOCK" ? preg_match_all($pattern, $source, $matches) : preg_match($pattern, $source, $matches); if ($_preg) { // Blocks, load them in the BlockSet if ($terminal == "T_BLOCK") { foreach ($matches[2] as $index => $name) { /** * Block parameters * Options that can be set to modify how the block is rendered * @var [type] */ $params = explode(',', $matches[3][$index]); $params = array_map(function ($param) { return trim($param); }, $params); static::$_blockSet->push($name, $matches[4][$index], $template, $params); } // Parent templates, read these and throw them back to the lexer. } else { if (!empty(static::$_hierarchy)) { list($type, $file) = array_slice($matches, 2); if (empty($type)) { $type = 'template'; } else { $type = trim($type); } // override the controller if more than simple file name is passed in. First directory becomes controller $file = explode('/', ltrim($file, '/')); if (count($file) > 1) { $options['controller'] = array_shift($file); } // set filename to template/layout $file = implode('/', $file); preg_match('/(' . implode('|', Media::types()) . ')\\.php$/', $file, $extensions); if (count($extensions) == 2) { // remove .{:type}.php $file = substr($file, 0, strlen($file) - strlen($extensions[0]) - 1); // set type to render as extension specified $options['type'] = $extensions[1]; } $options[$type] = $file; // template path from \lithium\template\view\adapter\File $_template = static::$_hierarchy->template($type, $options); // if $_hierarchy not passed in, use template as is } else { $_template = $matches[3]; } static::run($_template, $data, $options); } } } return static::$_blockSet; }