Closed slisznia closed 9 years ago
Ah, interesting -- you really shouldn't have to do anything special, so I have altered the behavior for non-primitives to call their .toString() function by default when rendering.
Please use these release candidates until 2.6.5 is officially released:
http://www.x5software.com/chunk/releases/chunk-templates-2.6.5.jar http://www.x5software.com/chunk/releases/chunk-templates-2.6.5-src.jar
I also added a {$x|str} filter to explicitly feed the string representation into subsequent filters but this also will not be necessary except in some very rare cases.
If the object in question has no .toString() method (other than the base definition in Object) then this is probably not the right behavior, but given the utility of this change, I'll just address that in a later release.
Let me know if this meets your needs! Thanks, -Tom
The proposed "toString" auto conversion helps a ton! It leads to another question. BigDecimal's toString outputs numbers in scientific notation as one example, but BigDecimal is often used in financial apps,for currency values, so a scientific notation is not desired. Similarly, DateTime (Joda) makes certain assumptions in toString about how to format the date. This is a general problem.
If you can think of providing an extra level of indirection -- for those cases where plain "toString" is not cutting it -- a hook perhaps to inject own serializer for a concrete class so that the user could control how to format the output, that would be great. It would be ideal to be able to provide that custom serializer, optional parameters (in the document itself, tag/filter) so that the serializer could apply different precision, rounding, or locale, depending on the context.
Yes! You can actually register custom filters in your theme that include advanced logic for rendering specific types of objects. For pojo data there might be an unwrapping issue, I'll take a look at that today.
Okay, released 3.0.0 (new major version since I did some cleanup that was not back-compatible) -- give it a try, and take a look at updated docs for writing your own filters:
http://www.x5software.com/chunk/examples/ChunkExample?example=contrib
There is a new example for how you might add a {$x|date(...)}
filter for formatting java.util.Date objects (you will have to roll your own for Joda DateTime but it shouldn't be too difficult).
Thanks for the feature requests! I was pretty dissatisfied with the initial POJO solution and I'm feeling a lot happier about the 3.0.0 release.
almost forgot:
public class BigDecimalFilter extends ObjectFilter
{
public String getFilterName()
{
return "bignum";
}
public Object transformObject(Chunk chunk, Object object, FilterArgs args)
{
if (object instanceof BigDecimal) {
BigDecimal big = (BigDecimal)object;
return big.toPlainString();
} else {
return "ERR: NOT A BIG DECIMAL";
}
}
}
Looks like this is exactly what we were looking for! Will check out 3.0.0 later today. On Jun 27, 2015 5:07 PM, "Tom McClure" notifications@github.com wrote:
almost forgot:
public class BigDecimalFilter extends ObjectFilter { public String getFilterName() { return "bignum"; } public Object transformObject(Chunk chunk, Object object, FilterArgs args) { if (object instanceof BigDecimal) { BigDecimal big = (BigDecimal)object; return big.toPlainString(); } else { return "ERR: NOT A BIG DECIMAL"; } } }
— Reply to this email directly or view it on GitHub https://github.com/tomj74/chunk-templates/issues/5#issuecomment-116160338 .
One of our POJO classes has a BigDecimal public field. How to add a hook to output its value? Thanks!