简体   繁体   中英

Twig template extract a subset

Is is possibile to extract a piece of a twig template? I need to manage a table ajax refresh, I have a twig template for example:

<html>
<body>
<table>
<thead>
  <tr><th></th></tr>
</thead>
<tbody id="mypiece">
  <tr><th></th></tr>
  <tr><th></th></tr>
</tbody>
</table>
</body>
</html>

In the first load I need the whole template, via ajax I need just the #mypiece content, is it possibile to extract it from twig using the DOM id or with some other markers?

The only solution I found is to divide this in two different template and use an include steatment.

whole.html

    <html>
    <body>
    <table>
    <thead>
      <tr><th></th></tr>
    </thead>
    <tbody id="mypiece">
{% include 'content.html' %}
    </tbody>
    </table>
    </body>
    </html>

content.html

  <tr><th></th></tr>
  <tr><th></th></tr>

But I think this is really a bad solution...

Well, I personally prefer do actually divide them, but if you intend to get something with AJAX as well - try using embeded controllers (in this case, specifically for your XHR request), for example:

<tbody id="mypiece">
    {{ render(controller(
        'SomeBundle:SomeController:someAction',
        { 'someParameter': "something" }
    )) }}
</tbody>

This is a lot better then to parse some rendered template to get part of it, because to me it seems like design flow.

Even better solution is to return specified json data on ajax call and render it in one of the javascript template engines on the client side.

Hope this helps, cheers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM