codeka / carrot Goto Github PK
View Code? Open in Web Editor NEWA Java templating engine, similar in syntax to Jinja.
License: Other
A Java templating engine, similar in syntax to Jinja.
License: Other
I have the following block in a for
block of my template:
{% if ! loop.last %},{% end %}
I would expect that this appends a ,
if the iterated item it not the last one.
Instead I'm getting shit exception:
au.com.codeka.carrot.CarrotException: Exception parsing statement for 'if' [! loop.last]
at au.com.codeka.carrot.tmpl.TagNode.create(TagNode.java:84)
at au.com.codeka.carrot.tmpl.TagNode.create(TagNode.java:70)
at au.com.codeka.carrot.tmpl.TemplateParser.parse(TemplateParser.java:41)
at au.com.codeka.carrot.tmpl.TemplateParser.parse(TemplateParser.java:59)
at au.com.codeka.carrot.tmpl.TemplateParser.parse(TemplateParser.java:59)
at au.com.codeka.carrot.tmpl.TemplateParser.parse(TemplateParser.java:59)
at au.com.codeka.carrot.tmpl.TemplateParser.parse(TemplateParser.java:21)
at au.com.codeka.carrot.CarrotEngine.process(CarrotEngine.java:83)
at au.com.codeka.carrot.CarrotEngine.process(CarrotEngine.java:115)
at au.com.codeka.carrot.CarrotEngine.process(CarrotEngine.java:130)
at org.dmfs.android.carrot.demo.DemoActivity.click(DemoActivity.java:43)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: au.com.codeka.carrot.CarrotException: 2131099649
27: ! loop.last
^
Variable, number, string or expression expected., found: NOT
at au.com.codeka.carrot.expr.Tokenizer.unexpected(Tokenizer.java:111)
at au.com.codeka.carrot.expr.StatementParser.parseFactor(StatementParser.java:202)
at au.com.codeka.carrot.expr.StatementParser.parseTerm(StatementParser.java:213)
at au.com.codeka.carrot.expr.StatementParser.parseComparator(StatementParser.java:231)
at au.com.codeka.carrot.expr.StatementParser.parseOrCond(StatementParser.java:129)
at au.com.codeka.carrot.expr.StatementParser.parseAndCond(StatementParser.java:120)
at au.com.codeka.carrot.expr.StatementParser.parseNotCond(StatementParser.java:111)
at au.com.codeka.carrot.expr.StatementParser.parseExpression(StatementParser.java:107)
at au.com.codeka.carrot.tag.IfTag.parseStatement(IfTag.java:33)
at au.com.codeka.carrot.tmpl.TagNode.create(TagNode.java:82)
... 21 more
Will it possible to add a default operator as
{{ myStringVar | "default" }} => if myString is null or empty, the output value is "default"
{{ myIntegerVar | 123 }} => if myInteger is null, the output value is "123"
Thx
Hello,
How concatenate string variables?
I don't find an example explaining that.
For example
{% set a = "aaa" %}
{% set b = "bbb" %}
{% set c = a + b %} ==> this lead to an error :(
Thanks
I use carrot and like its capabilities but I wonder if this project is still alive as I see no modification, no fix and no enhancement since a while?
Would you accept a PR which replaces some dependencies with Android compliant code? We're considering to use this library in an Android project (OpenTasks), but some dependencies are not available on current and older platform versions. At present the only problematic classes appear to be java.nio.file.Paths and java.nio.file.Path, which are not available on Android so far.
Also we'd like to replace lambdas, because support for this in Android didn't fully arrive yet.
Ideally we don't have to maintain our own fork but can use the upstream build, hence the question.
Hello,
I need to use variables with a character in their naming as for example {{app.name}}
Reasons of compatibility, etc...
How can I handle this? Is there a configuration to avoid/remove the accessor mechanism?
Thx
Any plans to add support for filters like in Jinja (see http://jinja.pocoo.org/docs/2.9/templates/#filters)?
Just out of curiosity, what's the point of mutable global Bindings?
Why would I want to add something to the global bindings after creating a CarrotEngine
? I can always just update the Bindings
(e.g. with Composite
bindings) which I pass to process
if I need additional bindings.
IMO, it's preferable to preserve immutability whenever possible.
I propose an alternative syntax for blocks like these (when you just want to echo something based on a simple condition) like in these cases
{% if ! loop.last %},{% end %}
{% if value %}{{ value }}{% else %}No value{% end %}
A more compact notation would be this:
render "" (empty string) if loop.last is true, render "," otherwise:
{{ loop.last and "" or "," }}
render `value` if value is non-null and non-empty, render "No Value" otherwise
{{ value or "No Value" }}
The actual value type of the expression is the type of the value which ultimately makes the expression true
. So boolean operators don't automatically result in a boolean. The boolean evaluation is only done when used in such a context (like in an if
statement).
I'm using or
and and
to distinguish them from the boolean operators (Python uses the same). I would be ok if the implementation uses &&
and ||
instead, because semantically it doesn't make much of a difference.
IRRC, Django and Jinja support this kind of notation too.
Here is how it works on Python
>>> x = True and "X" or "Y"
>>> x
'X'
>>> x = False and "X" or "Y"
>>> x
'Y'
>>> x = False or "Y"
>>> x
'Y'
>>> x = True or "Y"
>>> x
True
>>> x = True and "Y"
>>> x
'Y'
>>> x = False and "Y"
>>> x
False
My template in already in memory, will it be possible to add a new process method signature with a template as a string as an input argument ?
The boolean operators &&
and ||
don't appear to follow the common precedence rules. In particular the last one of these test fails:
Map<String, Object> context = ImmutableMap.of("true", (Object) true, "false", false);
// tests that pass:
assertThat(render("{{ true && true }}", new MapBindings(context))).isEqualTo("true");
assertThat(render("{{ true || false }}", new MapBindings(context))).isEqualTo("true");
assertThat(render("{{ false || true }}", new MapBindings(context))).isEqualTo("true");
assertThat(render("{{ (false && false) || true }}", new MapBindings(context))).isEqualTo("true");
// failing test:
assertThat(render("{{ false && false || true }}", new MapBindings(context))).isEqualTo("true");
The common order of boolean operators is "and" before "or", see https://en.wikipedia.org/wiki/Logical_connective#Order_of_precedence
so false && false || true
should be evaluated like (false && false) || true
which is true
.
How can I escape java operators as ^, -, etc in a variable name?
Same question for the dot
Example
{{myVar-v1.1}}
I can't get Carrot version 2.4.2 to compile with Gradle by adding the dependency compile 'au.com.codeka:carrot:2.4.2
.
In addition, when I go to the MVN Repository site, there doesn't seem to be any .jar file, whereas I do find all the compiled files for other versions. Am I missing something or are the files missing at MVN Repository?
Thank you in advance.
Would you consider a dedicated Binding
or Context
interface instead of a Map
?
I presume the only method of Map
you use is get(key)
, which makes a Map
sort of overkill if I want to compose my bindings from various sources. A Function
would be more appropriate, although I'd prefer a custom interface as stated above.
The interface would look like this
public interface Binding
{
Object value(String key);
}
This way it's much easier to create adapters and composite Binding
s and you don't need to copy all the context values to the map.
Here is how it could be used
new Composite(
new MapBinding(map),
new JsonBinding(json),
new SingletonBinding("@app", new AppBinding(getContext)),
new SingletonBinding("@intent", new IntentBinding(intent)));
Without copying any values, these adapters would give me access to all values in the map, jsonfile, app meta data and intent fields.
I've created something like this (but much less sophisticated) a couple of years ago: https://github.com/dmfs/android-xml-magic/tree/master/src/org/dmfs/android/xmlmagic/tokenresolvers
In my case a TokenResolver
had a similar function to your Binding
. We had TokenResolvers
for nearly every structure an Android project can have. This allowed me to add a Cursor
TokenResolver
like so new CursorTokenResolver(cursor)
and access every column in that cursor like so @cursor:customer_name
.
Is there any support for an include
tag like in Jinja?
I couldn't find any. Is there any other way to achieve the same (other than a custom tag)?
Something like:
{{ value ? "true" : "false" }}
Hi,
Is there any way to strip whitespaces as in jinpa
?:
http://jinja.pocoo.org/docs/2.9/templates/#whitespace-control
I have template containing lines like this:
{% if hasPrivacy %}{{ privacyKey }}: {{ privacy }}{% end %}
{% if hasStatus %}{{ statusKey }}: {{ status }}{% end %}
{% if hasUrl %}{{ url }}{% end %}
But even if the has*
property is false an empty line is added and I need to avoid that.
I tried the -
signs inside the if
, end
tags but I got exception.
Do you maybe have any tip how to solve this in other way?
Thanks
Hello,
I have this bash code in an include
installProduct() {
mkdir -p {{aa}}
tar xvf {{bb}} --dir {{cc}}
}
and in my main
{% include "myfile.inc.sh" %}
and the result after calling carrot is
installProduct() {
mkdir -p AA
tar xvf BB --dir CC
The line containing the } without space after disapears!
I found a workaround by adding a space after the }
Hi, I saved my template to in database. I want to load the template as string and evaluate by carrot library.
How can I get it ?
Thank you for your in advance.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.