symfony - How to return Javascript in Symfony2? -
like on ruby on rails, can return javascript prerendering dynamically update client site through ajax.
how return javascript when calling routes in symfony2?
for example, when calling updateitemappearanceaction
through \item\{itemid}\update_appearance
, return javascript file updateitemappearance.js.twig
, array("item"->$item)
?
thank much!
you pass rendered view response object , set header of updateitemappearanceaction text/javascript.
<?php namespace ownerbuildercentre\corebundle\controller; use symfony\bundle\frameworkbundle\controller\controller; use sensio\bundle\frameworkextrabundle\configuration\route; use symfony\component\httpfoundation\request; use symfony\component\httpfoundation\response; // example controller. class defaultcontroller extends controller { /** * @route("/updateitemappearance.js", name="default_update_item_appearance_js") */ public function updateitemappearanceaction() { $optionalparams = array('some'=>'param'); $response = new response($this->renderview('appbundle:default:updateitemappearance.js.twig',$optionalparams)); $response->headers->set('content-type','text/javascript'); return $response; } }
so now, javascript write in updateitemappearance.js.twig file can included javascript file via usual means.
<script src="/updateitemappearance.js"></script>
or using twig path helper.
<script src="{{ path('default_update_item_appearance_js') }}"></script>
Comments
Post a Comment