Using a JavaBean object in a template

119 views
Skip to first unread message

maku

unread,
Nov 14, 2009, 6:11:39 AM11/14/09
to Closure Templates Discuss
Hi,

I would like to use a JavaBean in a template (like the example code
below)

But it seems that this is not possible (in other common templates
libraries like velocity or freemarker this is no problem)

Could anybody of you give me a hint wheter this could be done with
this template engine (and how)?

TIA
Martin



example code:

SOY Template:

{namespace com.test.template}

/**
*
* @param user The user
*/
{template .UserProfile}
This is user {$user.name}!
{/template}


// USAGE:

...
UserBean bean=new UserBean("myname");
Map<String, Object> mapData=new HashMap<String, Object>();
mapData.put("user",bean);

String result=getSoyTofu().render
("com.test.template.UserProfile",mapData,null);
writer.write(result);
....


// BEAN def
public class UserBean {
private String name;

public UserBean(String name) {
this.name=name;
}
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}
}

Kai Huang

unread,
Nov 16, 2009, 4:58:28 PM11/16/09
to Closure Templates Discuss
In the current implementation of Closure Template for Java, you'll
need to convert your UserBean object into a SoyMapData object or a
Map<String, ?>.

The reason that Closure Templates doesn't support your UserBean class
directly is because we created Closure Templates with the idea of
keeping the usage model the same in both Java and JavaScript.

Nikita Tovstoles

unread,
May 26, 2011, 12:26:44 PM5/26/11
to closure-temp...@googlegroups.com
Hello,

Just started looking at Closure Templates (after integrating the very excellent Closure Compiler into our code base). Wondering whether the Templates team is still against supporting arbitrary POJOs as template model params (per below)?

Wouldn't dot notation work for both JS and Java cases? ie. 

/**
* @param u user object
*/
{$u.name} seemingly could apply to both java class User with property 'name' and JS object:

var User = function(name)
{
this.name = name;
}

var u = new User('Bob');

thanks!


Kai Huang

unread,
Jun 2, 2011, 9:13:03 PM6/2/11
to Closure Templates Discuss

On May 26, 9:26 am, Nikita Tovstoles <nikita.tovsto...@gmail.com>
wrote:
> Hello,
>
> Just started looking at Closure Templates (after integrating the very
> excellent Closure Compiler into our code base). Wondering whether the
> Templates team is still against supporting arbitrary POJOs as template model
> params (per below)?

We're not against it. We just don't feel it's particularly important
compared with other fixes/features we might do.
Reply all
Reply to author
Forward
0 new messages