Forums / Setup & design / Is there an eZ way to dynamically include css & js ?

Is there an eZ way to dynamically include css & js ?

Author Message

Sébastien Antoniotti

Saturday 08 December 2007 1:19:41 am

Hi,

I'm looking for a way to dynamically include my stylesheets and js files.

By example if the current node is an article, I would like the article.css to be included in the <head> section of my page, if the current node is a product, the product.css, product1.css and maybe product.js will be included and not article.css.

So did you know an eZ way to do this ? Maybe I can create a new settings file with rules like this :

includeCSS[article]=article.css
includeCSS[product]=product1.css
includeCSS[product]=product2.css

includeJS[product]=product.js

Thanks in advance for your help !

eZ Publish Freelance
web : http://www.webaxis.fr

Paul Leclercq

Saturday 08 December 2007 2:51:35 am

Hiya Sébastien,

If you which to dynamically include stylesheets or javascript to your webpage you need to place your stylesheets or javasctpt in the stylesheet or javascript folder of your siteaccess and include them in the override of the design.ini file :
http://ez.no/doc/ez_publish/technical_manual/3_10/reference/configuration_files/design_ini

You can then use the ezini operator to extract the data:
http://ez.no/doc/ez_publish/technical_manual/3_8/reference/template_operators/data_and_information_extraction/ezini

And you create specifique conditions for each section:
You then place something like this in the override of your page_head.tpl template
for example:

{if $module_result.uri|contains('/myfolder')}
    {def $stylesheets = ezini('StylesheetSettings', 'MyFolderSiteCSS', 'design.ini')}
           {foreach $stylesheets as $stylesheet}
              <link rel="stylesheet" type="text/css" href={"stylesheet"|ezdesign} media="screen" />
           {/foreach}
    {undef $stylesheets}
{/if}

This works in the same way for javascript files. There are probably ways of reducing this but this should work.

Sébastien Antoniotti

Saturday 08 December 2007 3:07:04 am

Thanks for this solution Paul,

Good dev ;)

eZ Publish Freelance
web : http://www.webaxis.fr