将动态页脚添加到TWIG文件

时间:2014-12-19 12:31:29

标签: twitter-bootstrap twig

我需要从我的一个php文件中获取数据(基本上我已经在那里编写了mysql查询)并且我想在TWIG文件中显示它(parent.html)

以下是有关详细信息的代码。 这是parent.html,我需要在此

中包含页脚
<!DOCTYPE html>
<html>
<head>
    <title>TEAM Aero: Your Commercial Jet Aircraft Trading Community</title>
    <meta content="text/html; charset=utf-8" http-equiv="content-type">
    <meta name="description" content="TEAM Aero: Your Commercial Jet Aircraft Trading Community" />
    <link rel="icon" href="/favicon.ico" type="image/x-icon" />
    <link rel="stylesheet" type="text/css" href="/includes/css/grid-menu-style.css" />
    <link rel="stylesheet" type="text/css" href="/includes/css/grid-menu-style-responsive.css" media="screen and (max-width: 900px)"/>
    <script type="text/javascript" src="/includes/js/libs/jquery-1.8.0.min.js"></script>
    <script type="text/javascript" src="/includes/js/libs/knockout-3.0.0.js"></script>
    {% block extra_js %}
    {% endblock %}
</head>
<body>

<div class='header'>
    <div class="clearfix">

        <div style="float: left;">
            <a href="/controls/grid_menu/view.php">
                <img src="/images/grid_menu/biglogo.jpg" style="float:right;"/>
            </a>
        </div>
        <div class="header-right-block">
            {% if globals.user.id %}
                <div class="header-image-wrapper">
                {% if globals.user.image %}
                    <img src="/files/user_photos/{{ globals.user.image }}" style="max-height: 100px; max-width: 100px;">
                {% else %}
                    <img src="/images/icon_no_photo_80x80.png" style="height: 100px; width: 100px;">
                {% endif %}
                </div>
            {% else %}
                <a href="/auth/login.php">
                    <div class="header-login-wrapper">
                        <div class="header-right-block-text">LOG IN</div>
                    </div>
                </a>
            {% endif %}
        </div>
    </div>
</div>

<div style="width:97%;margin: auto;padding-top:15px;">
    <a href="http://www.pw.utc.com/DependableServices" target="_blank">
        <img src="/images/grid_menu/PW-Services_Distance_600x100.jpg" style="width:100%;text-align:center;">
    </a>
</div>

<div id="user-info" data-role-id="{{ globals.role_id }}"></div>

<div class="free-wall grid-menu-container">
    {% block content %}
    {% endblock %}
</div>

{% include "grid_menu/footer.html" %}
</body>
</html>

以下代码是footer.php,其中包含查询

<?php

require_once("../../bootstrap.php");

$menus = Doctrine_Query::create()
    ->from("GridMenu")
    ->where("position = ?", array(12))
    ->orwhere("position = ?", array(11))
    ->orwhere("position = ?", array(10))
    ->orderBy("position ASC")
    ->execute();

$menusMap = array();
foreach($menus as $menu){
    if(empty($menusMap[$menu->position])){
        $menusMap[$menu->position] = array();
    }
    $menusMap[$menu->position][] = $menu;
}


foreach($menusMap as $menuPosition => $menusArray) {

    if($menuPosition == 0){

        continue;
    }
    //shuffle($menusArray);
    $menu = $menusArray[0];

    $block[$menuPosition]['image_path']= $menu->image_path;
    $block[$menuPosition]['url']= $menu->url;
} 
echo $twig->render('grid_menu/footer.html', array(
    "user" => User::getCurrent(),
    "menusMaps" => $block
));

?>

这是用树枝写的footer.html

{% block footer %}

<div class='footerAd'>
{% for key, menusMap in menusMaps %}
<a href='{{ menusMap.url|e }}' target='_self'><div class='cell' style='background-image: url(/images/grid_menu/{{ menusMap.image_path|e }});background-size:100% 100%;float:left;position:relative;margin:20px'></div></a>
{% endfor %}
</div>


<div class='header' style="clear:both">
    <div class="clearfix">
        <div class="float-left">

            <br>
            <br>
        </div>
    </div>
</div>

{% endblock %}

结果parent.html没有显示数据库结果,我想在parent.html中显示页脚内容,虽然它显示的是div而不是查询结果。

我是TWIG的新手,你的帮助不大可以解决我的大问题

提前致谢。

1 个答案:

答案 0 :(得分:2)

正如您所说,您的代码是&#34; 显示div而不是查询结果&#34;。这意味着{% include "grid_menu/footer.html" %}确实正在访问grid_menu/footer.html。查询结果未显示,因为您尚未执行查询。您所做的就是包括footer.html,但不执行footer.php

如果您使用的是Symfony,可以通过实现所谓的embedded controllers来自动执行。从您的代码结构中,我假设您没有使用Symfony。没关系!

我会给你一个非常简单的解决方案,尽量不要改变太多的代码。你在这里遇到的主要问题是{% include "grid_menu/footer.html" %}。通常情况下,您应该包含footer.php,但除非您使用像例如函数之类的树枝扩展名,否则枝条不允许这样做:

$function = new Twig_SimpleFunction("render", function ($phpFile) {
   include $phpFile;
}, array('is_safe' => array('html')));

$twig->addFunction($function);

现在使用此功能&#34; render&#34;定义后,您可以使用:

{{ render("footer.php") }}

而不是:

{% include "grid_menu/footer.html" %}

请允许我对与该问题无关的事情发表评论,但我不能不说出来:您可以使用whereIn函数,而不是使用位置参数,如下所示:< / p>

$menus = Doctrine_Query::create()
    ->from("GridMenu")
    ->whereIn("position", array(10, 11, 12))
    ->orderBy("position ASC")
    ->execute();

如果您遇到困难,请告诉我。我很乐意提供帮助。

希望它有用!