Reputation: 503
I am new to Zend Framework. I have a master layout file and I want to add and remove css/js files dynamically. I plan on creating an XML file which contains which controller/actions should have which files added. I was thinking of having the constructor for the controller read the xml file and add the files as required but this seems a bit bad practice. I am thinking it may be better to have this done in the bootstrap class file.
Can anyone tell me if this would be the correct way of doing it and how I may go about doing this please?
Upvotes: 0
Views: 218
Reputation: 40685
It is a good idea to load static resources only when you need them!
That said, it seems like a contradiction in your question that you're considering loading these view specific resources during bootstrap. That's much too early, your app has no clue yet about what will be needed.
With the tought of economic lazyness in the background, you should add resources in your views:
if (somecondition)
{
$this->headScript()->addJavascriptFile($this->baseUrl() . '/path/to/your file');
}
else
$this->jQuery()->addOnLoad($someShortjQueryScript);
}
If that's too late for your taste, you can do it in the action too:
$this->view->headLink()->appendStyle($someCSS);
Check out the view helpers, you can do all kinds of things, append, prepend, addOnLoad, add files, scripts, styles, etc.
It doesn't seem like a good idea to me to read a list of files from config. But I may be wrong.
Upvotes: 0
Reputation: 8196
Create your own layout plug-in class . Inside its post-dispatch hook code your own logic .
Upvotes: 1
Reputation: 2961
The correct way would be to let your views decide which styles/scripts they need. There are view helpers available for this very purpose. This way you separate your representation logic (views, scripts, css) from your application logic (controllers/bootstrap) and your data logic (database,...).
Upvotes: 2