Comments (9)
We are already using it in this way in our custom tags. I did a quick and dirty implementation like this:
// horrible name...
abstract class BeanTag extends Tag {
protected BeanTag(String name) {
super(name);
}
protected <T> T getFromContext(String key, TemplateContext context, Class<T> clazz) {
Map<String, Object> map = (Map<String, Object>) context.get(key);
if (map == null || map.isEmpty()) return null;
return MAPPER.convertValue(map, clazz);
}
}
And our tags just extend BeanTag
instead of Tag
Adding it to TemplateContext
would be just a little extra syntactic sugar, but not necessary.
from liqp.
Yes, that would be good to have. Check the PR and it's unit tests.
from liqp.
Maybe allow the ObjectMapper to be passed in or customized? For example, our looks like this:
new ObjectMapper().
registerModule(new Jdk8Module()).
registerModule(new JavaTimeModule()).
disable(FAIL_ON_UNKNOWN_PROPERTIES).
setSerializationInclusion(NON_ABSENT).
disable(WRITE_DATES_AS_TIMESTAMPS).
enable(ALLOW_COMMENTS).
setPropertyNamingStrategy(PropertyNamingStrategy.SNAKE_CASE).
setDateFormat(new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss-Z"));
Not all of that is relevant, but the Jdk8 and JavaTime modules probably are.
from liqp.
Another question. Will the value present in TemplateContext (like in a custom Tag) be the original object, or a Map? If it is a map, then we'll just be converting the map back to an object instead of JSON back to an object in our custom tags.
from liqp.
Maybe allow the ObjectMapper to be passed in or customized?
Ah yes, of course. Moved it to ParseSettings
so that you can set your own.
Will the value present in TemplateContext
I've not looked into this lib for a while, but I think it is. Give it a try.
from liqp.
Whoops, I accidentally merged it (and automatically closed this issue). Feel free to re-open if needed.
from liqp.
I'm getting a LinkedHashMap<String, Object> out of the TemplateContext instead of the instance of someComplexObject
. That's not ideal, but not horrible.
from liqp.
Maybe just add public T get(String key, Class<T> clazz)
to TemplateContext
?
But then TemplateContext
would also need ParseSettings
to access the ObjectMapper
from liqp.
I'm getting a LinkedHashMap<String, Object> out of the TemplateContext instead of the instance of someComplexObject. That's not ideal, but not horrible.
Ah, ok, I misunderstood. Yes, the instance is getting converted into a LinkedHashMap<String, Object>
.
Perhaps there is, but I can't imagine a use-case where this would be useful (keeping the instance instead of a LinkedHashMap<String, Object>
). Feel free to create a new issue for this, but it wouldn't be high on my list to implement (unless there is a good use-case for it).
from liqp.
Related Issues (20)
- Upgrade ANTLR HOT 11
- Template errors not being stored when using liquid for loops HOT 2
- Support for Liquid's Layouts HOT 3
- Integer and Float Comparisons After Using Times Filter HOT 15
- How to modify default renderer HOT 1
- Shaded jar should exclude non-ANTLR dependencies HOT 2
- Support for `liquid` tag HOT 1
- expression errors ignored in _some_ cases HOT 1
- Template parse is deprecated HOT 3
- Issues with the new release. HOT 2
- != working as == HOT 2
- Is Template (render) thread safe? HOT 2
- when text contains UNICODE characters the parsing fails HOT 1
- Parser error "Invalid Tag: 'liquid'" HOT 2
- Support Instant datetime pattern
- Support for Instant, LocalDateTime date pattern HOT 1
- BlockNode Map render issue: String.valueOf(Map<?,?>) returns '=' instead of ':' as delimiter HOT 12
- Strict typing enforcement cannot be fully disabled HOT 3
- Applying strip_html filter to escaped html will unescape the string HOT 6
- Variable Name with Hyphen Misinterpreted in Conditional Statements HOT 10
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from liqp.