簡體   English   中英

Twig:打印變量名為String的變量的值

[英]Twig: Print the value of a variable where the variable name is String

我有一個名為var1的變量,其值value1為字符串。 如何打印變量var1的值,其中var1是作為字符串獲得的?

{{ set container = 'var1' }}

變量container的值是動態的。 根據container的價值,我需要打印它的價值; 在這種情況下,我需要打印'value1'。

我正在尋找這樣的東西

{{ attribute(this, container) }} / * <=這不會因為this沒有在枝條定義* /

事實證明我錯了。

您可以使用包含傳遞給模板的所有變量的_context變量。

試試{{ dump(_context) }}

相關文件

您可以創建一個傳遞此上下文的函數,並使用數組鍵來訪問該值。

這個枝條功能應該工作正常:

public function getAttribute($context, $key)
{
    if (!array_key_exists($key, $context)) {
        return '';
    }

    return $context[$key];
}

變量傳遞title=foo ,另一個變量傳遞refTitle=title ,這應該輸出“foo”。

{{ attribute(_context, refTitle) }}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM