med*_*iev 2 php xslt templating
我正在使用某个框架,其中主要开发人员正在考虑从基于PHP的本机模板到XSLT模板的彻底改变.
我担心这不可行,因为在我们的网站上我们通常有非常复杂的模板逻辑.
对于像这样简单的事情:
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 } ?>
Run Code Online (Sandbox Code Playgroud)
等效的XSLT将是:
<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>
Run Code Online (Sandbox Code Playgroud)
有了这么简单的代码片段,当我想到高级场景时,它会让我感到害怕.
我想知道是否有人经历过类似的模板转换,如果是这样,你是如何应对它的?你回去了吗?
在XSLT 1.0中,您建议的代码可以简化为:
<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>
Run Code Online (Sandbox Code Playgroud)
在XSLT 2.0中,它可以进一步简化为:
<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>
Run Code Online (Sandbox Code Playgroud)
这让我觉得比你原来的PHP好多了.
对于更一般的问题,XSLT确实有一个陡峭的学习曲线.那些坚持并掌握概念的人通常对语言非常满意.但是很多人在达到这一点之前就会冷落并放弃,因为它与以前遇到的任何事情都有所不同.