Wednesday, June 14, 2017

Plone theming: Injecting a class into an existing tag with Diazo and xsl

In a recent post (https://community.plone.org/t/textareas-not-displaying-existing-content-when-my-theme-is-enabled/4117/2?u=pigeonflight) I mentioned a Diazo rule where I was injecting a new class into a tag, I used an <xsl:template>.

The following example adds a special class "gl-textarea" to all <textarea> elements it uses a Diazo <replace> directive instead of an <xsl:template>.
... ....

If you've done enough xslt then you probably know that this could have been achieved with an <xsl:template> element, the problem is that <xsl:template> elements won't work in all scenarios when using Diazo. 

A note about Diazo, <xsl:template> and nested scenarios

<xsl:template> elements in nested rules will fail silently. This also means that externally included files will also fail because rules in externally included files are implicitly nested.
So something like this, if located in the main rules.xml file, should work:
<rules>
    <xsl:template>
    ...
    </xsl:template>
</rules>

This will fail, because it is in a nested <rules> element.
<rules>
    <rules>
          <xsl:template>
          ...
         </xsl:template>
     </rules>
</rules>

On the other hand, Diazo's <replace> directive can be used as a work-alike to the <xsl:template> element and it works in nested scenarios.

Don't do this

Here's the <xsl:template> approach which works in very specific scenarios.
 ... ...

When you can do this

For comparison, here again, is an example that uses the <replace> directive and should work in most scenarios.
... ....
Don't forget to include the apply-template, it is needed.

No comments:

Sign up for my upcoming Plone 5 Book & Video tutorials

plone 5 for newbies book and videos