Adding page-specific Javascript to each view in CakePHP

前端 未结 9 1164
余生分开走
余生分开走 2020-12-28 09:35

In an attempt to keep my scripts maintainable, I\'m going to move each into their own file, organised by controller and action:

// scripts which only apply t         


        
9条回答
  •  独厮守ぢ
    2020-12-28 09:50

    I have authored a Plugin for this exact issue.

    If you have two files:

    • /app/View/Post/add.ctp
    • /app/View/Post/add.js

    it will include the add.js into the script block of the page.

    OR

    • app/View/Post/add.js
    • app/webroot/js/post/add.js

    it will include /js/post/add.js into the script block of the page.

    A few cute features are in there and it's simple as beans. You can even use PHP inside your .js files and use viewVars which you set in the action. Most of all, you don't need to hack the view or layout files to make it work, simply fetch the 'script' block in the layout. You can also simply write the .js to another view block.

    You can check it out here: https://github.com/dizyart/cakephp-viewautoload

    It's in the early stages, so make sure to comment and wishlist!

提交回复
热议问题