[stringtemplate-interest] quick question
David Moshal
dmoshal at gmail.com
Thu May 11 11:28:56 PDT 2006
Ok - I found a simple and inexpensive solution to the 'apply template n
times problem'
(formerly know as a 'for loop'):
--------------
template.setAttribute ("n", new int[1000]);
------
$n:{
<tr><td>$i$</td></tr>
}$
---------
ST accepts arrays, java initializes arrays automatically, no this seems a
relatively inexpensive solution.
Dave
On 5/11/06, David Moshal <dmoshal at gmail.com> wrote:
>
> Kay - thanks for the advice, I've implemented something similar.
>
> I guess my concern is regarding performance (I have built a
> 'near-real-time' system, so I'm always concerned about performance).
> Creating a list structure merely to tell the view that it needs to
> replicate a template seems an unnesseccary performance hit, especially as
> the list in my case is several thousand rows long,
> ie several thousand objects created.
>
> Surely being able to specify:
> "repeat the followng code block n times, where n is an attribute",
> wouldn't break the paradigm?
>
> Dave
>
>
> On 5/10/06, Kay Roepke <kroepke at dolphin-services.de> wrote:
>
> >
> > 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
> >
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20060511/123e3fa1/attachment.html
More information about the stringtemplate-interest
mailing list