Back to .NET-macros in Umbraco

Looking for the most solid and easy way to create macros in Umbraco I’ve been away from an obvious option – Ascx’es for quite some time. Some reasons for that.

  1. I like to have the markup editable within Umbraco UI for quick fixes.
  2. I dislike the need of two files for one single small macro.
  3. I like to get rid of that extra asp.form-markup as far as possible.
  4. Compiled code adds an extra administrative task for each small fix.

I thought that the third point here was next to impossible to get around – especially since the fact that one needs a asp:form control on each page that use Ascx’es that will add an unwanted <form> and viewstate markup on every page, even with no input controls.

Err…. What? … Yeah! That was my stupid idea of it anyway. But of course, as everyone else surely knew longtime, you dont need asp:form for pages without input controls. There’s really no need of that <form id=”aspnet” runat=”server”> on each page.

The first point I don’t care so much about any longer, with a nicely FTP-deploy setup. Also VS is such a great code editor and to edit Ascx markup – we have some back office Packages. user-control-file-editor and App_Code & UserControl Editor (link?)

Second point is not necessary so if one keeps all code inline. However I found that macro properties did not like that, and classes behave a bit strange in inline so with VS i keep the code behind following the great idea of separation between presentation and logic.

Fourth point – no need to compile at all, unless you want to.

And with that taken care of. There’s a fantastic amount of greatness coming with the pure .net-way of creating macros.

Most of the Macros I think of is smallish ones that really just transform data into html. Could I use Xslt? Yep. But not this time.

Entering regular .Net User Controls we are tempted to use ready made controls like ListViews, Repeaters and such.

But for most I found the plain inline syntax does it best (also data bound control markup does not allow conditions combined with Eval which makes stuff harder/uglier).

My basic exemple of a typical non-input Ascx-macro:


<ul>
 <%foreach (MyObjectRow itm in GetObjects())
 {%>
 <li><strong>
 <%=itm.Title %></strong>
 <div>
 <strong>Name:</strong>&nbsp;<span><%=itm.FullName%></span>
 </div>
 <div>
 <strong>Company:</strong>&nbsp;<%=itm.CompanyName %></div>
 <%if (itm.Phone != "")
 {%>
 <div>
 <strong title="voice">Phone:</strong>&nbsp;<span><%=itm.Phone%></span></div>
 <%} %>
 <div>
 <strong>E-mail:</strong>&nbsp;<a href="mailto:<%=itm.Email %>"><%=itm.Email %></a></div>
 </li>
 <%} %>
</ul>

And the code behind is as clean it could be with only that GetObjects function (combined with property for the macro):


public partial class usercontrols_MyUserControl : System.Web.UI.UserControl
{
 private string companyId;
 public string CompanyId
 {
 get { return companyId; }
 set { companyId = value; }
 }

 public MyObjectDataTable GetObjects()

 { get it from linq, static typed dataset or wherever }

}

As you see if you read my other blog posts I’ve been doing some Python for a couple of weeks, has been fun, but with this (really basic .net-knowledge) sorted out I dont see the need of DLR at the moment.

Actually I lost a lot of time doing stuff the hard way. Mostly trying to accomplish way too much with Xslt. This is one down side being a solo developer with little contact with others I guess.

I do think the clean inline code (as above) combined with Linq-syntax (and Xml data helper functions) makes Ascx the obvious way for all macros – over Xslt and DLR.

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s