Spark view engine

16 views
Skip to first unread message

Louis DeJardin

unread,
Jun 13, 2008, 1:43:41 PM6/13/08
to Castle Project Development List
Hello all,

I'm working on a spark view engine and I'd like to contribute a MR
IViewEngine project. If that sounds reasonable I'd like to apply
license, etc, and add it at castlecontrib/viewengines. Thoughts?

Some syntax can be seen at:
http://whereslou.com/2008/06/12/spark-view-engine-coming-soon
http://dev.dejardin.org/trac/spark/wiki/SparkDocumentation

Current source is at:
http://dev.dejardin.org/svn/spark/trunk

Feedback, requests, etc. all welcome of course. In fact for bugs/
requests feel free to use:
http://dev.dejardin.org/trac/spark/newticket

Cheers,
- Lou

Hamilton Verissimo

unread,
Jun 13, 2008, 2:10:33 PM6/13/08
to castle-pro...@googlegroups.com
Beautiful. Why the semi colon?
Also, any extensibility points? I saw you plan to support $H natively.
Any simple way to add my own, replacing existing? How can I extend it
to support new constructions, for instance, view components?

--
Cheers,
hamilton verissimo
ham...@castlestronghold.com
http://www.castlestronghold.com/

Louis DeJardin

unread,
Jun 13, 2008, 3:36:45 PM6/13/08
to Castle Project Development List
You can use $expression; or ${expression} - whichever looks best in
context - they're treated identically once parsed.

The expression is terminated explicitly because of the huge overlap
between valid html and valid csharp. Implicit termination of <p>
$areacode-555-1212</p> for example, or <p>www.$username.com</p>, would
have been extremely challenging at parse-time.

It's semicolon specifically because it seemed to fit. It's the
statement terminator in csharp so it almost never appears in
expressions, and "looked right" at the end of code.


For extensibility the library generates code over a named base class.
The MR specific base class is:

http://dev.dejardin.org/svn/spark/trunk/src/Castle.MonoRail.Views.Spark/SparkView.cs

By default when the MR SparkViewFactory allocates a SparkViewEngine it
names "Castle.MonoRail.Views.Spark.SparkView". The assumption is the
application would name it's own class based on SparkView (or
AbstractSparkView, or ISparkView) to introduce or mask anything used
from the template's code.


Support for view components is also one of the things I have
bookmarked to look into. I'm not sure what cooperation, if any, they
will require from the view engine.


On Jun 13, 1:10 pm, "Hamilton Verissimo"
> hamm...@castlestronghold.comhttp://www.castlestronghold.com/

Louis DeJardin

unread,
Jun 13, 2008, 4:15:42 PM6/13/08
to Castle Project Development List
And thank you, by the way. :)

Let me know if you have any recommendations about the best way to
introduce configuration of a view engine, or if there's something
particular you had in mind in introducing new constructions. Another
thing I've been considering is something like #macro which would help
avoid falling into code-behind traps.


On Jun 13, 2:36 pm, Louis DeJardin <Louis.DeJar...@gmail.com> wrote:
> You can use $expression; or ${expression} - whichever looks best in
> context - they're treated identically once parsed.
>
> The expression is terminated explicitly because of the huge overlap
> between valid html and valid csharp. Implicit termination of <p>
> $areacode-555-1212</p> for example, or <p>www.$username.com</p>, would
> have been extremely challenging at parse-time.
>
> It's semicolon specifically because it seemed to fit. It's the
> statement terminator in csharp so it almost never appears in
> expressions, and "looked right" at the end of code.
>
> For extensibility the library generates code over a named base class.
> The MR specific base class is:
>
> http://dev.dejardin.org/svn/spark/trunk/src/Castle.MonoRail.Views.Spa...

Kunle Odutola

unread,
Jun 13, 2008, 5:59:44 PM6/13/08
to castle-pro...@googlegroups.com

Hi,

> Hello all,
>
> I'm working on a spark view engine and I'd like to contribute
> a MR IViewEngine project.

What's the raison d'etre for Spark?.

How does it compare to pre-existing templating systems such as NVelocity,
Brail, Asp, NHaml, StringTemplate etc?. What does it do better/uniquely etc?

Kunle

Louis DeJardin

unread,
Jun 13, 2008, 8:33:41 PM6/13/08
to Castle Project Development List

Good question. I don't know if I can answer that.

The idea came about in a conversation which lead to the following
comment on haacked.com
http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net-mvc.aspx#67581

