String Variables

String Variables

Controls featured in this example:

  • Sitecore Web Control

Classes featured in this example:

  • Sitecore.Context
  • Sitecore.Data.Items.Item

String Variables C# Web Control Code

Sitecore.Data.Items.Item item = this.GetItem();
if (item == null) return;

string homePath = "/sitecore/content/Home";
string homeGUID = "{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}";
string homeTitle1 = string.Empty, homeTitle2 = string.Empty;

Sitecore.Data.Items.Item home1 = Sitecore.Context.Database.GetItem(homePath);
if (home1 != null) 
    homeTitle1 = home1.Fields["title"].Value;

Sitecore.Data.Items.Item home2 = Sitecore.Context.Database.GetItem(homeGUID);
if (home2 != null) 
    homeTitle2 = home2.Fields["title"].Value;

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.Write("C# Web Control Real-time Output");
output.RenderEndTag();

output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-size:x-small; width:100%; clear:both");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.Div);
output.AddAttribute(System.Web.UI.HtmlTextWriterAttribute.Style, "font-weight:bold");
output.RenderBeginTag(System.Web.UI.HtmlTextWriterTag.P);
//using : for colon and   for space
output.Write("{0}: {1}", "Item", item.DisplayName);
output.RenderEndTag(); //P

output.Write("{0}&#58;&#160;{1}", "homePath", "<b>" + homePath + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "homeGUID", "<b>" + homeGUID + "</b><br/><br/>");

output.Write("{0}&#58;&#160;{1}", "Home title obtained using homePath variable", "<b>" + homeTitle1 + "</b><br/>");
output.Write("{0}&#58;&#160;{1}", "Home title obtained using homeGUID variable", "<b>" + homeTitle2 + "</b>");

output.RenderEndTag(); //Div


String Variables XSLT Rendering Code

<xsl:variable name="homePath" select="'/sitecore/content/home'" />
<xsl:variable name="homeGUID" select="'{110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}'" />

<xsl:variable name="home1" select="sc:item($homePath,.)" />
<xsl:variable name="home2" select="sc:item($homeGUID,.)" />

<div style="font-weight:bold; width:100%; clear:both"> <br/>
<xsl:value-of select="'XSLT Rendering Real-time Output'"/>
</div>

<div style="font-size:x-small; width:100%; clear:both">

<p><b><xsl:value-of select="'Item'"/>
&#58;&#160;<xsl:value-of select="$sc_item/@name"/></b></p>

<xsl:value-of select="'homePath'"/>&#58;&#160;
<b><xsl:value-of select="$homePath"/></b><br/>
<xsl:value-of select="'homeGUID'"/>&#58;&#160;
<b><xsl:value-of select="$homeGUID"/></b><br/><br/>

<xsl:if test="$home1">
<xsl:value-of select="'Home title obtained using homePath variable'"/>&#58;&#160;
<b><sc:text field="title" select="$home1" /></b><br/>
</xsl:if>

<xsl:if test="$home2">
<xsl:value-of select="'Home title obtained using homeGUID variable'"/>&#58;&#160;
<b><sc:text field="title" select="$home2" /></b>
</xsl:if>

</div>


String Variables C# Web Control Output

homePath: /sitecore/content/Home
homeGUID: {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}

Home title obtained using homePath variable: Resources for Sitecore Users
Home title obtained using homeGUID variable: Resources for Sitecore Users


String Variables XSLT Rendering Output

homePath:  /sitecore/content/home
homeGUID:  {110D559F-DEA5-42EA-9C1C-8A5DF7E70EF9}

Home title obtained using homePath variable:  Resources for Sitecore Users
Home title obtained using homeGUID variable:  Resources for Sitecore Users