简体   繁体   English

细枝印刷可从字符串中获取

[英]Twig print available from string

How to print available form string in twig In my code php 如何在我的代码php中在树枝中打印可用的表单字符串

$data['fruits'] = array('apple', 'banana', 'orange');
$data['help_apple'] = 'This is help apple';
$data['help_banana'] = 'This is help for banana';
$data['help_orange'] = 'This is help for orange';

In twig template 在树枝模板中

{% for fruit in fruits %}
{{ "help_" ~ fruit }}
{% endfor %}

The print screen is help_apple, help_banana, help_orange How to print correct data i need for help_ fruit key ? 打印屏幕是help_apple,help_banana,help_orange如何打印我需要help_水果键的正确数据?

You need to use the attribute function with _context. 您需要将属性函数与_context一起使用。 Tested on twigfiddle.com. 在twigfiddle.com上测试。 Hope this helps. 希望这可以帮助。

{% for fruit in fruits %}
    {# Here is how you do it #}
    {{ attribute(_context, 'help_'~ fruit) }}
{% endfor %}

The _context variable holds all variables in the current context. _context变量保存当前上下文中的所有变量。 Instead of using the attribute function , you can access values of the _context array with the regular bracket notation as well: 除了使用attribute函数之外 ,您还可以使用常规方括号表示法访问_context数组的值:

{% for fruit in fruits %}
    {{ _context['help_' ~ fruit] }}
{% endfor %}

I would personally do it this way as it's more concise and in my opinion clearer. 我个人将以这种方式进行操作,因为它更简洁并且我认为更清晰。

You might want to check for undefined variables when accessing values of the _context array. 访问_context数组的值时,可能要检查未定义的变量。 I have written about it in my answer to this question: Symfony2 - How to access dynamic variable names in twig . 我在回答以下问题时已经写过: Symfony2-如何在twig中访问动态变量名


You also asked whether something like this is possible: 您还问这样的事情是否可能:

{% set attribute(context, 'help' ~ fruit, "value") %}

That's not possible. 那不可能 If you want to set variables with dynamic names, you need to create a Twig extension. 如果要使用动态名称设置变量,则需要创建一个Twig扩展名。 Take a look at my answer to this question: How to set a variable name with dynamic variables? 看看我对这个问题的回答: 如何使用动态变量设置变量名?

But, like @user9189147 mentioned, it would be easier and in my opinion clearer if you instead created a new array to hold the help values. 但是,就像提到的@ user9189147一样,如果您创建一个新数组来保存帮助值,那会更容易,而且我认为更清晰。 Then you wouldn't need to create an extension. 然后,您无需创建扩展。

$data['fruits'] = ['apple', 'banana', 'orange'];
$data['help']   = [];
$data['help']['apple']  = 'This is help apple';
$data['help']['banana'] = 'This is help for banana';
$data['help']['orange'] = 'This is help for orange';
{% for fruit in fruits %}
    {{ help[fruit] }}
{% endfor %}

Then you can set new values to the help values in Twig using the merge filter , like this (though I don't know why you would want to do it in Twig): 然后,您可以使用merge过滤器将新值设置为Twig中的帮助值(尽管我不知道您为什么要在Twig中这样做):

{# Set single value #}
{% set help = help|merge({
    banana: 'New help for banana',
}) %}

{# Or multiple values #}
{% set help = help|merge({
    apple:  'An apple a day keeps the doctor away',
    orange: 'Orange is the new black',
}) %}

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

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