Fake Latin Generator in ASP.NET MVC

While designing HTML templates, I, like everyone else, frequently use filler text to fill up space before content exists. I’ve been doing this a lot lately in ASP.NET MVC 2. Having static fake Latin filler text in a web application while developing is nice but having random fake Latin text is more useful for a couple of reasons:

  1. It forces your page to render with different lengths of text which can help find bugs
  2. It’s more amusing and beats staring at the exact same text

Since I’m working in ASP.NET MVC mostly, I’ve used C# to create several extension methods on the HtmlHelper object. This enables me to generate fake Latin text inline in HTML and Views.

<h1><%= Html.FakeLatinTitle(5) %></h1>
<p>
    <%= Html.FakeLatinParagraph(500) %>
</p>

Here’s the C# source behind FakeLatinParagraph(), FakeLatinParagraphs() and FakeLatinTitle():

using System;
using System.Web.Mvc;
using System.Text;

namespace Web.Extensions
{
    public static class HtmlHelperExtensions
    {
        #region Private Statics

        private static int _seed;

        #endregion

        #region Extension Methods

        public static string FakeLatinParagraph(this HtmlHelper helper, int wordCount)
        {
            string[] dictionary = "lorem ipsum dolor sit amet consectetuer adipiscing elit sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat ut wisi enim ad minim veniam quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi Ut wisi enim ad minim veniam quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat duis autem vel eum iriure dolor in hendrerit in vulputate velit esse molestie consequat vel illum dolore eu feugiat nulla facilisis at vero eros et accumsan et iusto odio dignissim qui blandit praesent luptatum zzril delenit augue duis dolore te feugait nulla facilisi".Split(' ');
            StringBuilder words = new StringBuilder();
            Random rand = null;
            string paragraph = string.Empty;

            if (HtmlHelperExtensions._seed >= int.MaxValue - 1000)
                HtmlHelperExtensions._seed = 0;

            rand = new Random(HtmlHelperExtensions._seed += DateTime.Now.Millisecond);

            for (int n = 0; n < wordCount; n++)
                words.AppendFormat("{0} ", dictionary[rand.Next(0, dictionary.Length)]);

            paragraph = words.ToString();
            paragraph = string.Format("{0}{1}.", char.ToUpperInvariant(paragraph[0]), paragraph.Substring(1).TrimEnd());

            return paragraph;
        }

        public static string FakeLatinParagraphs(this HtmlHelper helper, int paragraphCount, int wordsPerParagraph, string beforeParagraph, string afterParagraph)
        {
            StringBuilder paragraphs = new StringBuilder();

            for (int n = 0; n < paragraphCount; n++)
            {
                paragraphs.AppendFormat("\n{0}\n", beforeParagraph);
                paragraphs.AppendFormat("\t{0}", HtmlHelperExtensions.FakeLatinParagraph(helper, wordsPerParagraph));
                paragraphs.AppendFormat("\n{0}\n", afterParagraph);
            }

            return paragraphs.ToString();
        }

        public static string FakeLatinTitle(this HtmlHelper helper, int wordCount)
        {
            string title = HtmlHelperExtensions.FakeLatinParagraph(helper, wordCount);

            title = System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(title);
            title = title.Substring(0, title.Length - 1); // kill period from paragraph
            return title;
        }

        #endregion
    }
}

Don’t forget to include the namespace where the extension methods exist anywhere where you want to have access to them.

using System;
using System.Web;
using System.Web.Mvc;
// ... whatever other namespaces
using Web.Extensions; // YOU NEED ME!

If you want a set of extension methods to be available on every ASP.NET MVC View, you can add the reference in the web.config file.

<!-- SNIPPET OF WEB CONFIG -->
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID">
    <namespaces>
        <add namespace="System.Web.Mvc"/>
        <add namespace="System.Web.Mvc.Ajax"/>
        <add namespace="System.Web.Mvc.Html"/>
        <add namespace="System.Web.Routing"/>
        <add namespace="System.Linq"/>
        <add namespace="System.Collections.Generic"/>
        <add namespace="Web.Extensions"/> <!-- EXTENSION METHODS NAMESPACE -->
    </namespaces>
</pages>
<!-- END SNIPPET OF WEB CONFIG (don't replace your entire web.config file with only this) -->


Comments are closed.