Setting values for layout using mustache

513 views
Skip to first unread message

David Copeland

unread,
Jan 15, 2011, 3:36:11 PM1/15/11
to Scalate
Just getting started with Scalate, but I really want to use Mustache.
One thing I'm having trouble with is getting values to the layout from
the template. For example:

default.mustache:

<html><head><title>{{title}}</title></head>
<body>
{{{body}}}
</body>
</html>

index.mustache

<h1>Hello!</h1>

"body" is special (I guess), so that + the three-mustache usage
results in:


<html><head><title></title></head>
<body>
<h1>Hello!</h1>
</body>
</html>

<title> is just blank, because I didn't set it. I can set it in the
context that I'm calling all of this from (a Scalatra block), however
I'd like to set the title in index.mustache, as this is a more logical
place, and it seems allowed via the other templating types.

Is this possible, and, if so, how do I do it?

James Strachan

unread,
Jan 17, 2011, 7:16:15 AM1/17/11
to sca...@googlegroups.com

So the way the layouts work with the other template languages in
Scalate is you define attributes inside the templates which are then
passed into the layout template as template attributes. However
Mustache has no 'set attribute' syntax since its a 'no logic in the
template' style.


We've had a long standing pending issue to make it easier to do a pure
mustache layout nicely...
http://www.assembla.com/spaces/scalate/tickets/122

I've just added an implementation which provides a way in the layout
template to navigate over the markup of the generated template output
using the element names.

Its probably better if I show you a sample template
https://github.com/scalate/scalate/blob/master/scalate-core/src/test/resources/org/fusesource/scalate/mustache/sample.mustache

and a layout
https://github.com/scalate/scalate/blob/master/scalate-core/src/test/resources/org/fusesource/scalate/mustache/mylayout.mustache

Notice the layout navigates the special 'html' section (which
represents the HTML markup of the generated template content) then all
the child sections pull out child elements by name. Also notice that
you can use _ to refer to all the children. Names starting with @
refer to attributes. (This is using the NodeSeq.\(name: String) method
BTW underneath).

So inside the {{#head}} section you can refer to {{title}} and you'll
get the entire <title> element (attributes and all). If you want the
contents of an element, create a section for it and use _ (e.g. the
way we exclude the <body> element in the layout but just use all the
children)


the test case is here with the expected output:
https://github.com/scalate/scalate/blob/master/scalate-core/src/test/scala/org/fusesource/scalate/mustache/LayoutTest.scala#L30


--
James
-------
FuseSource
Email: ja...@fusesource.com
Web: http://fusesource.com
Twitter: jstrachan
Blog: http://macstrac.blogspot.com/

Open Source Integration

David Copeland

unread,
Jan 17, 2011, 7:30:11 AM1/17/11
to sca...@googlegroups.com
That's pretty interesting.  So, if you wanted to only set the title, say, you might have, in your template

<title>My Title!</title>
<body>
Whatever else I <span>need</span>.
</body>

And the layout could be:

<html>
<head>{{title}}</head>
{{#body}}
<body>
{{_}}
</body>
{{/body}}
</html>

Or would you need to provide the entire hierarchy?

Dave

---
My Blog: http://www.naildrivin5.com/blog
Scala Tour for Java Developers: http://www.naildrivin5.com/scalatour
Fork me on Github: http://davetron5000.github.com

James Strachan

unread,
Jan 17, 2011, 8:11:09 AM1/17/11
to sca...@googlegroups.com
On 17 January 2011 12:30, David Copeland <da...@naildrivin5.com> wrote:
> That's pretty interesting.  So, if you wanted to only set the title, say,
> you might have, in your template
> <title>My Title!</title>
> <body>
> Whatever else I <span>need</span>.
> </body>
> And the layout could be:
> <html>
> <head>{{title}}</head>
> {{#body}}
> <body>
> {{_}}
> </body>
> {{/body}}
> </html>
> Or would you need to provide the entire hierarchy?

Currently the {{#html}} basically means parse the output as XML
markup. To be parsable as XML markup you need a single root element,
so wrapping the whole thing in <html> is a good idea. Though if you
want to omit the <head> and just use title directory thats fine
too....

<html>


<title>My Title!</title>
<body>
Whatever else I <span>need</span>.
</body>

</html>

Though I guess one of the big strengths of Mustache is it can work on
valid HTML docs that can be owned by a designer; so having a <head>
might be nice to do; but not required for sure.

Łukasz Lenart

unread,
Oct 18, 2012, 9:50:42 AM10/18/12
to sca...@googlegroups.com, ja...@fusesource.com
W dniu poniedziałek, 17 stycznia 2011 13:16:15 UTC+1 użytkownik James Strachan napisał:

Notice the layout navigates the special 'html' section (which
represents the HTML markup of the generated template content) then all
the child sections pull out child elements by name. Also notice that
you can use _ to refer to all the children. Names starting with @
refer to attributes. (This is using the NodeSeq.\(name: String) method
BTW underneath).

How to use @ for html attributes ? Is it possible ? I would like extract sections base not only on <body/> <title/> or <div/> tag but also base on attribute  (@id="container").


Thanks in advance
--
Lukasz
Reply all
Reply to author
Forward
0 new messages