class Role extends Twig_Extension
{
public function getTokenParsers()
{
return array(
new HasRoleParser(),new EndHasRoleParser()
);
}
}
And created the two sub class :
class HasRoleParser extends \Twig_TokenParser
{
public function parse(Twig_Token $token)
{
$parser = $this->parser;
$stream = $parser->getStream();
$role = $stream->expect(Twig_Token::NAME_TYPE)->getValue();
$guard = null;
return "<?php if(auth({$guard})->check() && auth({$guard})->user()->hasRole({$role})): ?>";
}
public function getTag()
{
return 'hasrole';
}
}
and
class EndHasRoleParser extends \Twig_TokenParser
{
public function parse(Twig_Token $token)
{
return '<?php endif; ?>';
}
public function getTag()
{
return 'endhasrole';
}
}
However, i end up with this answer "Lexer or parser ended up in unsupported state".
Should i define the tag in another extensions , is there a thing i missed in the twigbridge for configuring new tags, or do you see any errors in the code ?
Hello,
I m currently trying to add custom tag to one project. My goal is to reach something more look like : https://github.com/spatie/laravel-permission/blob/c3c98a97e037950fe7a5dd1f1362626a4d95ea37/src/PermissionServiceProvider.php
So in my twig view , i ve add :
I ve create a new class
And created the two sub class :
and
and in my twigbridge file, i set it up like this
However, i end up with this answer "Lexer or parser ended up in unsupported state".
Should i define the tag in another extensions , is there a thing i missed in the twigbridge for configuring new tags, or do you see any errors in the code ?
Thanks a lot !