1
votes

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.

1
You'll never get $a = 'Test' because replacing 'a' with 'Test' in your $arr will cause $Test='Test' instead of $a='Test' - Luke

1 Answers

0
votes

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'