Adding page-specific Javascript to each view in CakePHP

前端 未结 9 1185
余生分开走
余生分开走 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:45

    With Cake v3 you can do it like this. Add the scripts you want in the specific controller.

    //YourController.php
    public function beforeRender (Event $event)
    {
        array_push($this->scripts, 'Admin/gallery');
        parent::beforeRender($event);
    }
    

    Set the default in intialize and render once //AppController.php public function initialize() { parent::initialize();

        $this->scripts = [];
    }
    
    public function beforeRender (Event $event)
    {
        /* Define scripts in beforeRender of Child */
        $this->set('scripts', $this->scripts);
        /*-----------------------------------------*/
    }
    

    Now you can run this function once.

    Html->script($scripts) ?>
    

提交回复
热议问题