We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

how insert js code in end body

Hi! I can insert different js.js in here < / body> but how insert <scrypt> bla-bla-bla </scrypt> </ body> depending from the action controller or simple controller. I know there are ways to code injection through the "elements", but it is necessary to implement JavaScript code is depending on the controller used. One controller wants to show one code to another need another. hint about the true idea please



85.5k
Accepted
answer
edited Nov '15

well according to this : https://docs.phalcon.io/en/latest/api/Phalcon_Mvc_Router.html

there are :

public getModuleName ()
public getControllerName ()
public getActionName ()

so inside your layout you can do


if($this->router->getControllerName () === 'cars') {
    echo "<script type="text/javascript" src=""whatver.js></script>";
}

or if you have different views just put the js there.

hope i understood you correctly



14.4k

Thank you!!! All right. It is that what i needed.



17.5k

UI guys hate this, FYI.



17.5k

Sorry, not a very helpful comment. If you want to dynamically insert JS, either include it in the view that is being loaded in the controller (my favorite way) or use Phalcon's resource combiner (still haven't gotten it to work). https://docs.phalcon.io/en/latest/reference/assets.html

Use views(volt) and just render this code in volt..... not from controller, wtf.