Defining reusable template blocks

47 views
Skip to first unread message

Yang Zhang

unread,
Oct 26, 2011, 5:46:56 PM10/26/11
to sca...@googlegroups.com
I know you can reuse templates by putting the reusable pieces (e.g.
the template for rendering a button) into separate files:

-@ val name: String
-@ val href: String
-@ val body: String
.button-container
a(href=href) = name
.button-body != body

and calling:

render("button.jade", "name" -> "Push Me", "href" -> "go.html",
"body" -> "<img src='foo.png'>")

However I'm wondering if there's something that's (1) lighter weight
and (2) can pass template blocks, along the lines of:

- def button(name: String, href: String)(body: String) =
.button-container
a(href=href) = name
!= body

To be used with (e.g.):

!= button("Push Me", "go.html")
img(src='foo.png')

The lighter-weight definitions would be a boon for small pieces of
abstraction/reusability (like buttons), and passing template bodies
would give these similar power as layouts.

If this doesn't already exist, I think it would make a killer feature.

--
Yang Zhang
http://yz.mit.edu/

Hiram Chirino

unread,
Oct 26, 2011, 10:40:39 PM10/26/11
to sca...@googlegroups.com
Yes it can be done. Try:

- def button(name: String, href: String)(body: => Unit) =
.button-container
a(href=href) = name
- body

To be used with (e.g.):

- button("Push Me", "go.html")
img(src='foo.png')


Regards,
Hiram

FuseSource
Web: http://fusesource.com/

Yang Zhang

unread,
Oct 27, 2011, 3:21:29 PM10/27/11
to sca...@googlegroups.com
Awesome, thanks Hiram.
Reply all
Reply to author
Forward
0 new messages