【问题标题】:How do I access a member in Twig determined by a variable?如何访问由变量确定的 Twig 中的成员?
【发布时间】:2011-09-11 14:46:18
【问题描述】:

我想做如下代码:

{% 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 %}

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

 bldg.hasStudio
 bldg.has1Bed
 bldg.has2Bed
 etc....

【问题讨论】:

    标签: php symfony twig


    【解决方案1】:

    简答:目前还没有直接/本地可能。

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

    但到目前为止,您只能下载 Twig 1.1.2;所以 SF2 可能没有附带 1.2 - 虽然我找不到版本号。 (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' ]
    

    树枝:

    {% set x = "a" %}
    {{ obj.getValueForKey(x) }}
    

    将输出'1'

    【讨论】:

    • 感谢您的信息。我对这个解决方案的问题是我不想让我的 Entity 类与 getter 混淆,因为 twig 需要它们......
    • twig 1.2 中的属性功能很棒
    【解决方案2】:

    使用括号语法:bldg[key]

    【讨论】:

      【解决方案3】:

      我写了自己的树枝扩展来做到这一点。你会以我想要的方式使用它:

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

      这里是:

      // 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;
          }
      }
      

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

      //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...
      

      【讨论】:

        猜你喜欢
        • 2021-07-23
        • 1970-01-01
        • 1970-01-01
        • 2017-09-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多