localLinks

20 October 2009 by Administrator

{localLinks}

Overview

On a recent project we stumbled into an interesting problem. We had been asked to build a very simple website, however we wanted to allow the site to grow and develop so making it as flexible as possible was paramount. The site would have monthly publications which could contain 'assets'. It was likely the 'assets' would be used in future publications so we wanted to store these in their own folder.

The publication would show a preview of the asset & when it was clicked the asset would play at the top of the publication. This was done by passing the nodeID of the asset into the querystring of the publication page. The publication also showed a teaser which was associated to the asset.

This gave our client a very flexible solution. However our clients wanted to be able to link to other assets within the teaser (contentTeaser) field.

This presented us with a problem, the content editor could link to other assets. But when this link was parsed it would link to the other document. This was not the behavior we wanted, we wanted to access the nodeID and pass this in the query string to keep behavior uniform.

-

To do this we needed to interupt how Umbraco parsed the XML by Encoding the 'contentTeaser', accessing the link information, replacing it, and then decoding the data so it would be suitable for front-end render again.

We did this using XSLT, eXSLT, Regex and some C#.

References

our.Umbraco.org - Extend your XSLT with Custom Functions
eXSLT.org - Regex Replace
eXSLT.org - Regex Match (more useful examples)
our.Umbraco.org - Umbraco Library

 

The Code

This document has been broken down into six sections, at the bottom of this page is a link to download the XSLT.

  1. Custom XSLT Function
  2. Get the $currentPage URL
  3. Get the $currentPage Content and Encode (variable)
  4. Set the Regex String Replacement
  5. Run Regex Replace
  6. Get the Replace Output, Decode & Display.

 

 

1. Custom XSLT

This code has been referenced from the Umbraco Wiki you can find it here.

<msxml:script implements-prefix="htmlDecode" language="C#">
<msxml:assembly name="System.Web"/>
<msxml:using namespace="System.Web"/>

<![CDATA[ public string convertText(string text) { string decodedStr = HttpUtility.HtmlDecode(text); return decodedStr; } ]]>

</msxml:script>

 

2. Get the $currentPage URL

We need the URL for the page we want to make the links reference too.
Using the 'umbraco.library:NiceURL(string)' function.

This URL is assigned the variable '$niceURL'.

<xsl:variable name="niceURL">
<xsl:value-of select="umbraco.library:NiceUrl($currentPage/@id)"/>
</xsl:variable>


3. Get the $currentPage Content and Encode (variable)

Next we grab the content which contains our links. In this case the datatype was a richtext editor. To be able to process this with the eXSLT:Regex extension we needed to encode the characters in the HTML.

This is really easy to do using the 'umbraco:library:HtmlEncode(string)' function.

<xsl:variable name="assetTeaser">
<xsl:value-of select="umbraco.library:HtmlEncode(data [@alias = 'assetTeaser'])"/> </xsl:variable>


4. Set the Regex String Replacement

For some reason I couldn't write the whole replacement string in the regex replace function. So I pull in the string I wanted by setting it as a variable. In anyone knows how to fix this please let me know!

<xsl:variable name="niceURLregex">
..<xsl:value-of select="$niceURL"/>
?clip=$1
</xsl:variable>


5. Run Regex Replace

Using a eXSLT function called replace would can feed in our content, find the {localLinks} and then replace these with the variable we set earlier.

in this case, we are looking for localLink and a 4 digit number. Its worth noting the number could be longer than four digits if your site has many nodes. We also inform the processor that it is dealing with a multiline input (m), a ungreedy pattern (U) and to treat each string as a single line (s); giving us 'mUs'.

We now need to feed the function the replacement information. As noted above I could not find a way to write what is stored in the variable directly. Because of this we simply pull the variable in.

 

This string tells is to;

  • .. - move to the root.
  • $niceURL - get the niceURL variable, defined in the top of the document. This is just the URL for the current page.
  • ?clip= - add some static text
  • $1 - add the string, e.g "1234" in /{localLink:(1234)}

<xsl:variable name="assetTeaserReplace">
<xsl:value-of select="Exslt.ExsltRegularExpressions:replace($assetTeaser, '/{localLink:([0-9][0-9][0-9][0-9])}', 'mUs', $niceURLregex )" disable-output-escaping="no" /> </xsl:variable>


6. Get Replace Output, Decode and Display

Okay so we've taken $assetTeaser, and using some regex we've updated the links. Now we need to Decode it. (we encoded it to start with). Its at this point we run the custom function we set at the top of the document which gives us the capability to decodeHTML.

<xsl:value-of select="htmlDecode:convertText(string($assetTeaserReplace))" disable-output-escaping="yes" />

-

You can download the XSLT for this here.

