Copied to clipboard

Flag this post as spam?

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


  • Kasper Dyrvig 246 posts 379 karma points
    Oct 21, 2010 @ 12:25
    Kasper Dyrvig
    0

    Use call-template or apply-templates?

    I would like to simplify some of my xslt. I think I can do that with call-template... or apply-templates...? Well, I'm a little confused here.

    My idea is that first I get the node, witch contains the data I need to view. (the presentation of the data is setup in tabs) Then I think it could be nice if I first had the basic code for the tabs. In a tab there should be a call-template/apply-templates that gets the content for the tab.

    In my mind it should look something like this:

    <xsl:template match="/">
        <div id="tabPanes">
          <div id="tabPane1">
            <xsl:call-template name="tab1"/>
          </div>
          <div id="tabPane2">
            <xsl:call-template name="tab2"/>
          </div>
        </div>
      </xsl:template>
      <xsl:template name="tab1">
        <h1>This is the content of tab 1</h1>
        <p>
          <xsl:value-of select="content1"/>
        </p>
      </xsl:template>
      <xsl:template name="tab2">
        <h1>This is the content of tab 2</h1>
        <p>
          <xsl:value-of select="content2"/>
        </p>
      </xsl:template>

    But don't really know how to do it and my experiments has failed until now.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 21, 2010 @ 20:48
    Lee Kelleher
    2

    Hi Webspas,

    Ideally you should use apply-templates when you have a node-set to apply a template against, and call-template when you have a template that you need to use, but doesn't match a node-set.

    There's a good explanation on StackOverflow about it: http://stackoverflow.com/questions/84422/xslt-performance-call-template-vs-apply-template

    Not sure how your example XSLT is related to Umbraco, but if there is anything specific that you want to do, then let us know!

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Oct 22, 2010 @ 12:44
    Kasper Dyrvig
    0

    My goal for this is to present the content the particular node in a way that is easy for the end-user to read. I think I have achieved that be designing and viewing it in tabs (e.g. "Introduction", "Facts", "Transport", "Media" and so on).

    But my current xslt code to get the result I want is very long and sometimes complicated. So am wondering if there is a way to make my xslt more easy to develop - and here my thought is that "slicing" the code might be the solution.

    But am I wrong? Is it "not go" to use call-template or apply-templates to reach my goal here?

  • Jeff Grine 149 posts 189 karma points
    Oct 22, 2010 @ 17:15
    Jeff Grine
    0

    I think you should use a template if you would wind up calling/applying it more than once, i.e. if it simplifies the code and reduces repetition. Otherwise your code will be just as long, but a little more comlicated.

  • Petr Snobelt 923 posts 1535 karma points
    Oct 22, 2010 @ 17:50
    Petr Snobelt
    0

    Hi, I don't read whole question, but try look at this presentation, he recomend apply templates (and know xslt well :-)
    http://pimpmyxslt.com/presentations/

    Petr

  • Kasper Dyrvig 246 posts 379 karma points
    Oct 25, 2010 @ 12:43
    Kasper Dyrvig
    0

    I have now made some changes in my xslt code, so it mahces my idea - and it works; I get the result I want... But is it okay or is it bad practice?

      <xsl:template match="/">
    <!--I HAVE SIMPLIFIED THIS CODE - IT'S NOT THE TOPIC-->
    <xsl:apply-templates select="$root//NodeType[Exslt.ExsltStrings:lowercase(@nodeName) = $qs]" />
      </xsl:template>

      <!--TABS TOP-->
      <xsl:template match="*[@isDoc] | node">
        <ul class="tabs">
          <li>
            <a id="t1" href="#">Start</a>
          </li>
          <li>
            <a id="t2" href="#">Information</a>
          </li>
          <li>
            <a id="t3" href="#">Facts</a>
          </li>
          <li>
            <a id="t4" href="#">Transport</a>
          </li>
          <li>
            <a id="t5" href="#">Weather</a>
          </li>
       
    </ul>

    <!--HERE COMES THE THINGS I'M WONDERING ABOUT-->
         <div class="panes">
            <xsl:call-template name="first"/>
          </div>
          <div class="pane">
            <xsl:call-template name="info"/>
          </div>
          <div class="pane">
            <xsl:call-template name="fact"/>
          </div>
          <div class="pane" id="pane4">
            <xsl:call-template name="transport"/>
          </div>
          <div class="pane">
            <xsl:call-template name="weather"/>
          </div>
        </div>
        <!--TABS TOP END-->
      </xsl:template>

    <!--TABS CONTENT-->
      <!--TAB 1-->
      <xsl:template match="*" name="first">
        <h1><xsl:value-of select="firstHeadline"/></h1>
    <xsl:value-of select="firstBody"/>
      </xsl:template>

    <!--AND SO ON...-->

    <!--TABS CONTENT END-->
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 25, 2010 @ 13:02
    Lee Kelleher
    0

    Hey Webspas,

    Bad practice? Nah, I don't think so... there are probably ways to make it a little more re-usable - but that depends on if each of your tabs hold the same HTML mark-up.

    I notice on "pane4" has its own ID ... so that's unique to that? Unless you want all the "panes" to have their own ID?

    There are many ways to do it.  If you want some examples, feel free to post your entire XSLT and we'll do a little refactoring? (all in the sake of XSLT "fun" LOL!)

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Oct 25, 2010 @ 15:58
    Kasper Dyrvig
    1

    Okay... you asked for it ;-)

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [
      <!ENTITY nbsp "&#x00A0;">
    ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

      <xsl:output method="html" omit-xml-declaration="yes"/>

      <xsl:param name="currentPage"/>
      <xsl:variable name="root" select="$currentPage/ancestor-or-self::root" />
      <xsl:variable name="qs" select="Exslt.ExsltStrings:lowercase(umbraco.library:RequestQueryString('name'))" />

      <xsl:template match="/">
        <xsl:choose>
          <xsl:when test="$qs != ''">
            <xsl:if test="normalize-space($qs)">
              <xsl:apply-templates select="$root//Area[Exslt.ExsltStrings:lowercase(@nodeName) = $qs]" />
            </xsl:if>
          </xsl:when>
          <xsl:otherwise>
            <!--Ingenting-->
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <!--TABS TOP-->
      <xsl:template match="*[@isDoc] | node">
        <ul class="tabs">
          <li>
            <a id="t1" href="#">Start</a>
          </li>
          <li>
            <a id="t2" href="#">Information</a>
          </li>
          <li>
            <a id="t3" href="#">Facts</a>
          </li>
          <li>
            <a id="t4" href="#">Transport</a>
          </li>
          <li>
            <a id="t5" href="#">Weather</a>
          </li>
        </ul>
        <div class="panes">
          <div class="pane" id="punchlinePicture">
            <xsl:attribute name="style">
              <xsl:if test="string(image) != ''">
                background-image: url('<xsl:value-of select="umbraco.library:GetMedia(number(image), 0)/umbracoFile"/>');
              </xsl:if>
            </xsl:attribute>
            <span id="punchlineText">
              <xsl:value-of select="punchline" />
            </span>
            <xsl:call-template name="first"/>
          </div>
          <div class="pane">
            <xsl:call-template name="info"/>
          </div>
          <div class="pane">
            <xsl:call-template name="fact"/>
          </div>
          <div class="pane" id="pane4">
            <xsl:call-template name="transport"/>
          </div>
          <div class="pane">
            <xsl:call-template name="weather"/>
          </div>
        </div>
        <!--TABS TOP END-->
      </xsl:template>

      <!--TABS CONTENT-->
      <!--TAB 1-->
      <xsl:template match="*" name="first">
        <xsl:call-template name="pister">
          <xsl:with-param name="front" select="'1'"/>
          <xsl:with-param name="number" select="'1'"/>
        </xsl:call-template>
      </xsl:template>

      <!--TAB 2-->
      <xsl:template match="*" name="info">
        <table cellpadding="0" cellspacing="0" class="pane2">
          <tr>
            <td id="tdLeft">
              <h5>
                <xsl:value-of select="name" />
              </h5>
              <xsl:if test="description != ''">
                <div>
                  <xsl:value-of select="description" disable-output-escaping="yes" />
                </div>
              </xsl:if>
            </td>
            <td>
              <xsl:if test="destinationPicture1 != ''">
                <xsl:call-template name="pictures">
                  <xsl:with-param name="number" select="'1'"/>
                </xsl:call-template>
              </xsl:if>
              <xsl:call-template name="pister">
                <xsl:with-param name="front" select="'0'"/>
                <xsl:with-param name="number" select="'2'"/>
              </xsl:call-template>
            </td>
          </tr>
        </table>
      </xsl:template>

      <!--TAB 3-->
      <xsl:template match="*" name="fact">
        <table cellpadding="0" cellspacing="0" class="pane3">
          <tr>
            <td id="tdLeft">
              <h5>
                Facts about <xsl:value-of select="name"/>
              </h5>
              <table>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <xsl:if test="place != ''">
                  <tr>
                    <td>Place: </td>
                    <td>
                      <xsl:value-of select="place"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="height != ''">
                  <tr>
                    <td>Height: </td>
                    <td>
                      <xsl:value-of select="height"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="alpin!= ''">
                  <tr>
                    <td>Alpin: </td>
                    <td>
                      <xsl:value-of select="alpin"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="long!= ''">
                  <tr>
                    <td>Long: </td>
                    <td>
                      <xsl:value-of select="long"/>
                    </td>
                  </tr>
                </xsl:if>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <xsl:if test="inAll != ''">
                  <tr>
                    <td>In all: </td>
                    <td>
                      <xsl:value-of select="inAll"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="lift1 != ''">
                  <tr>
                    <td>Lift: </td>
                    <td>
                      <xsl:value-of select="lift1"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="lift2 != ''">
                  <tr>
                    <td>Lift: </td>
                    <td>
                      <xsl:value-of select="lift2"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="lift3!= ''">
                  <tr>
                    <td>Lift: </td>
                    <td>
                      <xsl:value-of select="lift3"/>
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="kanon = 1">
                  <tr>
                    <td>Kanon: </td>
                    <td>
                      <img src="/media/1588/yes.png" alt="Yes" />
                    </td>
                  </tr>
                </xsl:if>
                <xsl:if test="down != ''">
                  <tr>
                    <td>Down: </td>
                    <td>
                      <xsl:value-of select="down"/>
                    </td>
                  </tr>
                </xsl:if>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <xsl:if test="activites != ''">
                  <tr>
                    <td colspan="2">
                      <b>Activites:</b>&nbsp;<xsl:value-of select="activites"/>
                    </td>
                  </tr>
                </xsl:if>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <xsl:if test="distance != ''">
                  <tr>
                    <td colspan="2">
                      <b>Aistance:</b>&nbsp;<xsl:value-of select="distance"/>
                    </td>
                  </tr>
                </xsl:if>
                <tr>
                  <td colspan="2">&nbsp;</td>
                </tr>
                <xsl:choose>
                  <xsl:when test="cardText = ''">
                    <xsl:if test="card != ''">
                      <tr>
                        <td colspan="2">
                          <a href="{card}" target="_blanck">Information about cards</a>
                        </td>
                      </tr>
                    </xsl:if>
                  </xsl:when>
                  <xsl:otherwise>
                    <tr>
                      <td colspan="2">
                        <b>Card:</b>&nbsp;<xsl:value-of select="cardText"/>&nbsp;
                        <xsl:if test="card != ''">
                          <a href="{card}" target="_blanck">More...</a>
                        </xsl:if>
                      </td>
                    </tr>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:choose>
                  <xsl:when test="rentalText = ''">
                    <xsl:if test="rental != ''">
                      <tr>
                        <td colspan="2">
                          <a href="{rental}" target="_blanck">Information about rental</a>
                        </td>
                      </tr>
                    </xsl:if>
                  </xsl:when>
                  <xsl:otherwise>
                    <tr>
                      <td colspan="2">
                        <b>Rental:</b>&nbsp;<xsl:value-of select="rentalText"/>&nbsp;
                        <xsl:if test="rental!= ''">
                          <a href="{rental}" target="_blanck">More...</a>
                        </xsl:if>
                      </td>
                    </tr>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:choose>
                  <xsl:when test="schoolText= ''">
                    <xsl:if test="school != ''">
                      <tr>
                        <td colspan="2">
                          <a href="{school}" target="_blanck">Information about schools</a>
                        </td>
                      </tr>
                    </xsl:if>
                  </xsl:when>
                  <xsl:otherwise>
                    <tr>
                      <td colspan="2">
                        <b>Schools:</b>&nbsp;<xsl:value-of select="schoolText"/>&nbsp;
                        <xsl:if test="school != ''">
                          <a href="{school}" target="_blanck">More...</a>
                        </xsl:if>
                      </td>
                    </tr>
                  </xsl:otherwise>
                </xsl:choose>
              </table>
            </td>
            <td>
              <xsl:if test="destinationPicture1 != ''">
                <xsl:call-template name="pictures">
                  <xsl:with-param name="number" select="'2'"/>
                </xsl:call-template>
              </xsl:if>
              <xsl:call-template name="pister">
                <xsl:with-param name="front" select="'0'"/>
                <xsl:with-param name="number" select="'3'"/>
              </xsl:call-template>
              <xsl:if test="pistemap != ''">
                <div class="map">
                  <img alt="Ski" style="width: 231px; height: 130px;">
                    <xsl:attribute name="src">
                      <xsl:value-of select="umbraco.library:GetMedia(number(pistekort), 0)/umbracoFile"/>
                    </xsl:attribute>
                  </img>
                  <p>
                    <a rel="prettyPhoto[pist5]" title="Map">
                      <xsl:attribute name="href">
                        <xsl:value-of select="umbraco.library:GetMedia(number(pistekort), 0)/umbracoFile"/>
                      </xsl:attribute>
                      See in full size
                    </a>
                  </p>
                </div>
              </xsl:if>
            </td>
          </tr>
        </table>
      </xsl:template>

      <!--TAB 4-->
      <xsl:template match="*" name="transport">
        <h5>
          Transport in <xsl:value-of select="name"/>
        </h5>
        <xsl:choose>
          <xsl:when test="transportText != ''">
            <xsl:value-of select="transportText" disable-output-escaping="yes"/>
          </xsl:when>
          <xsl:otherwise>
            <p>
              <i>No description yet.</i>
            </p>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>

      <!--TAB 5-->
      <xsl:template match="*" name="weather">
        <p><i>No weather yet.</i></p>
      </xsl:template>
      <!--TABS CONTENT END-->

      <!--FUNCTIONS-->
      <xsl:template match="*" name="pister">
        <xsl:param name="front"/>
        <xsl:param name="number"/>
        <xsl:choose>
          <xsl:when test="onlyLong = 0">
            <div class="pister">
              <xsl:if test="$front = '1'">
                <xsl:attribute name="id">
                  <xsl:value-of select="'front'"/>
                </xsl:attribute>
              </xsl:if>
              <table cellpadding="0" cellspacing="7">
                <tr>
                  <td colspan="8">
                    <h4>
                      Roads in
                      <xsl:value-of select="name" />:
                    </h4>
                  </td>
                </tr>
                <tr>
                  <xsl:if test="grn != ''">
                    <td class="green">
                    </td>
                    <td class="pistnr">
                      <xsl:value-of select="grn" />
                    </td>
                  </xsl:if>
                  <xsl:if test="bl != ''">
                    <td class="blue">
                    </td>
                    <td class="pistnr">
                      <xsl:value-of select="bl" />
                    </td>
                  </xsl:if>
                  <xsl:if test="rd != ''">
                    <td class="red">
                    </td>
                    <td class="pistnr">
                      <xsl:value-of select="rd" />
                    </td>
                  </xsl:if>
                  <xsl:if test="sort != ''">
                    <td class="black">
                    </td>
                    <td class="pistnr">
                      <xsl:value-of select="sort" />
                    </td>
                  </xsl:if>
                </tr>
                <xsl:if test="string(pistemap) != ''">
                  <tr>
                    <td colspan="8">
                      <a rel="prettyPhoto[pist{$number}]">
                        <xsl:attribute name="href">
                          <xsl:value-of select="umbraco.library:GetMedia(number(pistekort), 0)/umbracoFile"/>
                        </xsl:attribute>
                        See big map
                      </a>
                    </td>
                  </tr>
                </xsl:if>
              </table>
            </div>
          </xsl:when>
          <xsl:otherwise>
            <xsl:if test="string(pistekort) != ''">
              <div class="pister">
                <table cellpadding="0" cellspacing="7">
                  <tr>
                    <td colspan="8">
                      <h4>
                        Roads in
                        <xsl:value-of select="name" />:
                      </h4>
                    </td>
                  </tr>
                  <tr>
                    <td colspan="8">
                      <a rel="prettyPhoto[pist{$number}]">
                        <xsl:attribute name="href">
                          <xsl:value-of select="umbraco.library:GetMedia(number(pistekort), 0)/umbracoFile"/>
                        </xsl:attribute>
                        See bigger map
                      </a>
                    </td>
                  </tr>
                </table>
              </div>
            </xsl:if>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:template>
      <xsl:template match="*" name="pictures">
        <xsl:param name="number"/>
        <div class="pictures">
          <img alt="Image">
            <xsl:attribute name="src">
              <xsl:value-of select="umbraco.library:GetMedia(destinationPicture1, 0)/umbracoFile"/>
            </xsl:attribute>
          </img>
          <div style="float: left;">
            <p>
              <a rel="prettyPhoto[galleri{$number}]">
                <xsl:attribute name="href">
                  <xsl:value-of select="umbraco.library:GetMedia(destinationPicture1, 0)/umbracoFile"/>
                </xsl:attribute>
                Se flere billeder
              </a>
            </p>
          </div>
          <xsl:if test="destinationVideoLink != ''">
            <div style="float: right; text-align: right;">
              <p>
                <a rel="prettyPhoto[video{$number}]">
                  <xsl:attribute name="href">
                    <xsl:value-of select="destinationVideoLink"/>
                  </xsl:attribute>
                  Se video
                </a>
              </p>
            </div>
          </xsl:if>
          <div class="hidden">
            <xsl:if test="destinationPicture2 != ''">
              <a rel="prettyPhoto[galleri{$number}]">
                <xsl:attribute name="href">
                  <xsl:value-of select="umbraco.library:GetMedia(destinationPicture2, 0)/umbracoFile"/>
                </xsl:attribute>&nbsp;
              </a>
            </xsl:if>
            <xsl:if test="destinationPicture3 != ''">
              <a rel="prettyPhoto[galleri{$number}]">
                <xsl:attribute name="href">
                  <xsl:value-of select="umbraco.library:GetMedia(destinationPicture3, 0)/umbracoFile"/>
                </xsl:attribute>&nbsp;
              </a>
            </xsl:if>
            <xsl:if test="destinationPicture4 != ''">
              <a rel="prettyPhoto[galleri{$number}]">
                <xsl:attribute name="href">
                  <xsl:value-of select="umbraco.library:GetMedia(destinationPicture4, 0)/umbracoFile"/>
                </xsl:attribute>&nbsp;
              </a>
            </xsl:if>
            <xsl:if test="destinationPicture5 != ''">
              <a rel="prettyPhoto[galleri{$number}]">
                <xsl:attribute name="href">
                  <xsl:value-of select="umbraco.library:GetMedia(destinationPicture5, 0)/umbracoFile"/>
                </xsl:attribute>&nbsp;
              </a>
            </xsl:if>
            <xsl:if test="destinationPicture6 != ''">
              <a rel="prettyPhoto[galleri{$number}]">
                <xsl:attribute name="href">
                  <xsl:value-of select="umbraco.library:GetMedia(destinationPicture6, 0)/umbracoFile"/>
                </xsl:attribute>&nbsp;
              </a>
            </xsl:if>
          </div>
        </div>
      </xsl:template>
      <!--FUNCTIONS END-->
    </xsl:stylesheet>

    * Some of the text has been modifired.

     

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Oct 25, 2010 @ 16:09
    Lee Kelleher
    0

    Whoah! LOL! Had a quick look over it... each of the panes are quite unique - so I reckon that you are doing it the right way! :-)

    Cheers, Lee.

  • Kasper Dyrvig 246 posts 379 karma points
    Oct 25, 2010 @ 16:43
    Kasper Dyrvig
    0

    haha... Well, thanks Lee

Please Sign in or register to post replies

Write your reply to:

Draft