2016-01-08 197 views
0

樹枝模板在我的網頁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,我想呈現頁面產品< < {{ render(controller('FLYBookingsBundle:Post:product')) }} >>僅在用戶點擊鏈接我的產品列表時在div描述中。我如何用樹枝做到這一點?

+0

在哪裏鏈接? – Isky

+0

@Isky這是鏈接:'

' – Sirius

+0

一種方法是在'path('post')'使用路由來提供對'

...
的Ajax響應。 – geoB

回答

1

下面是我用來「彈出」一個表單,用於根據與該實體出現的按鈕向實體發送電子郵件的解決方案。

如果尚未安裝,您將需要安裝jquery。有很多可能的方法來做到這一點。使用你的朋友谷歌&「symfony安裝jquery」找到一個。您可能還想爲您的瀏覽器找到一個好的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>