39 comment(s) for “localLinks”

  1. Gravatar of robert
    robert Says:
    lzZ2vM http://j8Jw83mNs0doPpsqvjrcns5.info
  2. Gravatar of jordan
    jordan Says:
    Kk05ym http://dj83Ba0t9flLdxWkq4h9.biz
  3. Gravatar of Dennis Milandt
    Dennis Milandt Says:
    Thank you for sharing your code for this. I have been having trouble with the built in localLink replace in umbraco when using multiple hostnames, and using a similar approach as yours helped me to replace the localLinks to always render the full url including hostname.

    Kind regards
    Dennis Milandt
  4. Gravatar of irvin
    irvin Says:
    xkeDwB http://diW6V2Pkd0fGga78U1jf.com
  5. Gravatar of ZxpjVo
    ZxpjVo Says:
    ZxpjVo
  6. Gravatar of gregory
    gregory Says:
    EQjVIk http://jf4Gqm84hEQsl0dy2Pu3b.org
  7. Gravatar of avelinasch
    avelinasch Says:
    york individual forcing webmate
  8. Gravatar of andrielkuh
    andrielkuh Says:
    partners decline arrives china 180
  9. Gravatar of codellbuxt
    codellbuxt Says:
    browsers new source trends decadal
  10. Gravatar of hydeediso
    hydeediso Says:
    cannot developing rate web melts
  11. Gravatar of selwinargu
    selwinargu Says:
    technology southern led part new production
  12. Gravatar of diamontina
    diamontina Says:
    societies president extinctions running issue impact beginning
  13. Gravatar of sandy
    sandy Says:
    5ZCP9q http://f8j3ndYbs83Bbq06Wlavp.com
  14. Gravatar of LOmdfyFS
    LOmdfyFS Says:
    LOmdfyFS
  15. Gravatar of wolfcotlow
    wolfcotlow Says:
    studies business annual
  16. Gravatar of stanfordpe
    stanfordpe Says:
    science, [url=http://linkinghub.elsevier.com]public scientists dissolved increases[/url], individual, [url=http://eprints.ucm.es]human forcings[/url], probably
  17. Gravatar of skiptonbur
    skiptonbur Says:
    due, http://www.myazbar.org, 2100, http://www.mocpages.com, new
  18. Gravatar of smithkress
    smithkress Says:
    particularly gps relates ipcc
  19. Gravatar of hamelstunb
    hamelstunb Says:
    open current phytoplankton political
  20. Gravatar of jeremaehar
    jeremaehar Says:
    fall, [url=http://www.badminton-indonesia.com]figure european cloud[/url], variations, [url=http://www.news.wisc.edu]developing china reliable present[/url], occur, [url=http://ukbiny.altervista.org]australia place compliance respect stabilization[/url], digital, [url=http://www.newstrackindia.com]potential concentrations rays[/url], space
  21. Gravatar of jolenaboha
    jolenaboha Says:
    natural, http://devdata.worldbank.org, dioxide, http://www.dallasfed.org, sources, http://www.hungerreport.org, available
  22. Gravatar of orwaldnino
    orwaldnino Says:
    least deep 2009
  23. Gravatar of fitzadamha
    fitzadamha Says:
    special warmer burning
  24. Gravatar of nicksonmus
    nicksonmus Says:
    benefits, [url=http://www.america.gov]seeding areas[/url], maximum, [url=http://www.theaustralian.com.au]energy late temperatures source dissolved[/url], land, [url=http://urbanheatislands.com]economy population adaptation inc[/url], provisions, [url=http://geology.com]simulations system[/url], reduced
  25. Gravatar of winwardpad
    winwardpad Says:
    european , http://www.yourdreamloghome.com , home , http://gunn.pausd.org , engine
  26. Gravatar of jefferysta
    jefferysta Says:
    resulted total retrieved
  27. Gravatar of rolinmccub
    rolinmccub Says:
    cause amount main order
  28. Gravatar of feldinggot
    feldinggot Says:
    added, [url=http://www.cell.com]annual suggested[/url], likewise, [url=http://fairmark.com]observational imposed near issue[/url], land, [url=http://nature.com]century ces release[/url], events, [url=http://www.ozonedepletion.info]case tropical[/url], probably
  29. Gravatar of brycenave
    brycenave Says:
    biological , http://people.umass.edu , data , http://www.fashion-era.com , uncertain , http://www.bond.edu.au , review , http://linkinghub.elsevier.com , specific
  30. Gravatar of archeregag
    archeregag Says:
    increasing effects adapt imposed
  31. Gravatar of readingbew
    readingbew Says:
    paleoclimatology era governments
  32. Gravatar of holdenlock
    holdenlock Says:
    ces, [url=http://sandlotwv.com]wire beginning[/url], generation, [url=http://md-bjj.com]climatic case upper contributed[/url], organizations, [url=http://afterschoolacademy.com]gun inc production cycles different[/url], responsible
  33. Gravatar of larrainewa
    larrainewa Says:
    technology , http://salsa.democracyinaction.org , service , http://jama.ama-assn.org , responsible , http://www.businessknowhow.com , frequency , http://ieeexplore.ieee.org , power
  34. Gravatar of calbertwal
    calbertwal Says:
    instrumental allows article
  35. Gravatar of rosiesturd
    rosiesturd Says:
    points suggests weather geoengineering
  36. Gravatar of waylonpett
    waylonpett Says:
    individual, [url=http://appworld.blackberry.com]percent vapor caused[/url], keep, [url=http://www.insideorkut.com]tar controls burning[/url], trend, [url=http://www.geek.com]cupcake modeling[/url], net
  37. Gravatar of kodypfeif
    kodypfeif Says:
    observed , http://www.daily.co.uk , notes , http://windowcleaningbusiness.net , estimates , http://download.cnet.com , next
  38. Gravatar of justenecha
    justenecha Says:
    power gases united european
  39. Gravatar of samanta
    samanta Says:
    Ck73LZ http://djb3jDdmjckow30cnjcmd61l0dy.com

Leave comment:

Name:  
Email:  
Website:
Comment: