Tuesday, February 2, 2010

Get Month from a DateTime Field in Sitecore

Using sc:formatdate(), is a sweet and simple method to format a date in a Sitecore rendering. 

For Example:
sc:formatdate(sc:fld('MyDateField', .),'d MMM yyyy @ h:mm')

Using the format string 'MMM' gives us the three-letter month names, while the 'MM' gives months in the numeric format (eg. 02 for Feb), and 'M' gives us the  full month name along with the date.

Another way is using the substring-after in conjunction with the format '/M'. 

For Example:
substring-after(sc:formatdate(sc:fld('MyDateField', .), '/M'), '/')

Tuesday, January 19, 2010

Working with Session inside XSLT

Here we would be seeing two XSLT statements - firstly on how to set a value in session and secondly how to retrieve that session value:

<!-- Setting the Session Value -->
<xsl:value-of select="sc:SetSession('sessionval','2451')"/>

<!-- Retriving the Session Value -->
<xsl:value-of select="sc:Session('sessionval')"/>
The output can be observed on the front-end as: 2451

Monday, January 11, 2010

Sitecore Tweets n Tricks


Untitled DocumenHey Folks,

Got some really cool stuff and out of the box solution for things that could otherwise turn into some real headache...


1. Modifying page editor buttons

What would you do if you wanted to add a button to the page editor default toolbar? Well here's what I found.

Locate the page edit button:

Core: /sitecore/system/setttings/html editor profiles/rich text default_old/webeditButtons/

Duplicate an item there and you have a new button. In the datasection on the new item you find 3 options:

Icon - self explanatory

Tooltip - selfexplatory

Click: A call to a javascript function,

(Example)

Striketrough: javascript:Sitecore.WebEdit.execute("strikeThrough", true, true);

Italic: javascript:Sitecore.WebEdit.execute("Italic", true, true);

All the standard javascript execute commands should work

The result is a strikethough button:

Settings:

Click field: "javascript:Sitecore.WebEdit.execute("strikeThrough", true, true);"

Icon field: found an strike through icon in the standard sitecore image library.

Tooltip field: "Strike through"

The javascript-file with functions are found here : website/shell/applications/webEdit.js

Regarding calling sitecore functionality. There is only created 2 functions in the js file:

Insert image:

javascript:Sitecore.WebEdit.insertImage($JavascriptParameters)

Insert Link:

javascript:Sitecore.WebEdit.insertLink($JavascriptParameters)


So if you wan't to make something more that that we would have to write new functions for that.


2. using Sitecore.Kernel.Webutils

Webutils functions are great helper functions as well. If we want to get the url of the server, we can have it as a great help.

Install it in web.config:

<xslExtensions>

...

<extension mode="on" type="Sitecore.Web.WebUtil, Sitecore.Kernel" namespace="http://www.sitecore.net/webutil" singleInstance="true" />

</xslExtensions>


Now declare it in your xslt:

xmlns:webutil="http://www.sitecore.net/webutil"

Now it’s ready for use (example):

<xsl:text>Display Url: </xsl:text><xsl:value-of select="webutil:GetServerUrl()" /><xsl:value-of select="sc:path(.)" />



3. util:GetString

It might not strike us at the first thought but it’s really a common one - util:GetString(string, default).

A xsl:choose used to display the title of an item can be replaced as follows:

<xsl:choose>

<xsl:when test="sc:fld('menutitle',.)">

<xsl:value-of select="sc:fld('menutitle',.)">

</xsl:when>

<xsl:when test="sc:fld('title',.)">

<xsl:value-of select="sc:fld('title',.)">

</xsl:when>

<xsl:otherwise>

<xsl:value-of select="./@name">

</xsl:otherwise>

</xsl:choose>


simpler call:

<xsl:value-of select="util:GetString(util:GetString(sc:fld('title',.), sc:fld('menutitle',.)),@name)"/>



Hope they work for you.... :-)