Extending PageData with some cool Html Helpers

Posted on December 8, 2009 by Frederik Vig in EPiServer

I’ve been reading up on ASP.NET MVC recently. One of the things I love about ASP.NET MVC is the control we as developers have over our markup. ASP.NET MVC doesn’t use server controls, instead it relies heavily on extension methods for generating markup. For some time now I’ve been playing with the idea of adding similar functionality to EPiServer and the EPiCode.Extensions project.

This evening I started adding some of these Html Helpers to the project. I’ve started by focusing on generating links (anchors, hyperlinks). Nothing special, but something that I think will save a lot of repetitive coding.

Update: 08.12.2009

I wrote another blog post explaining my thoughts more: Html Helpers vs Server controls.

LinkExtensions

using System;
using System.Collections.Generic;
using System.Web.Routing;
using EPiCode.Extensions.Helper;
using EPiServer.Core;
 
namespace EPiCode.Extensions
{
    public static partial class PageDataExtensions
    {
        public static string HtmlLink(this PageData page)
        {
            return HtmlLink(page, page.PageName, null);
        }
 
        public static string HtmlLink(this PageData page, string linkText)
        {
            return HtmlLink(page, linkText, null);
        }
 
        public static string HtmlLink(this PageData page, Func<PageData, string> linkText)
        {
            return HtmlLink(page, linkText(page), null);
        }
 
        public static string HtmlLink(this PageData page, object attributes)
        {
            return HtmlLink(page, page.PageName, new RouteValueDictionary(attributes));
        }
 
        public static string HtmlLink(this PageData page, string linkText, object attributes)
        {
            return HtmlLink(page, linkText, new RouteValueDictionary(attributes));
        }
 
        public static string HtmlLink(this PageData page, Func<PageData, string> linkText, object attributes)
        {
            return HtmlLink(page, linkText(page), new RouteValueDictionary(attributes));
        }
 
        private static string HtmlLink(PageData page, string linkText, IDictionary<string, object> attributes)
        {
            if (page.PageLink == null || page.PageLink.ID < 1)
            {
                return string.Empty;
            }
 
            return HtmlHelper.GenerateLink(page.LinkURL, linkText, attributes);
        }
    }
}

Usage

    // Method signatur
    public static string HtmlLink(this PageData page)
 
    // Usage
    CurrentPage.HtmlLink();
 
    // Renders
    <a href="/en/mypage/">My PageName</a>
    // Method signatur
    public static string HtmlLink(this PageData page, string linkText)
 
    // Usage
    CurrentPage.HtmlLink("Some text");
 
    // Renders
    <a href="/en/mypage/">Some text</a>
    // Method signatur
    public static string HtmlLink(this PageData page, Func<PageData, string> linkText)
 
    // Usage    
    CurrentPage.HtmlLink(p => p.GetProperty("Heading|PageName").Value.ToString());
 
    // Renders
    <a href="/en/mypage/">My Heading</a>
    // Method signatur
    public static string HtmlLink(this PageData page, object attributes)
 
    // Usage
    CurrentPage.HtmlLink(new { style = "color:red;" });
 
    // Renders
    <a href="/en/mypage/" style="color:red;">My PageName</a>
    // Method signatur
    public static string HtmlLink(this PageData page, string linkText, object attributes)
 
    // Usage
    CurrentPage.HtmlLink("Some text", new { target = "_blank", title = "A very cool link"});
 
    // Renders
    <a href="/en/mypage/" target="_blank" title="A very cool link">Some text</a>
    // Method signatur
    public static string HtmlLink(this PageData page, Func<PageData, string> linkText, object attributes)
 
    // Usage
    CurrentPage.HtmlLink(p => p.StartPublish.ToShortDateString(), new { style = "text-decoration: none;", @class = "myclass" });
 
    // Renders
    <a class="myclass" href="/en/mypage/" style="text-decoration: none;">11/3/2009</a>

This will of course be extra cool combined with the Page Type Builder project.

So what do you think? Cool? Spaghetti code? Don’t care? Comments are very much appreciated! :)

Related Posts: