开发者

Echoing sitecore item name in an ASPX layout

开发者 https://www.devze.com 2023-04-02 12:56 出处:网络
I\'ve got what I\'m sure is a very simple Sitecore question, however I\'m not familiar with ASP.NET at all and would appreciate any help that can be given. Below is a snippet of the LayoutMain.aspx fi

I've got what I'm sure is a very simple Sitecore question, however I'm not familiar with ASP.NET at all and would appreciate any help that can be given. Below is a snippet of the LayoutMain.aspx file which, unsurprisingly, is the main layout file for the majority of the pages of the website I run.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="LayoutMain.aspx.cs" Inherits="Company.Website.Layouts.LayoutMain" %>
<%@ Import Namespace="Company.Business"%>
<!DOCTYPE 开发者_运维技巧html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <!-- LayoutMain -->
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title><sc:FieldRenderer FieldName="item" runat="server" /></title>

I would like to add an ID and a couple of class attributes to the <html> tag. This will give me fine grained styling control via CSS of the entire site without having to mess around in .aspx files!

I would like the ID attribute to be taken from the first child level from Sitecore\Home and the class attribute to include both the current item name and the current item template item name. This would result in something like this:

<html xmlns="http://www.w3.org/1999/xhtml" id="shop" class="big-boxes product">

Where the current item is Sitecore\Home\shop\boxes\big-boxes in the main CMS tree and has a template of product

Many thanks in advance.


I think you could simply do that with this line:

<html xmlns="http://www.w3.org/1999/xhtml" id="shop" class='<%# Sitecore.Context.Item.Name + Sitecore.Context.Item.Template.Name %>'>

I hope that helps.

edit:

Remember to add a Databind(); to the Page_Load method to get the data bound.

You can use the same principle to make the id dynamic.


To get the ID too you will need to walk back up the ancestors of the current item until you find an item which has a parent of Home, that will give you your Shop item.

Ideally you should do this in code-behind rather than inline in the ASPX. You say you're not familiar with ASP.NET, add a comment if you have trouble with this to see if someone can help further.

In your ASPX:

<html xmlns="http://www.w3.org/1999/xhtml" id="Html" runat="server" >

Then in your code behind, i.e. in the LayoutMain.aspx.cs file:

protected void Page_Load(object sender, EventArgs e)
{
    Html.ID = GetHtmlID();
    Html.Attributes["class"] = GetHtmlClass();
}

protected string GetHtmlID()
{
    Sitecore.Data.Items.Item currentItem = Sitecore.Context.Item;

    while ( currentItem.Parent != null && currentItem.Parent.Name != "Home" )
    {
        currentItem = currentItem.Parent;
    }

    return (currentItem.Parent.Name == "Home") ? currentItem.Name : String.Empty;            
}

protected string GetHtmlClass()
{
    return string.Format( "{0} {1}", Sitecore.Context.Item.Name, Sitecore.Context.Item.Template.Name );
}

The GetHtmlID method here will attempt to find an ancestor which has Home as the parent (e.g. Shop). If it can't then you will end up without an ID, or you could change that String.Empty to put in a default ID.

Note that this hardcodes the Home template name directly into your code, ideally it would be better to move this out into config or instead match against Parent.ID with the Sitecore ID of your Home Item which is unlikely to change. However I imagine it's going to be rare that you would be renaming your Home item anyway.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号