简体   繁体   中英

Phalcon access dynamic variables in Volt View

I'm currently studying phalcon and volt for a project. Anyone knows how to access dynamically the variables in the view page?

For example, I have this in my controller

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

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

$this->view->$name , I want to get the value assigned to the $name in the volt view.

I have this in my view,

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

It displays both " a ", but what i need is if $a = 'Test' it should display " a " in the label and " Test " in the value.

Straight from Volt documentation :

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

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

So in your case it will be something like:

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

But in your example you'll never get $a = 'Test' because replacing 'a' with 'Test' in your $arr will cause $Test='Test' instead of $a='Test'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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