虽然没有打印,但Twig`Coliable不存在'错误

时间:2017-03-14 23:31:55

标签: symfony twig

我正在尝试处理我的控制器中的非现有变量,如果数据库为空将返回一个没有任何变量的视图:

if(!$votes->findOneById(1) || !$images->findOneById(1)){
    return $this->render('admin/stats_and_images.html.twig');
}

return $this->render('admin/stats_and_images.html.twig', [
    'images' => $images->countVotesForAllImages(),
    'image_podium' => $images->getTopNImages(3),
    'form' => $form->createView(),
    'votesToday' => $votes->votesToday(),
    'votesMonth' => $votes->votesMonth(),
    'votesTotal' => $votes->votesTotal()
]);

在我看来,我正试图处理缺乏这样的变量:

        {% if (votesTotal[0][1] is defined) and (votesToday[0][1] is defined) and (votesMonth[0][1] is defined) %}

            <div class="col-md-6">
                <h4 class="sub-section--header">Liczba Oddanych Głosów:</h4>
                <hr>
                <p>
                <div class="col-sm-6">
                    Dzisiaj:
                    <span class="text-info large-num">{{ votesToday[0][1] }}</span>
                </div>

                <div class="col-sm-6">
                    Ten miesiąc:
                    <span class="text-info large-num">{{ votesMonth[0][1] }}</span>
                </div>
                </p>
                <p>
                    <strong>Głosów ogółem: </strong><span class="text-info large-num">{{ votesTotal[0][1] }}</span>
                </p>
            </div>
            <div class="col-md-6">
                <h4 class="sub-section--header">Wygrywające zdjęcia:</h4>
                <p class="text-muted">Ten miesiąc</p>
                <hr>
                <div class="row text-center">
                    <div class="col-md-4">
                        <a 
                            href="{{ asset("uploads/"~image_podium[0][0].fileName) }}" 
                            target="blank">
                            <img 
                                src="{{ asset("uploads/"~image_podium[0][0].fileName) | imagine_filter('my_thumb') }}" alt="{{image_podium[0][0].title}}" 
                                class="site-thumbnail" 
                                title="{{image_podium[0][0].title}} - {{image_podium[0][0].author}}">
                        </a>
                        <p><strong>Głosów: {{image_podium[0]['votes']}}</strong></p>
                    </div>
                    <div class="col-md-4">
                        <a 
                            href="{{ asset("uploads/"~image_podium[1][0].fileName) }}" 
                            target="blank">
                            <img 
                                src="{{ asset("uploads/"~image_podium[1][0].fileName) | imagine_filter('my_thumb') }}" alt="{{image_podium[1][0].title}}" 
                                class="site-thumbnail" 
                                title="{{image_podium[1][0].title}} - {{image_podium[1][0].author}}">
                        </a>
                        <p>Głosów: {{image_podium[1]['votes']}}</p>
                    </div>
                    <div class="col-md-4">
                        <a 
                            href="{{ asset("uploads/"~image_podium[2][0].fileName) }}" 
                            target="blank">
                            <img 
                                src="{{ asset("uploads/"~image_podium[2][0].fileName) | imagine_filter('my_thumb') }}" alt="{{image_podium[2][0].title}}" 
                                class="site-thumbnail"
                                title="{{image_podium[2][0].title}} - {{image_podium[2][0].author}}">
                        </a>
                        <p>Głosów: {{image_podium[2]['votes']}}</p>
                    </div>
                </div>
            </div>
        {% else %}
            <h2 class="text-danger text-center">
                No votes at the moment :)
            </h2>
        {% endif %}

但是,尽管要定义所有三个领域的严格要求,我还是得到了这个:

  

变量“votesToday”不存在。

并指向视图的<span class="text-info large-num">{{ votesToday[0][1] }}</span>部分。

为什么会这样?如何避免?

1 个答案:

答案 0 :(得分:0)

将$ votes发送给twig并使用{{dump(votes)}}

进行检查

如果是学说实体,请检查你的吸气剂。也许是$ votes-&gt; getVotesToday()