Exemple #1
0
 /**
  * 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);
     });
 }
Exemple #2
0
<?php

$lib = new Library();
$lib->tag('badtag', function ($parser, $token) {
    throw new RuntimeError('I am a bad tag');
});
$lib->simpleTag('badsimpletag', function () {
    throw new RuntimeError('I am a bad simpletag');
});
return $lib;
Exemple #3
0
$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'));
    // This check is kept for backwards-compatibility. See #3100.
    $endblock = $parser->nextToken();
    $acceptable_endblocks = array('endblock', 'endblock ' . $block_name);
    if (!in_array($endblock->contents, $acceptable_endblocks)) {
        $parser->invalidBlockTag($endblock, 'endblock', $acceptable_endblocks);
    }
    return new BlockNode($block_name, $nodelist);
});
$lib->tag('extends', function ($parser, $token) {
Exemple #4
0
        foreach ($this->vary_on as $var) {
            $v_ = new Variable($var);
            $vs_[] = urlencode($v_->resolve($context));
        }
        $args = join(':', $vs_);
        unset($vs_);
        $cache_key = 'template.cache.' . $this->fragment_name . '.' . md5($args);
        $manager = Dja::getCacheManager();
        $value = $manager->get($cache_key);
        if ($value === null) {
            $value = $this->nodelist->render($context);
            $manager->set($cache_key, $value, $expire_time);
        }
        return $value;
    }
}
$lib = new Library();
$lib->tag('cache', function ($parser, $token) {
    /**
     * @var Parser $parser
     * @var Token $token
     */
    $nodelist = $parser->parse(array('endcache'));
    $parser->deleteFirstToken();
    $tokens = py_str_split($token->contents);
    if (count($tokens) < 3) {
        throw new TemplateSyntaxError('\'cache\' tag requires at least 2 arguments.');
    }
    return new CacheNode($nodelist, $tokens[1], $tokens[2], py_slice($tokens, 3));
});
return $lib;
Exemple #5
0
$lib->tag('url', function ($parser, $token) {
    /**
     * @var Parser $parser
     * @var Token $token
     */
    $bits = $token->splitContents();
    if (count($bits) < 2) {
        throw new TemplateSyntaxError('\'url\' takes at least one argument (path to a view)');
    }
    $viewname = $parser->compileFilter($bits[1]);
    $args = array();
    $kwargs = array();
    $asvar = null;
    $bits = py_slice($bits, 2);
    if (count($bits) >= 2 && py_arr_get($bits, -2) == 'as') {
        $asvar = py_arr_get($bits, -1);
        $bits = py_slice($bits, null, -2);
    }
    if (count($bits)) {
        foreach ($bits as $bit) {
            $match = py_re_match(DjaBase::$re_kwarg, $bit);
            if (!$match) {
                throw new TemplateSyntaxError('Malformed arguments to url tag');
            }
            list($name, $value) = $match->groups();
            if ($name) {
                $kwargs[$name] = $parser->compileFilter($value);
            } else {
                $args[] = $parser->compileFilter($value);
            }
        }
    }
    return new URLNode($viewname, $args, $kwargs, $asvar, False);
});
Exemple #6
0
        }
        $context->update($values);
        $output = $this->nodelist->render($context);
        $context->pop();
        return $output;
    }
}
$lib->tag('autoescape', function ($parser, $token) {
    /**
     * @var Parser $parser
     * @var Token $token
     */
    $args = py_str_split($token->contents);
    if (count($args) != 2) {
        throw new TemplateSyntaxError('\'autoescape\' tag requires exactly one argument.');
    }
    $arg = $args[1];
    if (!in_array($arg, array('on', 'off'))) {
        throw new TemplateSyntaxError('\'autoescape\' argument should be \'on\' or \'off\'');
    }
    $nodelist = $parser->parse(array('endautoescape'));
    $parser->deleteFirstToken();
    return new AutoEscapeControlNode($arg == 'on', $nodelist);
});
$lib->tag('comment', function ($parser, $token) {
    /**
     * @var Parser $parser
     * @var Token $token
     */
    $parser->skipPast('endcomment');
    return new CommentNode();
Exemple #7
0
    public function __construct($contents)
    {
        $this->contents = $contents;
    }
    public function render($context)
    {
        return join(' ', $this->contents);
    }
}
function do_echo($token)
{
    return new EchoNode(py_slice(explode(' ', $token->contents), 1));
}
$lib = new Library();
$lib->tag('echo', function ($parser, $token) {
    return do_echo($token);
});
$lib->tag('other_echo', function ($parser, $token) {
    return do_echo($token);
});
$lib->filter('upper', function ($value) {
    return strtoupper($value);
});
DjaBase::$libraries['testtags'] = $lib;
/*
 * Helper objects for template tests
 */
class SomeException extends Exception
{
    public $silent_variable_failure = True;
}