EPiPattern

You are currently browsing articles tagged EPiPattern.

How do you inject dynamic content into your web page? Following is a list of the most common methods I find when doing quality and code reviews with some comments and dangerous pitfalls.

#1: Inline Expressions with Code Render Blocks

Using inline expressions is a shortcut for calling the Write method.

Example – DONT:

<h2><%= CurrentPage.PageName %></h2>
<img src="<%= CurrentPage["ImageUrl"] %>" alt="<%= CurrentPage["MainIntro"] %>" />
<div class="mainarea">

     <%= CurrentPage["MainBody"] %>
</div>

Since xhtml is just text, writing some more in-between existing static blocks is fast and simple. Probably the easiest way to inject dynamic content but it is also dangerous!

Example – DO always use HtmlEncode:

<h2><%= HttpUtility.HtmlEncode(CurrentPage.PageName) %></h2>

What happens if our PageName property contains a “<” or “&” character in the example above? Your page will not validate and you risk that it breaks down. It can also be exposed to script injection if the content is user generated.

To be on the safe side you should always call HttpUtility.HtmlEncode on all strings that you inject with inline expressions.

Example – DO always use a fallback for src-attributes

<img src="<%= CurrentPage["ImageUrl"] ?? "/Missing.png" %>" />

If your ImageUrl property is empty you would get a img-tag with an empty src-attribute. This can lead to the page being rendered twice. FireFox interpret a null src-attribute a relative url and evaluate it using the page url as base returning the same url.

#2: With EPiServer:Property web control

Using the EPiServer Property Web Controls instead of inline expressions has several benefits. It could be used for rendering the PageName and MainBody in the example above.

Example – DO use EPiServer:Property web control

<h2><EPiServer:Property PropertyName="PageName" runat="server" /></h2>
<EPiServer:Property CssClass="mainarea" PropertyName="MainBody" runat="server" />

1) When rendering EPiServer’s ToWebString() is used. This does almost the same job as always calling HtmlEncode yourself.

public virtual void CreateDefaultControls()
{
    Label target = new Label();
    target.Text = PropertyData.ToWebString();
    CopyWebAttributes(target);
    Controls.Add(target);
}

2) It enables the Simple Edit feature for the editors without any extra work. (If you do not know what “Simple Edit” is, you should read the Editors Manual for EPiServer before doing any more development.)

3) Different types have get different rendering automatically. Commonly used for PageReferences and Url’s that are rendered as a Hyperlink.

Example – DO use EPiServer:Property for rendering links if possible

<EPiServer:PageList PageLinkProperty="NewsArchivePage" MaxCount="5" runat="server" >
  <HeaderTemplate>
    <h3><EPiServer:Property PropertyName="PageLink" runat="server" /></h3>
    <ul>
  </HeaderTemplate>
  <ItemTemplate>
    <li><EPiServer:Property PropertyName="PageLink" runat="server" /></li>
  </ItemTemplate>
  <FooterTemplate>
    </ul>
  </FooterTemplate>
</EPiServer:PageList>

4) Use PageLinkProperty to follow a Page-property to another page and render a value. Takes care of all hassle and calls GetPage() and check for nulls for you.

Example – DO use EPiServer:Property to render values from another page

<h3><EPiServer:Property PageLinkProperty="FeatureArticlePage" PropertyName="PageLink" runat="server" /></h3>
<div><EPiServer:Property PageLinkProperty="FeatureArticlePage" PropertyName="MainIntro" runat="server" /></div>

Tags: , , , , ,

This might be obvious stuff for most seasoned EPiServer developers but I still think it is worth blogging about because I see these dangerous mistakes whenever I do a code review of an EPiServer project.

Null values

Remember that all EPiServer properties with an empty value never are stored in the database. If you access it from code, it will always be null – not an empty string, 0 or false as you maybe expected.

Why null? It is by design and is very convenient if you want to check if something is not set by an editor or does not exist on this page. You just have to compare with null regardless of data type.

Example – DO & DONT

This will throw NullReferenceException if value is empty or missing:
sb.Append(CurrentPage["MyProperty"].ToString());

StringBuilder.Append accepts null objects so this is better:
sb.Append(CurrentPage["MyProperty"]);

This will throw NullReferenceException if value is empty or missing:
<%= CurrentPage.Property["MyProperty"].Value.ToString()%>

Markup will accept any type and convert to string so cast is not needed:
<%= CurrentPage ["MyProperty"] %>

Other examples with fallback

If must have a string returned use as operator and ?? operator:
string x = ["MyProperty"] as string ?? string.Empty;

And for Value Type it is written like this: (thanks Henrik)
DateTime x = (DateTime)(CurrentPage[”MyProperty”] ?? DateTime.Now);
int i = (int)(CurrentPage[”MyProperty”] ?? 0);

If you need a fallback in markup the?? Operator can be used:
<%= CurrentPage[“Heading”] ?? CurrentPage.PageName %>

Use Default indexer

Remember that there is a default indexer on PageData that returns the value, too.

Example – DO & DONT

This expression:
<%= CurrentPage.Property["MyProperty"].Value %>

…is equivalent:
<%= CurrentPage["MyProperty"] %>

Common EPiServer Property Names

It is very common that Page Types in EPiServer has the following properties.

Heading (string)

Used for the h1-tag of the page. If not defined the page is coded to fall back to the PageName. (See public templates for example implementation). This makes it possible to use PageName for a shorter text in menus and the meta title-tag and have a little longer text for the actual h1-header.

MainIntro (string)

This property is commonly used to describe a page in Teasers on other pages and in search results. It is usually also used to generate the meta description-tag which can affect the description external search engines like Google show.

There are functions in EPiServer that uses MainIntro – like PreviewText() method on PageTemplateContainer (type of Container object in EPiServer PageList, PageTree, etc). It implements a fallback if no MainIntro exists to strip MainBody of HTML and use first 400 characters.

MainBody (string)

Almost always used for the body text of an page in EPiServer.

Read more

Read more about Properties with Special Functionality in EPiServer.

Tags: , ,