dongyuan7981 2016-04-14 06:28
浏览 88

Phalcon模板“未定义变量”(变量已设置)

I want to show a template file (ajax include) with variable from controller. I want to create a simple shoutbox.

development environment:

PHP Version 5.6.19 (xampp)

Phalcon 2.1.0r (php c-ext)

Windows 10

IDE Netbeans

This is the include part: (it works)

$("#shoutbox_messages").load("{{ static_url("shoutbox/getshouts") }}");

This is my controller function (app/controllers/ShoutboxController.php):

    public function getshoutsAction() {

    $shouts = $this->di->getModelsManager()
            ->createBuilder()
            ->columns(array('Shouts.*', 'Users.name'))
            ->from('Shouts')
            ->join('Users')
            ->orderBy('Shouts.created_at DESC')
            ->getQuery()
            ->execute()
            ->toArray();


    $this->view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT);


    $this->view->setVar("shouts", $shouts);

    }

This is my view file (app/views/shoutbox/getshouts.twig):

{% for shout in shouts %}



<div class="panel panel-default">
    <div class="panel-heading">
        <h3 class="panel-title">{{ shout.name }}</h3>
    </div>
    <div class="panel-body">
        {{ shout.shouts.text }} 
    </div>
</div>

{% endfor %}

(The twig file extension is set as the volt engine file extension.)

This view is part of a layout file (included from the main layout):

{# Shoutbox #}

<div id="flash_sb"></div>
{% include "shoutbox/shoutform.twig" %}

<hr/>
<div id="shoutbox_messages">
{% include "shoutbox/getshouts.twig" %}

</div>

I don't know why i get an error when include this file:

Notice: Undefined variable: shouts (in app/views/shoutbox/getshouts.twig)

When i use only the controller/action (http://myurl/shoutbox/getshouts) it works. I have access to the variable "shouts".

I don't understand why this works when i use http://myurl/shoutbox/getshouts but in the layout there is no var "shouts".

If u need more information tell me please.

I hope someone can tell me whats wrong.

</div>
  • 写回答

1条回答 默认 最新

  • douguai4653 2016-04-18 08:05
    关注

    You may should pass the vars with using with like that:

    {% include "shoutbox/getshouts.twig" with ['shouts': shouts] %}
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ESP8266连接阿里云出现问题
  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角