Yii add another $ content template to the template

I have a Yii template whose structure is:

  • Include CSS files in
  • echo $ content right after
  • Include Javascript files (e.g. JQPlot) after $ content

What I would like to do is add a custom set of Javascript / PHP code after including all Javascript files. I know that this can be done by simply adding code to the template.

But I have many pages with custom JS / PHP code, and I would like to include only the specific code for this page to avoid long loading times.

Can I create a new element that works like $ content, but it will include, for example, "js.php" from the same View folder as "index.php"? "Js.php" will be in each view folder, I need a special code and contain a combination of js / php code for this particular page.

Thanks for the help!

+4
source share
2 answers

Clips are your best bet, as @jfrej offers, pointing you to a forum topic where clips are discussed.

Under normal circumstances, each controller is associated with a folder in which all its views are located, and in this case you want to include some PHP + Javascript content that is common to all actions in the controller at the end of the layout (this is how they refer, not the templates) .

I would override the CController::afterRender() method to capture the content for your clip; allows you to call your clip controller_content :

 afterRender(string $view, string &$output) $this->beginClip('controller_content'); // output here any content you want to capture into your clip // eg renderPartial, echo, etc. ... $this->endClip(); parent::afterRender($view, $output); } 

Then in your layout you will display your clip after everything that is always there, for example:

 // register your CSSs // output $content variable // register Javascript ... <?= $this->clips['controller_content'] ?> 

Of course, there are different ways to generate your clip content. Here I tried the CController::afterRender() method, but you can also use filters, behavior, or any other approach that best suits your needs.

0
source

You can try:

Yii :: application () β†’ clientScript-> registerCssFile (Yii :: application () β†’ BaseUrl '/CSS/example.css.);
Yii :: application () β†’ clientScript-> registerScriptFile (Yii :: application () β†’ BaseUrl '/CSS/example.js'.);

You can call it from the controller

0
source

All Articles