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]">
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>
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.
Andthen Imust saythat Chriztianis a master ofxslt:)
@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.
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:
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]">
Hi eaus,
I think this xslt umbraco library method DateGreaterThanOrEqualToday can help you with your problem.
You can read more about the DateGreaterThanOrEqualToday method here: http://our.umbraco.org/wiki/reference/umbracolibrary/dategreaterthanorequaltoday
Hope this can help you out.
/Dennis
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:
/Chriztian
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
@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
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):
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:
EDIT: I tried to compare artDate with 2013-04-15T00:00:00 as well, without much luck.
Hi easus,
My bad - there's a typo in that line; it should read (changed to artDate as well):
(The final closing square bracket was placed to early...)
/Chriztian
is working on a reply...