本文整理汇总了PHP中Library::tag方法的典型用法代码示例。如果您正苦于以下问题:PHP Library::tag方法的具体用法?PHP Library::tag怎么用?PHP Library::tag使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Library
的用法示例。
在下文中一共展示了Library::tag方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: registerAsTag
/**
* 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);
});
}
示例2: Library
<?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;
示例3: Library
$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) {
示例4: foreach
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;
示例5: function
$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);
});
示例6: function
}
$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();
示例7: __construct
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;
}