The goal is to keep all of the strengths of the dotnet language in a
way allows the file to remain well-formed xml. The aspx template does
provide the full strength of dotnet, but it's very difficult to
maintain a clean file when any nontrivial code is <%escaped {%> into
the flow. Brace matching and html matching is especially difficult.

So one aspect of Spark is to cut with the grain of the markup. The use
of <for each=""></for> and <if condition=""></if> produce template
files which xml editors can consider well-formed. The automatic
formatting, element completion, etc. also work with you rather than
against you.

The other aspect of Spark is to try to imagine the most terse,
intuitive and readable markup possible, so:
var link=Url.Link("etc", args);
becomes
<var link='Url.Link("etc", args)'/>

A product list is
<ul if="Products.Count != 0">
<li each="var prod in Products">$H(prod.Name);</li>
</ul>
<else>
<p>No products available.</p>
</else>


And you can render/parse a _AdZone.xml partial file (that uses $width;
and $height;) with
<div id="top-nav">
...
<AdZone width="640" height="44"/>
</div>
...
<div id="right-rail">
...
<AdZone width="300" height="200"/>
</div>

Hamilton Verissimo

unread,
Jun 13, 2008, 9:10:14 PM6/13/08
to castle-pro...@googlegroups.com
Resembles nitro. Also see http://hammett.castleproject.org/?p=94

--

Louis DeJardin

unread,
Jun 13, 2008, 10:48:32 PM6/13/08
to Castle Project Development List
Well, how about that. There is nothing new under the sun. :)

I'm taking a look at view components. I think I might know where
you're coming from with the question about extensibility - I'm adding
an extension interface to allows external code to augment the markup
processing and subsequent code generation.


On Jun 14, 2:10 am, "Hamilton Verissimo"
<hamm...@castlestronghold.com> wrote:
> Resembles nitro. Also seehttp://hammett.castleproject.org/?p=94
>
> On Fri, Jun 13, 2008 at 9:33 PM, Louis DeJardin
>
>
>
> <Louis.DeJar...@gmail.com> wrote:
>
> > Good question. I don't know if I can answer that.
>
> > The idea came about in a conversation which lead to the following
> > comment on haacked.com
> >http://haacked.com/archive/2008/05/03/code-based-repeater-for-asp.net...
> hamm...@castlestronghold.comhttp://www.castlestronghold.com/

Ken Egozi

unread,
Jun 14, 2008, 2:56:21 AM6/14/08
to castle-pro...@googlegroups.com
the only downside I see:
what if you are using the engine to generate non-valid-xml snippet?
like a partial (which might not have a single root), or even a non-html piece, like some JS/Json etc.
--
Ken Egozi.
http://www.kenegozi.com/blog
http://www.musicglue.com
http://www.castleproject.org
http://www.mamaherb.com
http://www.gotfriends.co.il

Louis DeJardin

unread,
Jun 14, 2008, 5:37:14 AM6/14/08
to Castle Project Development List
I don't believe the first case would be a problem - no xml readers or
writers are used. The inconvenience of non valid xml are the vs/r#
warnings.

Yep - it's not a problem:

<if condition="true">
<span class="alpha">
</if>
<else>
<span class="beta">
</else>
<b>
hello<br>world</b>
</span>

yielded

<span class="alpha">
<b>
hello<br>world</b>
</span>


In the last case I'm really not sure how well non-html output would
appear. Even if it did work I don't believe the template would be very
attractive.

Nope... It works very poorly. Especially if you're using prototype's $
function. I wonder what a good way of handling that case would be.

---

Also - update on view components - they're working to some extent.
Plenty of open questions and notimpl exceptions remain but the
following works.


<div>
<viewdata items="System.Collections.IEnumerable" foo="string"/>
<DiggStylePagination Page="items"/>
<ul>
<li each="var item in items">$item;</li>
</ul>
<CaptureFor id='"foo"'>one</CaptureFor>
<DiggStylePagination Page="items" Adjacents="1"/>
<CaptureFor id='"foo"' append='"after"'>two</CaptureFor>
<CaptureFor id='"foo"' append='"before"'>
<p>It is $DateTime.UtcNow; somewhere</p>
</CaptureFor>
$foo;
</div>



On Jun 14, 7:56 am, "Ken Egozi" <egoz...@gmail.com> wrote:
> the only downside I see:
> what if you are using the engine to generate non-valid-xml snippet?
> like a partial (which might not have a single root), or even a non-html
> piece, like some JS/Json etc.
>
> On Sat, Jun 14, 2008 at 5:48 AM, Louis DeJardin <Louis.DeJar...@gmail.com>

