duannian7116 2015-03-19 02:47
浏览 104

如何在Twig扩展中的Token Parser中获取变量

I'm having trouble getting the correct data passed in my twig extension

Example token parser parse function:

public function parse(\Twig_Token $token)
{
    $parser = $this->parser;
    $stream = $parser->getStream();

    $firstVariable = $stream->getCurrent();    
}

The expression {% myTag "someString" %}will set $firstVariable as 'someString'.

However if we put in place a variable:

{% set someVariable = "someString" %}
{% myTag someVariable %}

You end up with a name type with the value 'someVariable' instead of the expected variable value 'someString'

How do I properly parse the first argument of my custom twig tag, allowing the input to either be a string or a variable containing a string?

Edit

It looks like you can just take the variable name and use it in the returned twig node.

public function compile(){
    $compiler->write('echo "$context[$variableName]"');
}

But I need access to it before compiling.

Edit I no longer believe it's possible to access the context before compiling. Making this question a dud unless anyone has some more information to share on the subject

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 stm32开发clion时遇到的编译问题
    • ¥15 lna设计 源简并电感型共源放大器
    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程