如何访问由变量确定的Twig中的成员?

Rob*_*tin 11 php symfony twig

我想做以下代码:

{% set rooms = [] %}
{% set opts = {
    'hasStudio': 'Studio',
    'has1Bed': '1 BR',
    'has2Bed': '2 BR',
    'has3Bed': '3 BR',
    'has4BedPlus': '4 BR+'
}
%}
{% for key, val in opts %}
    {% if bldg.{key} is none %} {# PROBLEM HERE.. HOW TO FIND THIS MEMBER!? #}
      {{ val }}?
    {% elseif bldg.{key} %}
      {{ val }}
    {% else %}
      No {{ val }}
    {% endif %}
{% endfor %}
Run Code Online (Sandbox Code Playgroud)

如何调用由值key?命名的bldg的成员属性?我想得到的价值

 bldg.hasStudio
 bldg.has1Bed
 bldg.has2Bed
 etc....
Run Code Online (Sandbox Code Playgroud)

Raf*_*ael 22

简短回答:不是直接/本地可能......但是.

显然他们在Twig 1.2中添加了一个名为attribute()的新函数,它正好满足了这个需求.

但到目前为止,你只能下载Twig 1.1.2; 所以1.2可能没有附带SF2 - 虽然我找不到版本号.(1.2现已上市!)

我尝试用不同的技巧解决这个问题,但无济于事; 1.2将修复它.

版本1.2中的新功能:在Twig 1.2中添加了属性功能.

属性可用于访问变量的"动态"属性:

{{ attribute(object, method) }}

{{ attribute(object, method,arguments) }}

{{ attribute(array, item) }}


你可以做的是为你的班级添加一个方法来照顾你需要的任何东西.类似的东西:

php:

class C
{
    public $a = 1;
    public $b = 2;

    public function getValueForKey($k)
    {
        return $this->$k;
    }
}

[ providing an instance of C to the template as 'obj' ]
Run Code Online (Sandbox Code Playgroud)

树枝:

{% set x = "a" %}
{{ obj.getValueForKey(x) }}
Run Code Online (Sandbox Code Playgroud)

将输出'1'


Rob*_*tin 3

我编写了自己的树枝扩展来执行此操作。你会按照我想要的方式使用它:

{% set keyVariable = 'propertyName' %}
{{ obj.access(keyVariable) }}
{# the above prints $obj->propertyName #}
Run Code Online (Sandbox Code Playgroud)

就这个:

// filename: Acme/MainBundle/Extension/AccessTwigExtension.php
namespace Acme\MainBundle\Extension;

class AccessTwigExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'access' => new \Twig_Filter_Method($this, 'accessFilter'),
        );
    }

    public function getName()
    {
        return 'access_twig_extension';
    }

    // Description:
    // Dynamically retrieve the $key of the $obj, in the same order as
    // $obj.$key would have done.
    // Reference:
    // http://twig.sensiolabs.org/doc/templates.html
    public function accessFilter($obj, $key)
    {
        if (is_array($obj)) {
            if (array_key_exists($key, $obj)) {
                return $obj[$key];
            }
        } elseif (is_object($obj)) {
            $reflect = new \ReflectionClass($obj);
            if (property_exists($obj, $key) && $reflect->getProperty($key)->isPublic()) {
                return $obj->$key;
            }
            if (method_exists($obj, $key) && $reflect->getMethod($key)->isPublic()) {
                return $obj->$key();
            }
            $newKey = 'get' . ucfirst($key);
            if (method_exists($obj, $newKey) && $reflect->getMethod($newKey)->isPublic()) {
                return $obj->$newKey();
            }
            $newKey = 'is' . ucfirst($key);
            if (method_exists($obj, $newKey) && $reflect->getMethod($newKey)->isPublic()) {
                return $obj->$newKey();
            }
        }
        return null;
    }
}
Run Code Online (Sandbox Code Playgroud)

要在我的程序中使用它,我还必须在依赖项注入中添加几行:

//filename: Acme/MainBundle/DependencyInjection/AcmeMainInjection.php
// other stuff is here....
public function load(array $configs, ContainerBuilder $container)
{
    // other stuff here...
    $definition = new Definition('Lad\MainBundle\Extension\AccessTwigExtension');
    $definition->addTag('twig.extension');
    $container->setDefinition('access_twig_extension', $definition);
    // other stuff here...
Run Code Online (Sandbox Code Playgroud)