Hamilton Verissimo

unread,
Jun 14, 2008, 12:53:19 PM6/14/08
to castle-pro...@googlegroups.com
Some things looks nice, but not all...


> <if condition="true">
> <span class="alpha">
> </if>
> <else>
> <span class="beta">
> </else>

This is really weird. the 'else' seems to lose the connection with the
if. Also, having to write

<if condition="

instead of

<% if
or
#if(

isnt very appealing. is that the only way to express statements?


> <viewdata items="System.Collections.IEnumerable" foo="string"/>
> <DiggStylePagination Page="items"/>

I like the declaration, although I'd recommend a namespace. Same point
on the System.Collections.IEnumerable, string.

I know that might bump into technical issues, but really, the more the
language can do for you, more time you have to concentrate on the real
problem you're trying to solve.

Another thing on my wish list for a viewcomponent is the support for
block/closures. Is that on the plans?

Kunle Odutola

unread,
Jun 14, 2008, 2:23:57 PM6/14/08
to castle-pro...@googlegroups.com

Hi,

> Another thing on my wish list for a viewcomponent is the
> support for block/closures. Is that on the plans?

There is a viewcomponent wish list?. Where? (or at least, what's in it?)

Kunle

James Curran

unread,
Jun 14, 2008, 5:04:11 PM6/14/08
to castle-pro...@googlegroups.com
On Sat, Jun 14, 2008 at 12:53 PM, Hamilton Verissimo <ham...@castlestronghold.com> wrote:

This is really weird. the 'else' seems to lose the connection with the
if.
 
It does look a bit weird in XML, but it is essentially the same as C#:
if ()
{
}
else
{
}
 
>> Also, having to write  <if condition="    instead of  <% if  isnt very appealing.
 
Stylistically, it doesn't bother me.  The only problem is that it's a lot of typing.  But, if you define a XSD for the format, several popular text editors (VisualStudio Express & SharpDeveloper 2.2 to name just two free one), will give you Code Completion/IntelliSense.

-
Truth,
James

Hamilton Verissimo

unread,
Jun 14, 2008, 6:16:02 PM6/14/08
to castle-pro...@googlegroups.com
On Sat, Jun 14, 2008 at 6:04 PM, James Curran <james....@gmail.com> wrote:
>>> Also, having to write <if condition=" instead of <% if isnt very
>>> appealing.
>
> Stylistically, it doesn't bother me. The only problem is that it's a lot of
> typing. But, if you define a XSD for the format, several popular text
> editors (VisualStudio Express & SharpDeveloper 2.2 to name just two free
> one), will give you Code Completion/IntelliSense.

If you have to rely on a tool to make the experience pleasant (or
bearable), then something is fundamentally wrong...

Hamilton Verissimo

unread,
Jun 14, 2008, 6:16:32 PM6/14/08
to castle-pro...@googlegroups.com
Sorry, I meant view engine...

--

James Curran

unread,
Jun 14, 2008, 6:53:45 PM6/14/08
to castle-pro...@googlegroups.com
Alternately, you could learn to type....... ;-)
 
--
Truth,
James

Louis DeJardin

unread,
Jun 15, 2008, 12:12:39 AM6/15/08
to Castle Project Development List
(apologies in advance for the long posts...)

I don't see a problem with providing several syntax for the same
statement. Even if there are explicit forms of if I'd like to retain
the element/attribute options. I prefer the if="" attribute technique
myself.

For example any <if condition="">or <elseif condition=""> that has a
single child element, e.g.

<if condition="user.IsAnonymous">
<div class="loginbox">
..login form..
</div>
</if>
<elseif condition="user.IsAdmin">
<div class="adminbox">
...admin tools...
</div>
</elseif>
<else>
<div class="userbox">
..hello ${user.Name}...
</div>
</else>

can always be reduced to an if="" or elseif="" attribute

<div if="user.IsAnonymous" class="loginbox">
..login form..
</div>
<div elseif="user.IsAnonymous" class="adminbox">
..admin tools..
</div>
<else>
<div class="userbox">.. hello ${user.Name}...</div>
</else>

If you're talking simple inline-ish text there's always

<p>You are ${user.IsAnonymous ? "anon" : user.Name} and that's just
fine.</p>


That said, I don't like the condition="" attribute either. I think
I'll change <elseif condition="expression"> to <else if="expression">
but that still leaves the original <if> question. It's hard to come up
with something fluent.

