[antlr-interest] StringTemplate enhancement question

matthew ford Matthew.Ford at forward.com.au
Thu Jun 24 15:16:16 PDT 2004


Hi Ter,
As you know WebStringTemplates is heavily influenced by your WebTemplates.
WebStringTemplates V2 is diverging but
the basic concepts of separation still apply.

>
> I often have lists of things that need to be formatted, but the list
> items are actually pieces of data that are not already in an object.  I
> need ST to do something like:
>
> Ter=3432
> Tom=32234
> ....
>
> using template:
>
> $items:{$attr.name$=$attr.type$}$
>
> This example will call getName() on the objects in items attribute, but
> what if they aren't objects?  I have perhaps two parallel arrays
> instead of a single array of objects containing two fields.  One
> solution is allow Maps to be handled like properties so that attr.name
> would fail getName() but then see that it's a Map and do
> attr.get("name") instead.
>
> This very clean approach is espoused by some, but the problem is that
> it's a hole in my separation rules.  People can put the logic in the
> view because you could say: "go get bob's data" in the view:
>
> Bob's Phone: $db.bob.phone$
>
> A view should not be part of the program and hence should never be able
> to go ask for a specific person's data.  Humor me that it's good to be
> ruthless (or read my paper <snicker>).  Is there another way to fake
> aggregates by given separate pieces of data to ST and have it
> aggregate?

WebStringTemplates does just that.
Given db.bob.phone
it tries to get db from the attributes map.
if db is a map then it uses get("bob") to get the next level object
if db is an object it tries db.getBob() to get the next level object
and so on recursively.

Now you can only ask for a specific person's data if  you know it is  there.
One thing WebStringTemplates has that StringTemplates does not is the
concept of a Data Interface Definition file.

This DID is an html file that defines all the attributes that will be
available to the template and gives some sample data values.

The template designer uses it to design and test his template independent of
the server.
The server programmer uses it a the specification of the output he is
required
to produce.
Neither the template designer nor the server programmer cares what the
other is doing as long as they are both working to the same DID.

WebStringTemplates has methods for loading a attribute map from a DID
and for saving an attribute map as a DID

For the template designer the rule is
If db.bob.phone is in the DID, then you can use it.
If it is not, don't expect it to be available.
For the server programmer the rule is
If db.bob.phone is in the DID, then you need to make it available.
If it is not, then you don't.

I think you real problem is you want to supply a two lists of data that are
synchronized.
In WebStringTemplates (and StringTemplates) this is achieved by creating a
list of objects
that have attributes of Name and Type

It seems you are looking for a way to short cut creating the objects.
A list of  Maps seems the easiest.

ArrayList items = new ArrayList();
items.add(new HashMap() {{put("name","Ter"); put("type","342");}});
items.add(new HashMap() {{put("name","Tom"); put("type","1342");}});
items.add(new HashMap() {{put("name","Fred"); put("type","42");}});
attributeMap.add("items",items);

Then in the template use
$items:{$attr.name$=$attr.type$}$

WebStringTemplates V2 will use the syntax
$[list items]$
    $[name]$=$[type]$
$[endlist]$

a la PHP. Which is more readable (IMHO :-) )

>
> Again, though, how do I prevent people from passing in a regular
> HashMap that is actually just a proxy for their DB?  Then the view
> could do DB accesses!  Oh, I could make a special object that worked
> like a HashMap, but which wouldn't allow subclasses so people couldn't
> sneak in a modified version that was actually a database.
>
> Which would you choose?  Anybody have some insight?

Nothing wrong with passing a proxy to a DB.  As long as the DB conforms to
the
Data Interface Definition, the template does not care and does not know.
The template can only access data it knows about from the DID.  The template
cannot do general db accesses as it does not know anything about the other
fields that may be available.

As for  attributes like  "a.b"
WebStringTemplates will never
retrieve a single "a.b" attribute, WST will always get object "a" and then
from object "a" try and get  object "b".

As an asside I use  attributes like
items.comment in the
Data Interface Definition file to insert comments about the type of data to
expect for items
eg
 items.comment   a list of items and types to be defined

see http://webstringtemplates.com for more details on the DID html format
or http://www.forward.com.au/webstringtemplatese

all the best
matthew



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list