简体   繁体   English

无法在TWIG中访问stdClass的属性

[英]can't access property of stdClass in TWIG

I have googled around and it seems as though the creators of TWIG really insists that what I am doing here, which is to me a pure job for the VIEW, something that the template shouldn't take care of at all?! 我到处乱逛,似乎TWIG的创建者真的坚持我在这里所做的工作,这对VIEW来说是纯粹的工作,而模板根本就不需要照顾?

I know I can't iterate over an object of stdClass without some custom TWIg filters, so I hacked that for now, but if I can't eve access properties dynamically this TWIG thing really isn't very useful at all. 我知道没有一些自定义的TWIg过滤器就无法遍历stdClass的对象,所以我暂时对此进行了介绍,但是如果我不能动态地动态访问属性,那么TWIG的确根本不是很有用。

    $fixedModuleNames = array('time', 'date', 'weather'); //since TWIG doesn't iterate over objects by default, this is my solution, don't feel like adding a bunch of twigfilters just for this.
    $fixedModules = json_decode($entity->getFixedModules());

    /*
    Here's what fixedModules look like (although here not JSON but array, before encoded to json, I like to create my JSONs this way in PHP)
    $fixedModules["time"] = array(
        'show'          => true,
        'left'          => 10,
        'top'           => 10,
        'width'         => 100,
        'height'        => 200,
        'fontColor'     => '#000000',
        'fontSize'      => 40,
        'fontFamily'    => 'Arial',
        'borderColor'   => '',
        'borderRounding'=> 0,
        'bgColor'       => ''
    );
    */

Here's what I am trying to do... 这就是我想要做的...

                {% for item in fixedModuleNames %}
                <TR>
                    <TD><input type="number" id="left_{{ item }}" value="{{ fixedModules[item].left }}" class="LayoutModuleEditField" /></TD>

So this line fails 所以这条线失败了

{{ fixedModules[item].left }}

There must be a way around this since what I am doing is very routine? 因为我的工作很常规,所以必须有解决的办法?

嗯,这也许是首选的方式吗?

{{ attribute(fixedModules, item).left }}

If your attribute function works then use it. 如果您的属性函数起作用,请使用它。

Consider however fixedModules[item].left. 考虑一下fixedModules [item] .left。 You are asking twig to figure out that item is a variable while left is a constant. 您要让小树枝弄清楚项目是变量,而左边是常量。 Difficult for any system to do to say the least. 至少任何系统都很难做到。

I would use something like: 我会用类似的东西:

{% for moduleName, module in fixedModules %} {# Time, Date, Weather module #}
    {% for itemName,itemValue in module %} {# Process each attribute in the module #}
        ...

If you want to iterate over an object then just implement the array iterator interface. 如果要迭代对象,则只需实现数组迭代器接口。 Usually pretty simple. 通常很简单。

item is not the key, but an element of your array. item不是关键,而是数组的元素。 So you can access your attributes this way: 因此,您可以通过以下方式访问属性:

{% for item in fixedModuleNames %}
  left = {{ item.left }}
{% enfor %}

If you really want to use the key instead, do something like: 如果您确实想使用该密钥,请执行以下操作:

{% for key, item in fixedModuleNames %}
  left = {{ fixedModuleNames[key].left }}
{% enfor %}

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM