dongliao1860 2017-01-23 02:39
浏览 57

Phalcon在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.

  • 写回答

1条回答 默认 最新

  • doufei7464 2017-01-23 14:02
    关注

    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'

    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程