Re: How to generate the output for a given java collection object
Rajvinder Pal <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Thanks, in that case i will prefer to write template with proper formating.I will use compress directive in template and let browser format the html display for me. Regards, Rajvinder From: Daniel Dekany <[email protected]> To: Rajvinder Pal <[email protected]> Cc: FreeMarker-user <[email protected]> Sent: Saturday, September 24, 2011 10:13 PM Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object Saturday, September 24, 2011, 5:00:00 PM, Rajvinder Pal wrote: > Thanks Daniel! finally i am able to get the expected output with your suggesstion:- > > <#list itemMap?keys as key> > ${key} > <#list itemMap(key) as item>${item} </#list> > </#list> > > I was trying to evaluate the FreeMarker for a requirement to > generate the html table dynamically.I felt FreeMarker is bit tricky > to solve the problem. Well, there are some old warts (legacies...) that users has to get over... the two main warts are that FTL has no map type (only a string-keys-only one) and that FTL doesn't have null (but treats null-s coming from Java as missing variables). > But the mailing list response is amazing.te > with respect to generating output i am facing one more challenge it > may be very silly to ask but since i am new to FreeMarker i want to > know, how can we write template with proper indention but still > able to get the get the output html in proper indention. Sadly, you can't... nor in any other template language that I know of. It's actually a rather tricky thing to do properly (much easier to do almost properly), unless the template language understands HTML/XML (FreeMarker doesn't), AND you are generating HTML/XML, and even then it would mean some runtime overhead. You may find some tricks here regarding white-space handling: http://freemarker.org/docs/dgui_misc_whitespace.html but they won't give you proper indentation. > for example suppose my template logic looks like:- > > for each bean > print <beean..... > <# if condition.....> > <#if condition .... > > <#if condition > > print <inner tag....... > > > > The above logic will print something similar to this:- > > <bean..... > <inner > > > > IN the above output, indention is too much for second . > How can we have template as well generated output in proper xml > type format? i mean with proper indention You had to apply something on the output Writer that understands the output format and reformats it... Of course, that will have its cost in resource usage. -- Best regards, Daniel Dekany > > Thanks, > Rajvinder > > > From: Daniel Dekany <[email protected]> > To: Rajvinder Pal <[email protected]> > Cc: FreeMarker-user <[email protected]> > Sent: Saturday, September 24, 2011 1:22 PM > Subject: Re: [FreeMarker-user] How to generate the output for a given java collection object > > The problem is that FTL, the template language, doesn't really support > maps... It only has a type that is like a Map<String, Object>, i.e., > the keys are String-s. It's horrible. Regardless, ?keys *happens* to > work even if the map keys are non-String. But the [] operator won't. > > What I always did is using BeansWraper. Where you configure FreeMarker > do this: > > Configuration cfg ... > ... > BeansWrapper wrapper = new BeansWrapper(); > wrapper.setSimpleMapWrapper(true); > cfg.setObjectWrapper(wrapper); > > Now you can use itemMap(key) to get items with non-String keys, so > your example will work. (Or, if you are using > wrapper.setSimpleMapWrapper(false), then you could just use the Java > API of the map, like itemMap.keySet() and itemMap.get(key), but then > ?keys and such will give you a mess of true keys and method names, so > I don't recommend that.) > > Note that wrapping XML nodes, Jython object and such won't be > automatic with this wrapper. Can be fixed with a custom one if you > care. > > An beware, HashSet is unsorted. It doesn't give back its items in the > order as you have added them. (It's a Java thing, nothing to do with > FreeMarker.) Use LinkedHashSet if you need to keep the order. > ------------------------------------------------------------------------------ All of the data generated in your IT infrastructure is seriously valuable. Why? It contains a definitive record of application performance, security threats, fraudulent activity, and more. Splunk takes this data and makes sense of it. IT sense. And common sense. http://p.sf.net/sfu/splunk-d2dcopy2 _______________________________________________ FreeMarker-user mailing list [email protected] https://lists.sourceforge.net/lists/listinfo/freemarker-user