XSLT: How to reuse a template within another template

xml, xslt

Solution

Try using this (off the top of my head):

<xsl:call-template name="button">
    <xsl:with-param name="name" value="Create" />
    <xsl:with-param name="href" value="/create" />
</xsl:call-template>

You'll also need to declare your two parameters within your button template using `<xsl:param ...>`.

Problem

If I have a template as follows, which is used to create a button: ``` <xsl:template match="button" name="button"> <a class="button" href="{@href}"> <xsl:value-of select="@name"/> </a> </xsl:template> ``` I want to be able to use that button in another template, like this: ``` <xsl:template match="createForm"> ... <button name="Create" href="/create"/> </xsl:template> ``` However, this will just output the button tag as-is. I would like it to be processed through the existing button template. How can this be achieved? -- Thanks David M for your answer. Here is what I have now for the button template: ``` <xsl:template match="button" name="button"> <xsl:param name="name" select="@name"/> <xsl:param name="href" select="@href"/> <a class="button" href="{$href}"> <xsl:value-of select="$name"/> </a> </xsl:template> ``` The createForm template now looks like this: ``` <xsl:template match="createForm"> ... <xsl:call-template name="button"> <xsl:with-param name="name" select="'Create'"/> </xsl:call-template> </xsl:template> ```

Original source