在条件下渲染树枝模板

时间:2016-01-08 17:37:02

标签: php symfony twig rendering

在我的页面show.html.twig中我有这个块

<div class="col-md-9 col-sm-9  user-wrapper">
<div class="description">                     
{{ render(controller('FLYBookingsBundle:Post:new')) }}
</div>
</div>

正如您所看到的,有一个呈现页面new.html.twig的渲染,我想渲染页面产品&lt;&lt; {{ render(controller('FLYBookingsBundle:Post:product')) }}&gt;&gt;仅当用户单击“我的产品列表”链接时,才在div描述中。我怎么用树枝做到这一点?

1 个答案:

答案 0 :(得分:1)

这是我以前使用的解决方案&#34;弹出&#34;用于根据与该实体一起出现的按钮向实体发送电子邮件的表单。

如果尚未安装,则需要安装jquery。有很多可能的方法可以做到这一点。使用你的朋友Google&amp; &#34; symfony安装jquery&#34;找一个。您可能还想为您的浏览器找到一个好的javascript调试器。我在Firefox中使用Firebug插件。

带链接的模板(为简洁起见而编辑)。返回的电子邮件表单显示在<div id="dialog"></div>

<div id="dialog"></div>
{% for opp in opportunities %}
    <div class="row">
        <div class="col-md-9">
            <ul class="list-unstyled">
                ...
                <li><a href="#" value="{{ opp.id }}" id="emailOrganization" class="btn btn-xs btn-info" >E-mail {{ opp.orgName }}</a>
            </ul>
        </div>
    </div>
{% endfor %}

使用Javascript:

$(document).on("click", "#emailOrganization", function () {
    var where = $(location).attr('pathname');
    var id = $(this).attr("value");
    //replaces URI ending in 'search' with 'oppForm/' + id (of organization)
    var url = where.replace('search', 'oppForm/' + id);
    $.get(url, function (data) {
        //creates dialog box containing e-mail form
        $('#dialog').dialog();
        $('#dialog').dialog({
            modal: true,
            buttons: [
                {
                    text: "Send",
                    id: "send",
                    class: "btn-xs btn-primary",
                    click: function () {
                        var formData = $("form").serialize();
                        $.post(url, formData, function (response) {
                            if (response.indexOf("Email sent") >= 0) {
                                $("#send").hide();
                            }
                            $('#dialog').html(response);
                        })
                    }
                },
                {
                    text: 'Close',
                    id: "close",
                    class: "btn-xs btn-primary",
                    click: function () {
                        $(this).dialog("close");
                    }
                }
            ],
            resizable: true,
        });
        $('#dialog').dialog("widget").find(".ui-dialog-titlebar").hide();
        $('#dialog').html(data);
    });
});

控制器(为简洁起见编辑)

/**
 * @Route("/oppForm/{id}", name="opp_form")
 * @Template("default/oppEmail.html.twig")
 */
public function oppFormAction(Request $request, $id)
{
    ...
    $form = $this->createForm(new OpportunityEmailType($oppName, $orgName, $email, $id));
    $form->handleRequest($request);
    if ($request->getMethod() == 'POST') {
        if ($form->isValid()) {
            ...
            }
            $response = new Response("Email sent: " . count($to));

            return $response;
        }
    }

    return [
        'form' => $form->createView(),
        'id' => $id,
    ];
}

模板:

<form role="form" action="{{ path('opp_form', {'id': id}) }}" method="post" name="opp_email">
    {# hidden submit button allows functional test; also tested with codeception #}
    <div style="visibility: hidden;"><input type="submit" value="Mail"></div>
{{ form_widget(form._token) }}
{{ form_row(form.id) }}
{{ form_row(form.to) }}
{{ form_row(form.from) }}
{{ form_row(form.subject) }}
{{ form_row(form.message) }}
</form>