<if when="">
<if case="">
<if true="">
<case if="">
<only if="">
<use if="">

For example

<only if="user.IsAnonymous">
</only>
<else if="user.IsAdmin">
</else>
<else>
</else>

Anyway! I'll see what I can do about providing some additional
alternatives based on <%if or #if instead of pseudo xml.


On Jun 14, 11:53 pm, "James Curran" <james.cur...@gmail.com> wrote:
> Alternately, you could learn to type....... ;-)
>
> --
> Truth,
> James
>
> On Sat, Jun 14, 2008 at 6:16 PM, Hamilton Verissimo <
>

Hamilton Verissimo

unread,
Jun 15, 2008, 12:31:42 AM6/15/08
to castle-pro...@googlegroups.com
How would I do something like

#foreach($user in $users)
<li class="userlist #if($user.IsAdmin) admin #end "> $user.FullName </li>
#end


On Sun, Jun 15, 2008 at 1:12 AM, Louis DeJardin
<Louis.D...@gmail.com> wrote:
>
> (apologies in advance for the long posts...)
>
> I don't see a problem with providing several syntax for the same
> statement. Even if there are explicit forms of if I'd like to retain
> the element/attribute options. I prefer the if="" attribute technique
> myself.

Louis DeJardin

unread,
Jun 15, 2008, 2:34:50 AM6/15/08
to Castle Project Development List

Probably something like

<li each="var user in users" class='userlist ${user.IsAdmin ?
"admin" : ""}'>${user.FullName}</li>



On Jun 15, 5:31 am, "Hamilton Verissimo"
<hamm...@castlestronghold.com> wrote:
> How would I do something like
>
> #foreach($user in $users)
> <li class="userlist #if($user.IsAdmin) admin #end "> $user.FullName </li>
> #end
>
> On Sun, Jun 15, 2008 at 1:12 AM, Louis DeJardin
>
> <Louis.DeJar...@gmail.com> wrote:
>
> > (apologies in advance for the long posts...)
>
> > I don't see a problem with providing several syntax for the same
> > statement. Even if there are explicit forms of if I'd like to retain
> > the element/attribute options. I prefer the if="" attribute technique
> > myself.
>
> --
> Cheers,
> hamilton verissimo
> hamm...@castlestronghold.comhttp://www.castlestronghold.com/

Louis DeJardin

unread,
Jun 15, 2008, 7:10:32 AM6/15/08
to Castle Project Development List
Thanks again for all of the input. Two more changes based on requests/
feedback.

One is the support for named sections in a view component.

<DiggStylePagination Page="items" Adjacents="1">
<startblock><hr></startblock>
<endblock><hr></endblock>
<prev>&laquo;</prev>
<next>&raquo;</next>
<link>
<button type="button" onclick="location.href='${url}';">${text}</
button>
</link>
</DiggStylePagination>

The second is the ability to use introduce code instead of relying
entirely on special elements and attributes. Raw code may take the
form <%statement%> or #statement[end-of-line], values to output can
may take the form $expression; ${expression} or <%=expression%>

#if (Params["page"] == "2") {
<p>page 2</p>
#} else {
<p>not page 2</p>
#}

<p if='Params["Page"] == "3"'>page 3</p>
<else><p>not page 3</p></else>

<%if (Params["Page"] == "4") {%>
<p>page 4</p>
<%} else {%>
<p>not page 4</p>
<% } %>


The entire sample file is here:
http://dev.dejardin.org/svn/spark/trunk/src/DemoCastleSite/Views/Home/ShowPage.xml

Thoughts?

Colin Ramsay

unread,
Jun 15, 2008, 8:14:49 AM6/15/08
to castle-pro...@googlegroups.com
The second new syntax type (<% %>) is identical to AspView, which I do
like, but IMO it becomes quite difficult to read with those "<%"
symbols all over the place. I think your original syntax, which
although more verbose, could be more readable. Still, it's nice to
have options.

Hamilton Verissimo

unread,
Jun 15, 2008, 10:07:04 AM6/15/08
to castle-pro...@googlegroups.com
I vote for options as well. Also, <% %> seems to be the defacto standard..

--

Ken Egozi

unread,
Jun 15, 2008, 10:18:12 AM6/15/08
to castle-pro...@googlegroups.com
yup options are good.

btw, my personal view is that <% %> is actually better, as it gives a very good distinction to a human observer on a template code, as for what is markup, and what is script
Reply all
Reply to author
Forward
0 new messages