1行中的动态行数

时间:2013-03-14 14:21:49

标签: xslt twitter-bootstrap

我想将多个类别分隔成行,一行中的类别数量将基于$catsInLine$catsInLine是从1到12的数字。每个类别将由DIV包装,类别从span1到span 12.并且类别将再次换行,每行<div class="row-fluid">

我在下面试了好几个小时,没有运气得到正确的结果。

<div class="row-fluid">
    <xsl:variable name="catsInLine">
        <xsl:value-of select="categories_in_line" />
    </xsl:variable>
    <xsl:variable name="column">
        <xsl:value-of select="(12 div $catsInLine)" />
    </xsl:variable>
    <xsl:for-each select="categories/category">
        <xsl:if test="position() mod $catsInLine = 1 or position() = 1">
            <xsl:text disable-output-escaping="yes">&lt;div class="row-fluid"&gt;</xsl:text>
        </xsl:if>
        <div class="span{$column}">
            <xsl:call-template name="category" />
        </div>
        <xsl:if test="position() mod $catsInLine = 0 or position() = last()">
            <xsl:text disable-output-escaping="yes">&lt;&#47;div&gt;</xsl:text>
        </xsl:if>
    </xsl:for-each>
</div>

假设$catsInLine2,我希望得到的结果是

<div class="row-fluid">
    <div class="span6">
        <a>Category1</a>
    </div>
    <div class="span6">
        <a>Category2</a>
    </div>
</div>
<div class="row-fluid">
    <div class="span6">
        <a>Category3</a>
    </div>
    <div class="span6">
        <a>Category4</a>
    </div>
</div>

如果$catsInLine1,则结果应为

<div class="row-fluid">
    <div class="span12">
        <a>Category1</a>
    </div>
</div>
<div class="row-fluid">
    <div class="span12">
        <a>Category2</a>
    </div>
</div>
<div class="row-fluid">
    <div class="span12">
        <a>Category3</a>
    </div>
</div>
<div class="row-fluid">
    <div class="span12">
        <a>Category4</a>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

请试试这个:

<xsl:apply-templates select="categories/category[position() mod $catsInLine = 1 or
                                                 $catsInLine = 1]" 
                     mode="row" />

<!-- Separate template -->
<xsl:template match="category" mode="row">
  <div class="row">
    <xsl:apply-templates 
              select=". | following-sibling::category[position() &lt; $catsInLine]" />
  </div>
</xsl:template>