[stringtemplate-interest] quick question
Kay Roepke
kroepke at dolphin-services.de
Wed May 10 16:40:38 PDT 2006
On 10. May 2006, at 23:18 Uhr, David Moshal wrote:
> thanks Ter, actually, it's not quite fixed (though in the example
> it is).
> What I really need to do is for the model to tell the view how many
> rows to
> create, ie:
>
> <% for (int i=0; i< rowCount: i++) { %>
>
> <tr> <td> <%=i%> </td></tr>
>
> <% } %>
>
> where the model passes in "rowCount".
Well, in my book I would make the controller(!) decide how the view
should display things like paging and supply it with the necessary
datastructures. It should be the model and the view that ideally are
reusable - the controller mostly isn't. Thus to implement paging,
there shouldn't be a need for the view to know about it.
I'd have one template that says how to render a single element, a
list of elements (say in a row), and the paging part of the list
(current page,
total number of pages, etc.). They I'd have the controller to sort
out pagination and the number of elements in a list/row and have it
prepare
the necessary datastructures.
No need for any expressions in the view. Clean model, clean code,
messy controller. That's what you mostly end up with in the MVC
paradigm. Of course
there are ways to get a little structure into the controller part,
but in my experience it will always stay glue code - it has to know
about both the
model and the view.
Specifically for the example you gave above, I'd create a list of
rowCount elements, with the elements being 0..rowCount-1. Then the
template would be
straightforward:
<listOfNumbers:{i|\<tr>\<td><i>\</td>\</tr>}; separator="\n">
Keeps the template nice and clean and easy to understand. When you're
using templates, you'll always have to read the code that feeds data
into the templates
at the same time, otherwise you couldn't possibly know what the data
structure is. That's one drawback of controller-view separation: You
have to look
in two spots to get the picture. But it is so useful to keep your
code cleaner. :)
> Regarding the "|" notation, I must confess that the documentation
> does leave
> me confused
> - perhaps I just need to experiment a bit.
The syntax is sort of looks like Smalltalk to me (well, ok, just a
tiny bit ;)). Consider
<list:{element|This is element number <element.cardinalNumber>.};
separator="\n">
The expression between '<' and ':' is the list of values/objects. The
stuff name between '{' and '|' is the name of the local variable used
to access the current element of that list. After the '|' comes the
template contents (this maybe a another template reference and so on).
The separator part is what goes between the template expansions (but
not after the last element).
(I don't want to sound lecturing, just hoping to make it clear :))
-k
More information about the stringtemplate-interest
mailing list