Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Kate 267 posts 610 karma points
    Sep 11, 2013 @ 15:19
    Kate
    0

    The same html in 2 different template

    Hi

    it is possible to write the html in a variable and then insert it into the templates.
    The point is to only write the Html once :-)

    <xsl:param name="currentPage"/>      

    <xsl:template match="/">
    <xsl:apply-templates select="$currentPage" />
    </xsl:template>

    <xsl:template match="Destination">
    <h2>Here I will insert some HTML</h2>
    </xsl:template>

    <xsl:template match="Destination/TekstSide">
    <h2>Here I will insert exactly the same HTML as on the Destination page</h2>
    </xsl:template>

    </xsl:stylesheet>

     

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 11, 2013 @ 16:09
    Chriztian Steinmeier
    100

    Hi Kate,

    If you're doing exactly that, you can use a single template:

    <xsl:template match="Destination | Destination/TekstSide">
      ...
    </xsl:template>
    

    But somehow I get the idea that you want to do more in one of them? :)

    /Chriztian

  • Kate 267 posts 610 karma points
    Sep 11, 2013 @ 17:24
    Kate
    0

    Nope, just for once Im making it simpel ;-) and it works perfectly. So Thanks for your help

    But now I have become curious. What if I want to do more in one of the templates. Is there an easy way to do that?

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 11, 2013 @ 19:02
    Chriztian Steinmeier
    1

    Haha - you asked for it :-)

    So here's a rundown of the "patterns" I use — yet there's still other ways to do it; usually depends on how much they differ, but here goes:

    Doing the exact same in more than one template

    Just join the element names with a pipe character in the match attribute:

    <xsl:template match="Destination | TekstSide">
        <!-- Do something -->
    </xsl:template>
    

    Extending the output of one template in another

    Give the reusable template a name attribute, which enables it to be called from another template, keeping its context (so when called from the Destination/TekstSide template, using @nodeName will refer to @nodeName on the TekstSide document):

    <xsl:template match="Destination" name="DestinationOutput">
        ...
    </xsl:template>
    
    <xsl:template match="Destination/TekstSide">
        <!-- Render the output of the Destination template -->
        <xsl:call-template name="DestinationOutput" />
    
        <!-- Output specific to TekstSide -->
        ...
    </xsl:template>
    

    Using a common output template with a mode attribute

    Works kind of the same as the above, but has the benefit of still using the matching rules, so you can still create a specific template for one/some of them:

    <xsl:template match="Destination | TekstSide">
        <section>
            <xsl:apply-templates select="." mode="output" />
        </section>
    </xsl:template>
    
    <xsl:template match="Destination/TekstSide">
        <div>
            <xsl:apply-templates select="." mode="output" />
        </div>
    </xsl:template>
    
    <xsl:template match="*" mode="output">
        <!-- Common output for TekstSide and Destination -->
    </xsl:template>
    
    <xsl:template match="TekstSide/TekstSide" mode="output">
        <!-- Specific output for TekstSide documents below another TekstSide (will still be wrapped in a <section>)  -->
    </xsl:template>
    

    Enjoy!

    (Remember it's still perfectly legal to have a single template with an <xsl:if> when the change is very simple :-)

    /Chriztian

  • Kate 267 posts 610 karma points
    Sep 11, 2013 @ 19:46
    Kate
    0

    Hi Chriztian

    It seems interesting out, I will try it tomorrow

    Thanks, for your help all day. It has been educational :-)

    /Kate

  • Kate 267 posts 610 karma points
    Sep 16, 2013 @ 11:49
    Kate
    0

    Hi Chriztian

    I'm sorry to bother you again, but you were right. It is not enough that I can insert the same html on two diffrent templat, I also want it to be recursiv :-)

    The content I insert on the template "Destination" must also be shown on the Template TekstSide.

    My xslt looks like this now:

    <xsl:template match="Destination | Destination/TekstSide">   

    <div class="destinationProject">
    <div class="cntDestinationProject">
    <h2>
    <xsl:value-of select="./headerProject"/>
    </h2>
    <div class="col col1 text-item">
    <h3><xsl:value-of select="./leftColHeader"/></h3>
    <xsl:value-of select="umbraco.library:ReplaceLineBreaks(./leftColText)" disable-output-escaping="yes" />
    </div>
    <div class="col col2 text-item">
    <h3><xsl:value-of select="./rightColHeader"/></h3>
    <xsl:value-of select="umbraco.library:ReplaceLineBreaks(./rightColText)" disable-output-escaping="yes" />
    </div>
    <div class="col img-item">
    <img src="{umbraco.library:GetMedia(./photo1, false())/umbracoFile}" alt="{./header}" class="projectImg1 rotate5deg" />
    <img src="{umbraco.library:GetMedia(./photo2, false())/umbracoFile}" alt="{./header}" class="projectImg2 rotate5deg" />
    <img src="{umbraco.library:GetMedia(./photo3, false())/umbracoFile}" alt="{./header}" class="projectImg3 rotate5deg" />
    &nbsp;
    </div>
    </div>
    </div>

    </xsl:template>

    I thought I could just write <xsl:value-of select="$Destination/headerProject" /> but that gives me an error.

    Do you have the time to help me again :-)

    /Kate

     


  • Kate 267 posts 610 karma points
    Sep 18, 2013 @ 09:30
    Kate
    0

    It works now.

    <xsl:template match="Destination | Destination/TekstSide">   

    <div class="destinationProject">
    <div class="cntDestinationProject">
    <h2>
    <xsl:value-of select="$currentPage/ancestor-or-self::*/./headerProject"/>
    </h2>
    <div class="col col1 text-item">
    <h3><xsl:value-of select="$currentPage/ancestor-or-self::*/./leftColHeader"/></h3>
    <xsl:value-of select="umbraco.library:ReplaceLineBreaks($currentPage/ancestor-or-self::*/./leftColText)" disable-output-escaping="yes" />
    </div>
    <div class="col col2 text-item">
    <h3><xsl:value-of select="$currentPage/ancestor-or-self::*/./rightColHeader"/></h3>
    <xsl:value-of select="umbraco.library:ReplaceLineBreaks($currentPage/ancestor-or-self::*/./rightColText)" disable-output-escaping="yes" />
    </div>
    <div class="col img-item">
    <xsl:if test="$currentPage/ancestor-or-self::*/./photo1 != ''">
    <img src="{umbraco.library:GetMedia($currentPage/ancestor-or-self::*/./photo1, false())/umbracoFile}" alt="{./@nodeName}" class="projectImg1 rotate5deg" />
    </xsl:if>
    <xsl:if test="$currentPage/ancestor-or-self::*/./photo2 != ''">
    <img src="{umbraco.library:GetMedia($currentPage/ancestor-or-self::*/./photo2, false())/umbracoFile}" alt="{./@nodeName}" class="projectImg2 rotate5deg" />
    </xsl:if>
    <xsl:if test="$currentPage/ancestor-or-self::*/./photo3 != ''">
    <img src="{umbraco.library:GetMedia($currentPage/ancestor-or-self::*/./photo3, false())/umbracoFile}" alt="{./@nodeName}" class="projectImg3 rotate5deg" />
    </xsl:if>

    &nbsp;
    </div>
    </div>
    </div>

    </xsl:template>

     

     

Please Sign in or register to post replies

Write your reply to:

Draft