【问题标题】:Phalcon access dynamic variables in Volt ViewPhalcon 在 Volt 视图中访问动态变量
【发布时间】:2017-01-23 02:39:10
【问题描述】:

我目前正在为一个项目研究 phalcon 和 volt。有人知道如何动态访问视图页面中的变量吗?

例如,我的控制器中有这个

$arr = array('a','b','c','d');

foreach($arr as $name)
{
    $this->view->$name = constant($name);
}
$this->view->arr = $arr;

$this->view->$name ,我想在 volt 视图中获取分配给 $name 的值。

我认为这是,

{% for name in arr%}
    <div>
        <label>{{ name }}</label>
        <span>{{ name }}</span>
    </div>
{% endfor %}

它同时显示“a”,但我需要的是如果 $a = 'Test'它应该在标签中显示“a”,在值中显示“Test”。

【问题讨论】:

  • 你永远不会得到 $a = 'Test' 因为在 $arr 中用 'Test' 替换 'a' 会导致 $Test='Test' 而不是 $a='Test'跨度>

标签: php phalcon volt


【解决方案1】:

直接来自Volt documentation

{% set numbers = ['one': 1, 'two': 2, 'three': 3] %}

{% for name, value in numbers if name !== 'two' %}
    Name: {{ name }} Value: {{ value }}
{% endfor %}

所以在你的情况下,它会是这样的:

{% for key,value in arr if key == 'a' and value == 'Test'%}
    <div>
        <label>{{ key }}</label>
        <span>{{ value }}</span>
    </div>
{% endfor %}

但在您的示例中,您永远不会得到 $a = 'Test',因为在 $arr 中将 'a' 替换为 'Test' 会导致 $Test='Test' 而不是 $a='Test'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2016-12-26
    • 1970-01-01
    • 1970-01-01
    • 2019-10-03
    相关资源
    最近更新 更多