Reflex ” Templates, part 1

I’ve just read this nice opinion piece about templates and why “the vast majority of template engines […] simply have it wrong”. And so I thought it may be a good time to talk more about templates in Reflex.

I don’t think I can put Reflex templates in the same category as most template engines. Templates in Reflex are there to save you (the framework or module programmer) from writing code. And by doing so, it changes that traditional template paradigm: instead of retrieving all the data then sending it to the template engine, it’s the template engine that gather the data based on what the template actually need.

But this doesn’t make the template hard to use; you’d hardly notice what’s happening when you write something like this:

<rx:entry-list>

<h2><rx:entry-title/></h2>

<rx:entry-content/>

</rx:entry-list>

which in reality is simply a shorter form for this:

<rx:list source="weblog/entry" as="entry">

<h2><rx:data source="entry/title"/></h2>

<rx:data source="entry/content"/>

</rx:list>

All this looks like a standard template engine doesn’t it? But based on this template, Reflex will perform the following SQL query:

SELECT title, content FROM weblog_entry WHERE weblog_id=1

Let’s say we add a publication date to our template:

<rx:entry-list>

<h2><rx:entry-title/></h2>

<rx:entry-content/>

<p>Published on <rx:entry-publication-date/></p>

</rx:entry-list>

The query will change accordingly:

SELECT title, content, published_on FROM weblog_entry WHERE weblog_id=1

How does this work? The template is converted to an intermediately form, which is an array of strings and “Pane” objects. Panes nested into other panes can request data to their parent; panes that gather data should select fields requested by their children.

So, Reflex use the template for both things: gathering the data and rendering the data. Doing this doesn’t make the template more complicated than other template systems. But it’s a subtle paradigm shift: instead of being about only how things are presented, the template is also about what is presented.


Comments

Mary

Awesome. I’ve been trying to figure out this kind of thing, and failed miserably. I can’t wait to see what else you’ve got in store.


  • © 2003–2024 Michel Fortin.