There is a certain framework I'm using in which the main developers are considering drastically changing from native PHP based templating to XSLT templating.
I'm worried that this won't be feasible because on our sites we usually have very complex templating logic.
For something as simple as this:
if ( $something ) { ?>
<p><?php if ( $another ) { ?>Lorem Ipsum<?php } else { ?>Dolor amet<?php } ?>.</p>
<?php } else { ?>
<p><?php if ( $another ) { ?>Lorem Ipsum<?php } else { ?>Dolor amet<?php } ?>.</p>
<?php } ?>
The equivalent XSLT would be:
<xsl:choose>
<xsl:when test="blah">
<xsl:choose>
<xsl:when test="another">
<p>Lorem Ipsum.</p>
</xsl:when>
<xsl:otherwise>
<p>Dolor amet.</p>
</xsl:otherwise>
</xsl:when>
<xsl:otherwise>
<xsl:when test="another">
<p>Lorem Ipsum.</p>
</xsl:when>
<xsl:otherwise>
<p>Dolor amet.</p>
</xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
With such a simple code snippet, it scares me when I think of advanced scenarios.
I'm wondering if anyone has undergone through a similar templating conversion and if so, how did you cope with it? Did you go back?
In my experience, eventually, I always go back to plain .phtml
files - not that this is the right thing or the ideal thing, but what solved the problems at the time.
XSLT never got things right for me, even for simpler logic templates.
If there is any template system that has made me happy, that was Twig.
In XSLT 1.0 your proposed code can be simplified to:
<p>
<xsl:choose>
<xsl:when test="blah">
<xsl:when test="another">Lorem Ipsum.</xsl:when>
<xsl:otherwise>Dolor amet.</xsl:otherwise>
</xsl:when>
<xsl:otherwise>
<xsl:when test="another">Lorem Ipsum.</xsl:when>
<xsl:otherwise>Dolor amet.</xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</p>
In XSLT 2.0 it can be further simplified to:
<p>
<xsl:value-of select="if (test=blah)
then if (test=another) then 'Lorem ipsum' else 'Dolor amet'
then if (test=another) then 'Lorem ipsum' else 'Dolor amet'"/>
</p>
Which strikes me as a lot nicer than your PHP original.
For the more general question, XSLT does have a steep learning curve. Those who stick with it and master the concepts are generally very happy with the language. But quite a few people get cold feet and give up before getting to that point, because it is so different from anything they have encountered before.