0

I'm a StringTemplate newbie with a really basic. I'm trying to utilize this to send automated emails. I've read as much as I can to digest what is out there. I'm starting with a simple test case and having trouble getting properties of objects to render. As a test case I have the following in my template file email.stg.

delimiters "$", "$"
activate(person) ::= <<$person.personFirstName$>>

I'm trying to pass my Person object and have the template render the personFirstName property. This would call a getter Person.personFirstName() which is public.

My java code looks like:

Person _thePerson = //fetched from database
STGroup group = new STGroupFile(/tmp/email.stg); 
ST st = group.getInstanceOf("activate");
st.add("person", _thePerson);
System.out.println("the person first name is: " + _thePerson.personFirstName());
System.out.println(st.render());

My output reflects that the personFirstName property is available via java but my template does not render it.

the person first name is: Ivan
<nothing is returned here>

If I limit the activate template to this:

activate(person) ::= <<$person$>>

I get the following result where the person object is rendered as _thePerson.toString().

the person first name is: Ivan
999999999 - Johnson, Ivan G

Any help would be greatly appreciated so I can move on to the more complex template that I'm trying to get to.

1 Answers1

0

Answering my own question: I think this is the answer from the Introduction here - https://theantlrguy.atlassian.net/wiki/display/ST4/Introduction.

"...in general they follow the old JavaBeans naming convention. StringTemplate looks for methods getP(), isP(), hasP() first. If it fails to find one of those methods, it looks for a field called p."

I took that to mean that "p" would work as a method name as well but was wrong. I'm using Enterprise Object Framework and, unfortunately, my model .java files' attribute accessors do not use the "get*" convention which means ST never requests them. They are also not stored as fields. I'll have to think of a way around it but I don't think I'm inclined to change large scale model frameworks to accommodate this. If I add cover (get*) methods it works but that is not the best solution.

Generally, I've never encountered this issue since WebObjects template engine will render with or without "get*."