Php 如何访问由变量确定的细枝中的成员?
我想执行以下代码:Php 如何访问由变量确定的细枝中的成员?,php,symfony,twig,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.
{% 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的成员属性?我想得到
bldg.hasStudio
bldg.has1Bed
bldg.has2Bed
etc....
使用括号语法:bldg[key]
简短回答:不直接/本机可能。。。然而
显然,他们在Twig1.2中添加了一个新的函数,该函数正好满足了这一需求
但到目前为止,你只能下载Twig1.1.2;所以1.2可能没有随SF2一起提供-尽管我找不到版本号(1.2现在可用!)
我试图用不同的技巧来解决这个问题,但没有成功;1.2将修复它
版本1.2中新增:Twig 1.2中添加了属性函数
属性可用于访问变量的“动态”属性:
{{属性(对象、方法)}}
{{属性(对象、方法、参数)}}
{{属性(数组,项)}}
但是您可以做的是向类中添加一个方法,该方法可以处理您需要的任何内容。诸如此类:
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) }}
小枝:
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”为此,我编写了自己的细枝扩展。你会以我想要的方式使用它:
{% 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...
谢谢你的信息。这个解决方案的问题是,我不想让实体类和getter混为一谈,因为twig需要它们……twig1.2中的属性特性太棒了