symfony - Render template into Symfony2 with ajax -
i have action in controller index route
routing.yml
index: pattern: /index defaults: { _controller:acmedemobundle:default:index }
controller path
public function indexaction() { return $this->render('acmedemobundle:plugin:index.html.twig'); }
and index.html.twig template
{% extends'::base.html.twig' %} {% block stylesheets %} {% stylesheets filter='cssrewrite' output='css/*.css' 'bundles/acmedemo/css/*' %} <link href="{{ asset_url }}" type="text/css" rel="stylesheet" /> {% endstylesheets %} {% endblock stylesheets %} {% block body %} <br> <div class="container"> <div class="wp_attachment_holder"> <div class="imgedit-response" id="imgedit-response-8"></div> <div class="wp_attachment_image" id="media-head-8"> <p id="thumbnail-head-8"><img class="thumbnail" src="http://localhost/wordpress/wp-content/uploads/2014/06/121-1024x583.jpeg" style="max-width:100%" alt=""></p> <p><a class="btn btn-sm btn-default" id="edik-wp-extended-edit">Редактировать</a> <span class="spinner"></span></p> </div> <div style="display:none" class="image-editor" id="image-editor-8"> </div> </div> <div id="output"></div> <img class="thumbnail" data-attach-id="8" data-src="http://localhost/wordpress/wp-content/uploads/2014/06/121-1024x583.jpeg" style="max-width:100%" alt=""> <script> $('#edik-wp-extended-edit').click(function() { window.location= routing.generate('ajax'); // $('#output').load('/ajax/index'); }); </script> </div> {% endblock %}`
when button Редактировать clicked want load template ajax.
another.html.twig
<div>hello</div>
routing.yml
ajax: pattern: /ajax/index defaults: { _controller :acmedemobundle:default:ajax } options: expose: true
controller path
public function ajaxaction() { $template = $this->renderview('acmedemobundle:plugin:another.html.twig'); return new response($template); }
but when click button uri /ajax/index
. want stays /index
, template rendered index template
what doing wrong?
thanks.
first, ajaxaction()
should bit different far know, me works:
$template = $this->forward('acmedemobundle:plugin:another.html.twig')->getcontent(); $json = json_encode($template); $response = new response($json, 200); $response->headers->set('content-type', 'application/json'); return $response;
what forward()
function renders template , gives rendered html code.
in javascript file should this
$.ajax({ type: "post", datatype: 'json', url: routing.generate('ajax'), async: false //you won't need if nothing in following code dependend of result }) .done(function(response){ template = response; $('#your_div').html(template.html); //change html of div id = "your_div" }) .fail(function(jqxhr, textstatus, errorthrown){ alert('error : ' + errorthrown); });
you make ajax call ajaxaction
, return html of template want rendered.
after need add <div id="your_div"></div>
@ position want template rendered. workes me.
to mention need break down ajax
template code should shown.
Comments
Post a Comment