/** * Registers tag within a library handled * by this naive if template node. * * @static * @param Library $lib * @param string $tag_name * @param Closure $condition */ public static function registerAsTag($lib, $tag_name, $condition) { $class = get_called_class(); $lib->tag($tag_name, function ($parser, $token) use($tag_name, $condition, $class) { /** * @var Parser $parser * @var Token $token */ $bits = py_str_split($token->contents); $nodelist_true = $parser->parse(array('else', 'end' . $tag_name)); $token = $parser->nextToken(); if ($token->contents == 'else') { $nodelist_false = $parser->parse(array('end' . $tag_name)); $parser->deleteFirstToken(); } else { $nodelist_false = new NodeList(); } $values = array(); foreach (py_slice($bits, 1) as $bit) { $values[] = $parser->compileFilter($bit); } return new $class($nodelist_true, $nodelist_false, $values, $condition); }); }
return $this->renderTemplate($template, $context); } catch (Exception $e) { if (Dja::getSetting('TEMPLATE_DEBUG')) { throw $e; } return ''; } } } $lib = new Library(); $lib->tag('block', function ($parser, $token) { /** * @var Parser $parser * @var Token $token */ $bits = py_str_split($token->contents); if (count($bits) != 2) { throw new TemplateSyntaxError('\'block\' tag takes only one argument'); } $block_name = $bits[1]; // Keep track of the names of BlockNodes found in this template, so we can check for duplication. if (property_exists($parser, '__loaded_blocks')) { if (isset($parser->__loaded_blocks[$block_name])) { throw new TemplateSyntaxError('\'block\' tag with name \'' . $block_name . '\' appears more than once'); } $parser->__loaded_blocks[] = $block_name; } else { // parser.__loaded_blocks isn't a list yet $parser->__loaded_blocks = array($block_name); } $nodelist = $parser->parse(array('endblock'));
/** * Truncates a string after a certain number of words. * * Newlines in the string will be stripped. * * @param $length * @param $truncate * * @return string */ private function _text_words($length, $truncate) { $words = py_str_split($this->_wrapped); if (count($words) > $length) { $words = py_slice($words, null, $length); return self::add_truncation_text(join(' ', $words), $truncate); } return join(' ', $words); }
/** * @param null|array $parse_until * * @return NodeList * @throws TemplateSyntaxError */ public function parse($parse_until = null) { if ($parse_until === null) { $parse_until = array(); } $nodelist = $this->createNodelist(); while ($this->tokens) { $token = $this->nextToken(); // Use the raw values here for TOKEN_* for a tiny performance boost. if ($token->token_type == 0) { // TOKEN_TEXT $this->extendNodelist($nodelist, new TextNode($token->contents), $token); } elseif ($token->token_type == 1) { // TOKEN_VAR if (!$token->contents) { $this->emptyVariable($token); } $filter_expression = $this->compileFilter($token->contents); $var_node = $this->createVariableNode($filter_expression); $this->extendNodelist($nodelist, $var_node, $token); } elseif ($token->token_type == 2) { // TOKEN_BLOCK $command = null; $command_ = py_str_split($token->contents); if (isset($command_[0])) { $command = $command_[0]; } else { $this->emptyBlockTag($token); } unset($command_); if (in_array($command, $parse_until)) { // Put token back on token list so calling code knows why it terminated. $this->prependToken($token); return $nodelist; } // Execute callback function for this tag and append resulting node. $this->enterCommand($command, $token); if (isset($this->tags[$command])) { /** @var $compile_func Closure */ $compile_func = $this->tags[$command]; } else { $this->invalidBlockTag($token, $command, $parse_until); } try { /** @var $compiled_result Node */ $compiled_result = $compile_func($this, $token); } catch (TemplateSyntaxError $e) { if (!$this->compileFunctionError($token, $e)) { throw $e; } } $this->extendNodelist($nodelist, $compiled_result, $token); $this->exitCommand(); } } if ($parse_until) { $this->unclosedBlockTag($parse_until); } return $nodelist; }
return mark_safe(str_replace("\n", '<br />', $value)); }, array('is_safe' => True, 'needs_autoescape' => True)); //stringfilter $lib->filter('safe', function ($value) { return mark_safe($value); }, array('is_safe' => True)); $lib->filter('safeseq', function ($value) { $s_ = array(); foreach ($value as $i_) { $s_[] = mark_safe($i_); } return $s_; }, array('is_safe' => True)); //stringfilter $lib->filter('removetags', function ($value, $tags) { $tags_ = py_str_split($tags); $tags = array(); foreach ($tags_ as $tag) { $tags[] = preg_quote($tag); } $tags = $tags_; unset($tags_); $tags_re = '(' . join('|', $tags) . ')'; $starttag_re = '~<' . $tags_re . '(/?>|(\\s+[^>]*>))~u'; $endtag_re = '~</' . $tags_re . '>~'; $value = preg_replace($starttag_re, '', $value); $value = preg_replace($endtag_re, '', $value); return $value; }, array('is_safe' => True)); //stringfilter $lib->filter('striptags', function ($value) {