With the new Zend_Layout component working complimentary with Zend_View, new amazing possibilities of complex views have become to the Zend Framework.

One of the main features of the new Zend_Layout - Zend_View pattern is the possibility of using “placeholders“. Among the “placeholders” developers can define by themselves, there are some predefined placeholders helpers in Zend Framework that are quite useful in this pattern. One of the most interesting predefined placeholders helper is headLink . One of the common uses of headLink placeholder helper is to append CSS style sheets in the different views that can compose a final layout, and this appended CSSs will be added to the head section in the final layout rendering. To do this, headLink placeholder has the next method:

appendStylesheet($href, $media, $conditionalStylesheet)

Parameters are quite self-explained, except the last “$conditionalStylesheet”. Documentation about this parameter states:

$conditionalStylesheet is a boolean, and will be used at rendering time to determine if special comments should be included to prevent loading of the stylesheet on certain platforms.

So if I set it to “TRUE” is going to insert a special comment (conditional comment for explorer), on certain platforms (Internet Explorer). This is good is my first feeling because I am using Condtional Comments for Internet Explorer CSS hacking. The problem is that there are different conditions you can establish in the conditional comments to target different versions of Internet Explorer. For instance: Normally I load a specific CSS file for older than 6 Internet Explorer Browsers, other for 6 and newer. Sometimes I need a specific CSS for Internet Explorer 7 (why? It is Microsoft, you know!).

This is not being possible, according to the headLink placeholder doc, because the only thing you can do is to set “true” the conditionalStyleSheet parameter, and your CSS will be inside a conditional comment that will target to all Internet Explorer versions. I hope to find a way of passing the condition I would like to use in that conditional comment, but I didn´t find nothing about it. Googling neither answered this.

At the end, looking at the code I finded a way of doing, quite simple: Because Zend is not doing a strict comparission in that parameter, you can use a string (the condition) instead of a strict boolean in that 3rd parameter. This do the job. So, instead of writing:

headLink()->appendStylesheet('/styles-for-all-explorers.css', 'screen', true);?>

you can write:

headLink()->appendStylesheet('style-only-for-older-than-6.css', 'screen', 'lt IE 6');?>

And it works!.

Note for Zend Guys: You should include some explanation about this in your documentation.
Note for english readers: Sorry for my english…. I Know it could be better!
Nota para lectores en español: Perdón por escribir en inglés, pero dado el interés del truco que quiero compartir, voy a tratar de hacerlo lo más “internacional” posible:


6 comentarios a “How to append a conditional stylesheet using headLink placeholder in Zend Framework”  

  1. Gravatar Icon 1 Ernesto

    Just what i was looking for, many thanks !!
    you are in my feed reader now ;)

  2. Gravatar Icon 2 Oscar Reales

    Happy to help you!.

  3. Gravatar Icon 3 xxcemil
  4. Gravatar Icon 4 asp-dersi

    HI i need your help i really want to create my own website/web page but i dont know how to go about doing it so can you please help me out

  5. Gravatar Icon 5 Isaak

    Thank you, the ZFW documentation didn’t mention this but I’m glad you did.

  1. 1 Tutoriel Zend Framework : Intégrez vos feuilles de styles spécifiques à Internet Explorer via headLink | itanea, le blog

Deja una respuesta



Comparte este artículo

surlandia

comentados

Surlandia en tu email

Un email diario sólo cuando haya nuevas entradas

Gestionado por FeedBurner