2017-01-23 61 views
1

我目前正在學習phalcon和伏特爲一個項目。任何人都知道如何動態訪問視圖頁面中的變量?Phalcon訪問伏特視圖中的動態變量

例如,我有這個在我的控制器

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

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

$這個 - >查看 - > $名字,我想在伏視圖分配到$名稱的值。

我有這個在我看來,

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

它同時顯示 「一個」,但我需要的是,如果$ a = '測試'它應該顯示「a「中的值和」測試「中的值。

+0

你永遠不會得到$ a ='Test',因爲用'Test'替換'a'你的$ arr會導致$ Test ='Test'而不是$ a ='Test' – Luke

回答

0

直接從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 = '測試',因爲替換「你的$ arr中'with'Test'會導致$ Test ='Test'而不是$ a ='Test'