Kloojed

Create a CurrentPage expression builder

The use of expression builders is a convenient way to bind content data to controls on a template. Let´s say that we want to create an expression used for initializing a web control with the current page´s property values with the expression syntax.

You can build your own expression by inherits the ExpressionBuilder class. The ExpressionBuilder class has one method that must be implemented, GetCodeExpression.

In this example, The "CurrentPage" expression prefix is used:

  1. <%$ CurrentPage : PropertyName %>

PropertyName is the expression that is going to be sent to the CurrentPage expression builder as the value of the expression.

  1. [ExpressionPrefix("CurrentPage")]
  2. public class CurrentPageExpressionBuilder : ExpressionBuilder {
  3. public static object GetCurrentPageValue(string expression) {
  4. PageBase pageBase = HttpContext.Current.Handler as PageBase;
  5. return pageBase != null ? pageBase.CurrentPage[expression.Trim()] : null;
  6. }
  7. protected string ExpressionFormat {
  8. get { return @"Meridium.EPCms.Web.Compilation. CurrentPageExpressionBuilder.GetCurrentPageValue(""{0}"")"; }
  9. }
  10. public override CodeExpression GetCodeExpression(BoundPropertyEntry entry, object parsedData, ExpressionBuilderContext context) {
  11. return new CodeSnippetExpression(string.Format(ExpressionFormat, entry.Expression, entry.DeclaringType));
  12. }

To use the currentPage expression builder, you have to register it to the web.config in the <expressionBuilders> section:

  1. <configuration>
  2. <system.web>
  3. <compilation defaultLanguage="c#" debug="false">
  4. <expressionBuilders>
  5. <add expressionPrefix="CurrentPage" type="Meridium.EPCms.Web.Compilation. CurrentPageExpressionBuilder, Meridium.EPCms" />
  6. </expressionBuilders>
  7. </compilation>
  8. </system.web>
  9. </configuration>

With the expressionPrefix set to CurrentPage, the expression would look like this on a page template:

  1. <%$ CurrentPage : MainBody %>

The type attribute of the add element is the type of the CurrentPage expression builder we have created. If you created the class file in \Code folder of your project, you only need to write the name of the class. But if you have created your own class library you have to enter the full name of the class and assembly where the class is located.

Now you can use your newly created CurrentPage expression provider like this:

  1. <asp:Literal text="<%$ CurrentPage: MainBody %>" runat="server" />
  • Posted September 29, 2008
  • This is really cool! Wondered how much work it would take to implement something like this. Not much at all it seems. Thanks for showing it.

  • Steve Celius:

    You're welcome!

Leave a comment


Information

About

Marcus Lindblom is a Swedish professional frontend engineer with specialities like Web standards, ASP.NET, EPiServer, accessibility and long experience in developing high end websites for the public sector and commercial companies.

Copyright

Copyright © 2008 Marcus Lindblom. Powered by StormBreaker - Standards Compliant CMS. Hosted by Meridium AB.