Spark markup enhancements
geek, internet, programming, spark September 2nd, 2008A few more tricks added to Spark language. There was a question about how you would have an attribute that is present or not based on a condition. Say you have a bool isFooChecked variable and you want to do the following:
<input type="checkbox"
name="foo"
${isFooChecked ? 'checked=""' : ''}></input>
That doesn’t work of course… Spark elements are parsed by the xml spec. Plus this type of thing really messes with typical xml editors like Visual Studio, so keeping roughly xml spec compatible is one of the goals.
After mucking around with a few different ways you could express a conditional attribute, while trying to keep the attribute valid, one of them really stood out. Since there’s already a ${expr} syntax to output text, adding a ?{expr} to control the output seemed like an intuitive approach.
So the above example becomes:
<input type="checkbox"
name="foo"
checked="?{isFooChecked}"></input>
And this also takes care of another common scenario where you’re writing out class names conditionally. Say you’re writing out a menu and you want to mark the first and last items with a special class. Something like this would do that - it also shows how you use some of the new variables spark can provide when you iterate a collection.
<ul class="menublock">
<li each="var navItem in MainMenuItems"
class="first?{navItemIsFirst} last?{navItemIsLast}">
<a href="${navItem.Url}">${H(navItem.Text)}</a>
</li>
</ul>
Or say you want to have an “alt” class on even rows.
<table class="portfolio">
<tr each="var holding in CurrentPortfolio.Holdings"
class="alt?{(holdingIndex % 2) == 0} quoteline">
<td>${H(holding.Ticker)}</td>
<td>${holding.Quote.ToString("#,##0.00")}</td>
</tr>
</table>
Another thing added recently is a streamlined form of a few of the special elements that named things. Like content or macros. Here’s an example of adding material to a content block, a macro declaration, and how the content block would be used later.
<content name="rightrail">
<h3>See Also</h3>
<for each="var item in RelatedLinks">
${ShowLink(item)}
</for>
</content>
<macro name="ShowLink" link="RelatedLinkItem">
<p class="relateditem"><a href="${link.Url}">${H(link.Text)}</a></p>
</macro>
<div class="sidebar">
<use content="rightrail"/>
</div>
And with the shorter prefix-based variation of these elements.
<content:rightrail>
<h3>See Also</h3>
<for each="var item in RelatedLinks">
${ShowLink(item)}
</for>
</content:rightrail>
<macro:ShowLink link="RelatedLinkItem">
<p class="relateditem"><a href="${link.Url}">${H(link.Text)}</a></p>
</macro:ShowLink>
<div class="sidebar">
<use:rightrail/>
</div>
September 3rd, 2008 at 12:11 am
great stuff!
I also wrote a summery of all the new stuff that has been added :)
I am not 100% sure I will use the alternative prefix based syntax, Its nice to have the option though :) but I think I will try it, see how the views will look..
September 3rd, 2008 at 2:46 am
I noticed that a bit earlier.
http://www.codinginstinct.com/2008/09/sparks-of-brilliance.html
I thought it was really well written. You wouldn’t be interested in write access on the http://dev.dejardin.org/documentation would you? :)
I’m curious also to see if the prefix syntax catches on. The idea came up thinking about how {section name=”top”} and {render section=”top”} could look a little heavy once partials can render sections. But you couldn’t simply assume a child element like {top} is the name of a section… So {section:top} and {render:top} was a sort of middle ground.
September 3rd, 2008 at 3:00 am
Sure, I can help with the docs :)
September 3rd, 2008 at 4:08 am
I love this! It’s really coming together. You are offering a great deal of power. I hope I’ll see it used instead of the WebForms engine. It’s way better anyways.
September 4th, 2008 at 3:41 am
What a long day! Productive though. @Torkel, the documentation related pages should be editable now if you’re ever struck by the urge to do some authoring. It’s a pretty standard Drupal cms with the default theme.