12

次のコードを実行したい:

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

の値で名前が付けられた bldg のメンバー プロパティを呼び出すにはどうすればよいkeyですか? の値を取得したい

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

3 に答える 3

23

簡単な答え:直接/ネイティブに可能ではありません...まだ。

どうやら彼らは、まさにそのニーズに対応するattribute()と呼ばれる新しい関数をTwig1.2に追加したようです。

しかし、今日まで、ダウンロードできるのはTwig1.1.2のみです。したがって、1.2はおそらくSF2に同梱されていませんが、バージョン番号は見つかりません。(1.2が利用可能になりました!)

私はさまざまなトリックでそれを解決しようとしましたが、役に立ちませんでした。1.2はそれを修正します。

バージョン1.2の新機能:属性関数がTwig1.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」を出力します

于 2011-09-12T09:24:49.040 に答える
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...
于 2011-09-19T21:42:11.540 に答える
3

角かっこの構文を使用します。bldg[key]

于 2011-09-11T15:37:40.007 に答える