[stringtemplate-interest] template expressiveness

Terence Parr parrt at cs.usfca.edu
Thu May 10 19:53:09 PDT 2007


On May 10, 2007, at 5:24 PM, Nate wrote:

> Terence Parr wrote:
>>> B. Would index access to lists break separation? Eg...
>>> $tests[0].name$
>>> $tests[1].name$
>>>
>>
>> yep because it is like calling a function with an argument from  
>> the  template, not that there aren't similar things.  Even  
>> referencing an  attribute requires that I invoke toString, the  
>> difference is that  there are no attributes or arguments going in.
>>
>>
>>> [0] can be done with first(), but getting the nth element is   
>>> impossible otherwise.
>>>
>>
>>   on purpose I am afraid.
>>
> I disagree. Why is getting the first or last element any different  
> than getting the second? This is template logic and doesn't break  
> separation. IMO, it should be possible to index lists (stuff[1] and  
> stuff[-1]) and also to get sublists (stuff[2..5] and stuff[3..-2]).

picking element n from a list is very different than picking stuff 
[1].  I don't need an arg on stuff[1] as it's first(stuff).

So you are in favor of allowing randomfunction(randomarg).  That's  
fine.  Plenty of other turing complete template engines if you'd  
prefer.  Note I've built many sites and I've not had a problem.

> It isn't like calling a function with an argument. It only pulls an  
> indexed value out of a list.

Yes, but what about using stuff[i] where i is an attribute (stuff[32]  
is probably useless)?  In that case you assume is an int.  What if  
you change to a string with a char like '2-321'?

> No matter how hard you try, you cannot make StringTemplate both  
> useful AND impossible to break separation.

Well i've made a few things that are grey area for usability, but  
actually I think I can 'cause I have done useful things w/o opening  
it up. ;)

> Instead I feel the goal should be to provide a path for the user  
> where they respect separation.

If you want an engine that only encourages it in the doc, there are  
plenty.

> Anything that breaks separation should be an obvious misuse of the  
> API, but it does not necessarily have to be impossible. Enforcing  
> separation certainly should not reduce the expressiveness of my  
> templates.

You mean shouldn't make it too hard to get work done.  Agreed.

> As I see ST now, it can do probably 70% of things well. 15% of  
> things are awkward and 15% of things are impossible without  
> refactoring the controller. ST lacks a lot of expressiveness and  
> that really shows when it is used for HTML. For example,  
> alternating table rows in ST doesn't work in the real world. Sure,  
> in documentation you can show how templates can be applied in a  
> round-robin fashion, but this requires duplicating the template  
> that outputs the row. In a real application this is probably not a  
> simple template, and I certainly don't want to copy and paste it  
> just so I can alternate row colors!

Really? No problem.  factor into more templates then pass arguments.   
easy.  just like function calls right?

Ter



More information about the stringtemplate-interest mailing list