RFC: Suggestion to improve the <if>

2 views
Skip to first unread message

Shay Erlichmen

unread,
Jan 5, 2010, 3:51:13 AM1/5/10
to Spark View Engine Dev
Hi,

A lot of times I find myself writing the following markup:

<if condition='string.IsNullOrEmpty(Model.City) == false'>
$(DisplayCity(city)
</if>

I suggest that if the *condition* attribute has only string type then
spark will generate the code above.
The markup will look something like this:

<if condition='Model.City'>
$(DisplayCity(city)
</if>

which is a lot more elegant.


Donn Felker

unread,
Jan 6, 2010, 10:05:44 AM1/6/10
to spar...@googlegroups.com
I see what you're saying, but the code <if condition='Model.City'> does not read as fluently as it should. One of the goals of spark is to increase the readability of the flow of the html. 

My advise for you would be to create an extension method for the string class if you would like to reduce the code written and increase readability so that it may read as such:

<if condition='Model.City.HasValue()'>
  ... 
</if>

Which simply checks if the string is null or empty. 





--

You received this message because you are subscribed to the Google Groups "Spark View Engine Dev" group.
To post to this group, send email to spar...@googlegroups.com.
To unsubscribe from this group, send email to spark-dev+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/spark-dev?hl=en.





--
Donn
http://blog.donnfelker.com/

Shay Erlichmen

unread,
Jan 6, 2010, 10:31:45 AM1/6/10
to Spark View Engine Dev
I already have that extension method, I thought want to save some key
stroke.

new (crazy?) idea then, how about:

<if hasvalue="Model.City">

where hasvalue is an extension method that return bool. meaning that
you can put whatever attribute name you like and spark will look for
the extension method.

anther example:

<if hasItems="Model.MyItems"> ||

<if happendToday="Model.Birthday">

> > spark-dev+...@googlegroups.com<spark-dev%2Bunsubscribe@googlegroups .com>

bking

unread,
Jan 7, 2010, 1:29:19 PM1/7/10
to Spark View Engine Dev
Wow... I really don't like those suggestions myself. When I read
'hasvalue', for example, I expect it to return (or be compared to) a
boolean. In your example, it's being compared to a string. I much
prefer the readability of the extension method Donn suggested. For
me, readability almost always trumps saving keystrokes.

> > Donnhttp://blog.donnfelker.com/- Hide quoted text -
>
> - Show quoted text -

Shay Erlichmen

unread,
Jan 8, 2010, 2:18:42 AM1/8/10
to Spark View Engine Dev
Well, readability is subjective.
Every new aspect of programming is unreadable at first (IMHO) and
takes time to get used to.
As for saving keystrokes I agree with you that coherency comes first.

> > > Donnhttp://blog.donnfelker.com/-Hide quoted text -

bking

unread,
Jan 8, 2010, 11:33:44 AM1/8/10
to Spark View Engine Dev
I agree completely about new aspects and their learning curve.
hat's part of my 'objection', if you will, to your 'hasvalue' idea -
the datatype is counterintuitive IMO and might add to the learning
curve for someone inheriting the code. Dann's idea is easy to absorb
OOTB due to the common usage pattern.

As for keystrokes - does anyone stumble as much as I do when typing
fluent code due to the shift necessary to type the '=>' ??

> > > > Donnhttp://blog.donnfelker.com/-Hidequoted text -
>
> > > - Show quoted text -- Hide quoted text -

Donn Felker

unread,
Jan 8, 2010, 2:15:52 PM1/8/10
to spar...@googlegroups.com
I dont stumble at all with the => syntax. Just became second hand after I typed it the 200th time. :) 




Donn Felker
Microsoft ASPInsider
MCTS, MCP, CSM, ITIL
http://blog.donnfelker.com
http://twitter.com/donnfelker


To unsubscribe from this group, send email to spark-dev+...@googlegroups.com.

Andrew Siemer

unread,
Jan 9, 2010, 12:24:28 AM1/9/10
to spar...@googlegroups.com, spar...@googlegroups.com
The => syntax is so baked into .net these days...I would imagine that I write less methods without that syntax than I do methods with that syntax!

Sent from my iPhone

Tom Janssens

unread,
Jan 22, 2010, 11:43:50 AM1/22/10
to Spark View Engine Dev
You can implement this functionality using partial views (but you
would lack the "else")
I tried to implement similar functionality in one of my OSS projects,
but I deleted the code again; it was too much overhead...
I just switched back to basic <if condition="Model.City!=null">

I did it like this as far as I can recall... (checks for both null
values and empty strings)

Create a file name "_if2.spark" and put in in a shared folder
This should be the content :
<viewdata notnull="Object">
<if condition="notnull != null && notnull!=''" >
<render />
</if>

You can then use it in you spark files like this :

<if2 notnull="Model.City">
$(DisplayCity(city)
</if2>

BTW, a trivial remark: you could also simplify your example code


<if condition='string.IsNullOrEmpty(Model.City) == false'>

to

<if condition='!string.IsNullOrEmpty(Model.City)'>

or

<if condition='(Model.City??"")!=""'>

Shay Erlichmen

unread,
Jan 24, 2010, 10:26:42 AM1/24/10
to Spark View Engine Dev
+1 Nice trick Tom!, I will sure to check it out.

As for the simplification, I try not to use negative ops, meaning that
instead of "!value" I use the verbose way of "value == false" (but I
never ever "value==true").
the reason for that is the the ! operator something get lost by the
eye/mind (especially in 2AM, 6 hours before release), so in my coding
std I never use ! (but I'm not a zealot about it).

Reply all
Reply to author
Forward
0 new messages