![]() |
|
Snippets |
|
This filter will automatically add css file with the name of current module.
It's great to have these files saved separately that to have one huge file. (And demo of CSSEdit doesn't allow saving files bigger than 2.5k chars :D)
Just create your cssAdderFilter.class.php, and add it to your filters.yml. Clear cache and you are ready to go.
<?php class cssAdderFilter extends sfFilter { public function execute( $filterChain ) { $context = $this->getContext(); $request = $context->getRequest(); $response = $context->getResponse(); $module = $request->getParameter("module"); $path_to_web = sfConfig::get("sf_web_dir"); $path_to_css = $path_to_web . "/css/" . $module . ".css"; if (file_exists($path_to_css)) { $response->addStylesheet($module.".css"); } $filterChain->execute(); } }
Comments on this snippet
I think you dont need to add the file_exists check. If it doesnt exist it wont be included ;).
aha:) I didn't know that, thanks
I think that this snippet
http://www.symfony-project.org/snippets/snippet/278
is covering that in a more complete and efficient way...
Ok I know it's mine ;O)
I do think that's a good thing to chekc if the file exist because if you don't it will be put in your HTML and the request to retrieve it from the server will be done... of course if the file doesn't exist nothing will append but a file_exists will produce a cleaner code and will (I think) be less ressource using then a useless server call.