在我的 Umbraco CMS 站点中,我正在制作一个供内容编辑器使用的节点“小部件”列表,其中列出了他们可以切换以更改显示的许多选项。这通常涉及用锚、div 或其他东西包装元素。使用 XSLT 从 XML 输出中显示这些内容,因为我是一个非常新的 XSLT 初学者,所以我已经把这些方法放在一起了。
我得到的解决方案是多个嵌套的应用模板。这会创建大量条件,通常会要求重复检查,这些条件非常庞大。管理起来有点噩梦。
它看起来像这样(但每个选择都有两个以上的选项):
<xsl:template match="/">
<xsl:choose>
<xsl:when test="type='1'">
<xsl:apply-templates select="widgetContent" mode="type_1" />
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="widgetContent" mode="type_default" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="wigetContent" mode="type_1">
<xsl:choose>
<xsl:when test="./wrap_with_hyperlink != 0">
<xsl:element name="a">
<xsl:apply-templates select="." mode="hyperlink_wrapped" />
</xsl:element>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="widgetContent" mode="not_hyperlink_wrapped" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
我能做些什么来减少这种纠结的混乱?我已将条件尽可能自上而下地构建,但肯定存在重复检查,其中 type_2 必须再次询问与 type_1 相同的问题。
(编辑:清晰)因为设计基本上是一个洋葱,type_1 被某些标签包裹,type_2 被不同的标签包裹。下一层,两者都可以用相同的标签包裹,依此类推。完美的是:
<xsl:if test="this_wrap_style = 1"><xsl:element name="abc"></xsl:if>
<xsl:if test="this_wrap_style = 2"><xsl:element name="xyz"></xsl:if>
(everything else)
</abc> //if it exist.
</xyz> //etc
这绝对行不通。
通过为不同的小部件控件使用 Umbraco Doc Types 已经减少了其中一些,但部分性质是内容编辑器的理想结构是选择一个框小部件将为他们提供 5 种不同类型的小部件框(或更多)可供选择从,并且连贯的后端并不那么重要。
谢谢大家的时间。