Giter Club home page Giter Club logo

carrot's People

Contributors

codeka avatar dmfs avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

carrot's Issues

Expression starting with `!` fails

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

Add a default operator

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

String concatenation

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

Is carrot still alive?

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?

Android support

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.

Python-like conditional expressions

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

Add new process method

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 ?

boolean operator precedence

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.

Issue referencing Carrot from MVN Repository

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.

Better bindings interface

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 Bindings 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.

Support for include tag

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)?

Support for stripping whitespaces?

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

} disapear if alone in a line

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 }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.