Copied to clipboard

Flag this post as spam?

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


  • Adrian Blake 10 posts 40 karma points
    Feb 18, 2014 @ 17:44
    Adrian Blake
    0

    Checking field string length?

    Hi, this seems extremely simple but a Google search shows me about 20 different and varied examples which i can't get any to work. I am just trying to check a parameter for my text pages, if the value exists then add some javascript code to redirect the user. Here is my example code:

     

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <xsl:if test="string-length($currentPage/externalRedirect)!=0">
    <script language="javascript">
    document.location.replace("<umbraco:Item field="externalRedirect" runat="server"></umbraco:Item>");
    </script>
    </xsl:if>
    </xsl:template>

    the IF TEST always executes as TRUE so no matter what, it adds the redirect code even when there is no value and that just puts the page in a loop. Please someone help me i have been programming for decades but i am new to XSLT and this is embarassing that i can't get something this easy to work. Please help.

    Thanks,

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 18, 2014 @ 19:37
    Dennis Aaen
    0

    Hi Adrian,

    This should work for you. Remember that when you write the URL you will redirect to in the Umbraco field you need to remeber the http://, so like should be like e.g http://www.google.com

    <?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:Examine="urn:Examine" xmlns:Flexit.PdfThumbnails="urn:Flexit.PdfThumbnails"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets Examine Flexit.PdfThumbnails ">


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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">   
       
    <xsl:if test="string-length($currentPage/externalRedirect)!=0">

    <script>
        document.location.replace("<xsl:value-of select="$currentPage/externalRedirect" />");
    </script>

    </xsl:if>

    </xsl:template>

    </xsl:stylesheet>

    /Dennis

  • Adrian Blake 10 posts 40 karma points
    Feb 18, 2014 @ 20:01
    Adrian Blake
    0

    Hmm okay thanks for the reply Dennis, it looks like i am missing some fundamental information here because when i add the code to my Textpage template it shows the code in my HTML source and doesn't execute the code. Is it even possible to add XSLT code to a template or should i be using Razor? Sorry for the questions i am very new to this and I appreciate your help. 

    You can see my example where the XSLT is displayed in html soruce code here: http://jessesjourney.com/who-we-are/brochure.aspx

     

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 18, 2014 @ 20:44
    Dennis Aaen
    100

    Hi Adrian,

    Okay I will try to guide you step by step so we are get it work for you.

    1. Add the field on the document type for e.g frontpage. Go the setting section --> Document type (I have made a screenshot)

    2. Then we need to create a XSLT file. Go to developer section, then right click on the XSLT folder. Give the file a name (e.g Redirect) and choose clean. There are some build in templates for e.g navigation and so on. When you create the file don´´t remove the tick in Create Macro.

    When you have created the XSLT plaste in this code, and then save the file:

    <?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:Examine="urn:Examine" xmlns:Flexit.PdfThumbnails="urn:Flexit.PdfThumbnails"
        exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets Examine Flexit.PdfThumbnails ">

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

    <xsl:param name="currentPage"/>

    <xsl:template match="/">   
       
    <xsl:if test="string-length($currentPage/externalRedirect)!=0">

    <script>
        document.location.replace("<xsl:value-of select="$currentPage/externalRedirect" />");
    </script>
    </xsl:if>

    </xsl:template>

    </xsl:stylesheet>

    3. Go back to the settings section choose the templates forlder and choose the template where you what the redirect. It could be on the frontpage. (In my example I have choosen the frontpage)

     

    Then click at the button or the click at the arrow that i have marked. When you done that you chould get at list of your macros, could be XSLT or Razor. There you need to find the macro with the name you gave your XSLT when you created it.

     

    When click okay and save the template. After that this should be added in your template.

    4. Now go to content page and choose a page with the field where you can add the URL. In my example I added the field to the frontpage, and now I write the URL that the page should redirect to. (See sreenshot)

    I hope these screenshot will make it easier to you to get the redirect working.If you have any question about this keep asking. Since you´re relatively new to Umbraco I thik that you shold take a look at this:

    http://our.umbraco.org/wiki/reference/xslt

    http://our.umbraco.org/documentation/Reference/Templating/managing-templates

    If you like video tutorials I will recommend you to see this free videos: http://umbraco.tv/videos/umbraco-v7/implementor/ Although these are for Umbraco 7, is most of the basic principles in Umbaco, which also acts in the previous versioer. except the Razor path that will need (Umbraco 4.7 or later if I remember correctly).

    Here is some video tutorials about XSLT, but this need that you subscribe to the Umbraco.tv. http://umbraco.com/help-and-support/video-tutorials/umbraco-fundamentals/xslt-in-umbraco-45.aspx

    I hope this helps you, and make sense.

    /Dennis

  • Adrian Blake 10 posts 40 karma points
    Feb 18, 2014 @ 20:57
    Adrian Blake
    0

    Hi Dennis,

    Thank you so much for your step by step instructions, i had the parameter already added to my document type so creating the seperate macro and inserting it into the template worked perfectly! Thanks again for all your help.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Feb 18, 2014 @ 21:16
    Dennis Aaen
    0

    Hi Adrian,

    Great you got it working.

    /Dennis

Please Sign in or register to post replies

Write your reply to:

Draft