Copied to clipboard

Flag this post as spam?

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


  • higgsy 65 posts 92 karma points
    Feb 11, 2012 @ 16:46
    higgsy
    0

    Re-using uComponents url picker in Xslt

    Hi all,

    I'm going to start by apologising for using any incorrect terminoligy - first week using Umbraco after being thoroughly fedup with another .NET CMS!

    I've installed the uComponents package and im using the Url picker. I'm now trying to write a simple Xslt macro that can be used multiple times on any given page. The following works, but you will notice it depends on the name of the field in the doctype being called area1Link - well what happens if I want to use the same macro more than once on a page?

    <?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" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary"

      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">

     

     

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

     

    <xsl:param name="currentPage"/>

     

        <xsl:template match="/">

     

        <a>

    <xsl:attribute name="href">

    <xsl:value-of select="$currentPage/area1Link/url-picker/url" />

    </xsl:attribute>

     

    <xsl:if test="$currentPage/area1Link/url-picker/new-window[. = 'True']">

    <xsl:attribute name="target">_blank</xsl:attribute>

    </xsl:if>

     

    Website link

    </a>

     

        </xsl:template>

    </xsl:stylesheet>

    I'm presuming that I need to extend the macro to pass a fieldname to it, but im not sure how to do that. I can see how to configure Umbraco to prompt you for a macro parameter, but how would I read it in the macro and get the value from the correct field name.

    Any help anyone can provide would be greatly appreciated.

    thanks in advance

    Al

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Feb 11, 2012 @ 20:56
    Tom Fulton
    1

    Hi,

    First step is to create a parameter on the macro so you can pass in the field name.  Do this by going to Developer -> Macro -> (your macro) -> Parameters tab, and add a paramater (ie fieldAlias) of type Text.  Then go back to your template and re-insert the macro, it will prompt you to enter the filedname.

    Then here's a rough example of how to pull that into your XSLT file:

      <xsl:param name="currentPage"/>
      <xsl:variable name="fieldAlias" select="/macro/fieldAlias" /> <!-- Grabs the fieldAlias macro param in a variable -->
      <xsl:variable name="fieldValue" select="$currentPage/* [not(@isDoc)][name() = $fieldAlias]" /> <!-- Grabs the value of a field matching $fieldAlias on the current page -->

        <xsl:template match="/">
          <a>
    <xsl:attribute name="href">
              <xsl:value-of select="$fieldValue/url-picker/url" />
    </xsl:attribute>
    <xsl:if test="$fieldValue/area1Link/url-picker/new-window[. = 'True']">
       <xsl:attribute name="target">_blank</xsl:attribute>
    </xsl:if>
    Website link
          </a>
        </xsl:template>

    HTH,
    Tom 

  • higgsy 65 posts 92 karma points
    Feb 12, 2012 @ 13:26
    higgsy
    0

    Tom,

    Thanks so much mate - works like a treat!

    Cheers,

    Al

Please Sign in or register to post replies

Write your reply to:

Draft