Re: Sharing constants in .java and .ftl files
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Monday, November 17, 2008, 5:18:53 PM, Luther Baker wrote: > On Mon, Nov 17, 2008 at 4:09 AM, Daniel Dekany <[email protected]> wrote: > Sunday, November 16, 2008, 9:18:58 PM, Luther Baker wrote: > > > <snip/> > > >> NEW STYLE: I'd like to do this - but I don't know what to do at >> the ???? below. >> >> Constants.java >> public class Constants >> { >> public static final String PROJECTS = "projects"; >> } >> >> .class: >> root.put(Constants.PROJECTS, projectList); >> >> .ftl >> <#list ?????[] as p> >> <li>${p.name}</li> >> </#list> > > Er... but the java coded is equivalent with the previous one... so the > FTL should be <#list projects[] as p> again. Are you sure this is what > you wanted to ask? > > > > My issue here is that ... I don't WANT to type the string 'projects'. > > I want to use refer to something like Constants.PROJECTS. > > Assuming an instance of Constants (constants) is visible in the > page, I want to do something like: > > >> <#list constants.PROJECT[] as p> >> <li>${p.name}</li> >> </#list> > > Does that help? > > IE: if I refactor the 'key' I am using in Java ... I don't want to > have to remember every location I am using that key in the ftl files > ... etc. So I've wrapped it into the Constant class .. only, that > looks funny ... would FTL recognize the above syntax and do the right thing? Anything is possible since you can write own TemplateModel implementations (it's easy, as TemplateModel interfaces are quite trivial). (More specifically, you will want to deal with TemplateModelHash mostly.) Again, it's not the business of the template language if from where and how the variables names and value come from, so, I don't know, it's even possible to solve that ${time} will return the current time (I mean, a different value each time), only you need a custom TemplateHashModel for the data-model root. Also we is this, maybe you can just use this: http://freemarker.org/docs/pgui_misc_beanwrapper.html#autoid_54 > I haven't tried ... as, my previous posts indicate I'm still getting > my head around how to get place POJOs into the namespace via the > <#assign directive. How is #assign related to... anything in this topic? It just reads a variable like ${foo} does or anything else, and then assigns it to another variable. > Thanks again, > > -Luther -- Best regards, Daniel Dekany ------------------------------------------------------------------------- This SF.Net email is sponsored by the Moblin Your Move Developer's challenge Build the coolest Linux based applications with Moblin SDK & win great prizes Grand prize is a trip for two to an Open Source event anywhere in the world http://moblin-contest.org/redirect.php?banner_id=100&url=/