Tigraine

Daniel Hoelbling-Inzko talks about programming

Making simple things hard: NVelocity

Posted by Daniel Hölbling on July 14, 2009

I was pretty done with the world after spending almost 2 days with xhtml/css coding for the new iC-Website. Turns out the programming gods want to teach me a lesson. Look at my Google Search log:

image

What you see here is me spending almost 45 minutes on formatting a date!

I need to take the date and split it into 3 parts so I can stick it into the following markup:

<div class="date">
    <span class="day">$day</span><span class="month">$month</span>
    <hr />
    <span class="year">$year</span>
</div>

I thought, hey that should be really simple, just call DateTime.ToString() and all will be well. And because it was so obvious I tried to create a macro to keep my viewcode clean. The result was:

#macro ( dateBox $currentDate )
#set ($day = $currentDate.Day.ToString("00") )
#set ($month = $currentDate.Month.ToString("00") )
#set ($year = $currentDate.ToString("yy") )
<div class="date">
    <span class="day">$day</span><span class="month">$month</span>
    <hr />
    <span class="year">$year</span>
</div>
#end

I still have no explanation why passing a DateTime into the macro isn’t working. Maybe because calling macros in NV isn’t the same as calling a CLR method. So I had to abandon the macro idea and just code it the way it is.

Now what really pissed me off to the point where I almost considered using ASPView as a view engine was that I spent almost 45 minutes with this for one simple reason: NVelocity won’t report errors as long as they don’t break the parser.

Perfect example:

$DateTime.Now.Format("dd MM yyyy")

Will just result in a “$DateTime.Now.Format("dd MM yyyy")” output, for no apparent reason other than the fact that there is no Format() method on the DateTime. Clearly my bad, I’m stupid and far too dependent on IntelliSense, but if NV isn’t telling me how I screwed up it’s really hard to find the mistake. Especially without syntax highlighting and intellisense DateTime.Format(…) looks perfectly reasonable at first.

Finally I ended up with this:

#set ($day = $currentDate.ToString("dd") )
#set ($month = $currentDate.ToString("MM") )
#set ($year = $currentDate.ToString("yy") )
<div class="date">
    <span class="day">$day</span><span class="month">$month</span>
    <hr />
    <span class="year">$year</span>
</div>

Filed under net, castle, programmierung
comments powered by Disqus

My Photography business

Projects

dynamic css for .NET

Archives

more