Copied to clipboard

Flag this post as spam?

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


  • eaus 9 posts 39 karma points
    Apr 11, 2013 @ 10:00
    eaus
    0

    Look for node with today's date, otherwise show default-message

    Hi

    On an old Umbraco 4.0.3 site, I am working on a "Today's dinner special"-feature, and I need to make a fail-safe so that if it hasn't been published (yet) - it will just show a default message instead.

    So basically I need to check a node from childs containg todays date in the "artDate"-field, and if match = 1 it will publish the "artContent"-field, otherwise it will publish a standard "Today's dinner is a surprise!"-message or something.

    I added the following code to let you guys know what I'm currently playing around with.. any help will be greatly appriciated!

    <xsl:for-each select="umbraco.library:GetXmlNodeById($source)/node [string(data [@alias='umbracoNaviHide']) != '1' and @nodeTypeAlias='docNewsArticle' and umbraco.library:ShortDate(data [@alias = 'artDate']) = $todaysdate]">
  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 11, 2013 @ 10:28
    Dennis Aaen
    1

    Hi eaus,

    I think this xslt umbraco library method DateGreaterThanOrEqualToday can help you with your problem.

    <xsl:for-each select="$currentPage//node/data [@nodeTypeAlias='whataver' and umbraco.library:DateGreaterThanOrEqualToday(./data [@alias='dateField'])] " />

    You can read more about the DateGreaterThanOrEqualToday method here: http://our.umbraco.org/wiki/reference/umbracolibrary/dategreaterthanorequaltoday

    Hope this can help you out.

    /Dennis

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    Apr 11, 2013 @ 10:41
    Chriztian Steinmeier
    101

    Hi easus,

    Try this out - it looks a bit more complicated because of the old schema, and because it handles the dates as strings - but using some intermeddiate variables (like the $children variable) you can have the XPaths be pretty readable:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umbraco.library="urn:umbraco.library"
        exclude-result-prefixes="umbraco.library"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
    
        <xsl:variable name="today" select="umbraco.library:CurrentDate()" />
        <xsl:variable name="children" select="$currentPage/node[not(data[@alias = 'umbracoNaviHide'] = 1)]" />
    
        <xsl:template match="/">
            <!-- Find the one set for today -->
            <xsl:variable name="todaysSpecial" select="$children[substring(data[@alias = 'dateField'], 1, 10)] = substring($today, 1, 10)" />
    
            <xsl:apply-templates select="$todaysSpecial" />
    
            <xsl:if test="not($todaysSpecial)">
                <p>Today's dinner is a surprise!</p>
            </xsl:if>
        </xsl:template>
    
        <!-- Template for the Special -->
        <xsl:template match="node">
            <p>
                Today's special is "<xsl:value-of select="@nodeName" />"
            </p>
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 11, 2013 @ 10:55
    Dennis Aaen
    0

    Hi eaus,

    I have not read your question correctly, sorry for that.

    I my suggestion will only work, if you have to test, if the date is greater or equal the day to today. And you only have to check if the date was equal the the date today.

    And then I must say that Chriztian is a master of xslt :)

    /Dennis

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    Apr 11, 2013 @ 11:06
    Chriztian Steinmeier
    0

    @Dennis: Thanks :-) I hope you keep remembering that you yourself provide good feedback, regardless if the solution works or not. Many (including myself) learn a lot more from getting a link to something that helps them find a solution, than from being given a working solution.

    /Chriztian

  • eaus 9 posts 39 karma points
    Apr 12, 2013 @ 12:04
    eaus
    0

    Thanks for the quick feedback, unfortunately I still can't make it work.

    Copy/pasting your code gives me the following error when saving (wheter or not i change dateField to artDate which is the actual name for my field):

    Error in XSLT at line 20, char 17
    18:       <xsl:variable name="todaysSpecial" select="$children[substring(data[@alias = 'dateField'], 1, 10)] = substring($today, 1, 10)" />
    19:       
    20: >>>   <xsl:apply-templates select="$todaysSpecial" /> <<<
    21:       
    22:       <xsl:if test="not($todaysSpecial)">

    I can get the macro to save by chaning line 18 to the following, but it still only says "Todays dinner is a surprise!", even though I (should) have a match for that date:

     <xsl:variable name="todaysSpecial" select="$children[data[@alias = 'artDate'] = '2013-04-12']" />

    EDIT: I tried to compare artDate with 2013-04-15T00:00:00 as well, without much luck.

     

  • Chriztian Steinmeier 2798 posts 8787 karma points MVP 7x admin c-trib
    Apr 15, 2013 @ 01:19
    Chriztian Steinmeier
    0

    Hi easus,

    My bad - there's a typo in that line; it should read (changed to artDate as well):

    <xsl:variable name="todaysSpecial" select="$children[substring(data[@alias = 'artDate'], 1, 10) = substring($today, 1, 10)]" />

    (The final closing square bracket was placed to early...) 

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft