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 do I create a global stylesheets for headers and footers?

Hi All,

Can anybody please give me some advice how I can create CSS and JS assets which can be loaded as I need it on specific controllers. I was thinking of creating a Shared service then inside this shared service create my assets and use the $this-> function in my controller to load a particular stylesheet but how exactly do I do that?

I just want to minimize code duplication as much as possible.

Thanks



22.8k

Hi, @rewtor thanks for the link, I did follow the documentation but now I have a follow up question :

  1. Where should i put the php file with the assets inside? Must it be in my /app folder?
  2. How does it get loaded ( bootstrapped ) into my main app?

Kind Regards

edited Sep '14

As far as I can tell this is automatically loaded by Phalcon somewhere but I'm not too sure where in the documentation. Looking at the source: https://github.com/phalcon/cphalcon/blob/2.0.0/phalcon/di/factorydefault.zep#L61 I can see that it's automatically loaded if you use Phalcon\DI\FactoryDefault(); https://docs.phalcon.io/en/latest/reference/di.html#factory-default-di

You can add an asset anywhere that has acceess to the dependency injector by using either:

$this->assets->addJs();
$this->assets->addCss();

as described here https://docs.phalcon.io/en/latest/reference/assets.html#adding-resources

and just add either of the below in the bottom of your footer/layout and just make sure it's before </body>:

{{ assets.outputJs() }}
{{ assets.outputCss() }}


43.9k
edited Sep '14

Where should i put the php file with the assets inside

assets are css and js. You should put them in public folder. Also check here



22.8k

Hi everyone, thanks so much for the feedback, much appreciated!