Blog

DIY tips: XSLT template modes

2007-09-10 11:09:33 by Martynas Jusevičius

Working with Views, sometimes you need the same kind of objects rendered in different ways, depending on the situation. For example, you might want to be able to display an array of products as a list as well as a table. That means each product item should be rendered either as a list item or as a table row.
As you define templates for product items in XSLT, you need a way to differentiate between the two kinds. Here is where template modes come in handy. You can define templates with the same match but different modes, and choose the mode later when applying them. That is excatly what we need — product item templates with list and table modes:

<xsl:template match="Product" mode="list">
	<li>
		<a href="{@resource}{$query-string}">
			<xsl:value-of select="Title"/> (<xsl:value-of select="@price"/>)
		</a>
	</li>
</xsl:template>

<xsl:template match="Product" mode="table">
	<tr>
		<td>
			<xsl:value-of select="@code"/>
		</td>
		<td>
			<a href="{@resource}{$query-string}">
				<xsl:value-of select="Title"/>
			</a>
		</td>
		<td>
			<xsl:value-of select="@price"/>
		</td>
	</tr>
</xsl:template>

They can be applied as follows:

<ul>
	<xsl:apply-templates select="/Products/Product" mode="list"/>
</ul>

<tbody>
	<xsl:apply-templates select="/Products/Product" mode="table"/>
</tbody>

Digg Digg this! del.icio.us del.icio.us!

Comments (1)

thanks

2010-07-26 11:45:56 by ghd on sale

your information will be added to a list which will be published on the website under People/Students.

New comment






No HTML allowed.