Comments (2)
As discussed, potential solutions:
- Extend the AST to track how exactly a
CallE
was invoked (function call, filter, test, macro invocation), and then hand that information to functions, which can then change their behavior accordingly. This would allow us to write agfnLower
that acts as an "all-lowercase" test when invoked throughis
, and as a "make lowercase" function otherwise. This is terrible. - Abandon the "tests and filters and functions and macros are the same thing" paradigm and put them in separate namespaces. Breaks the API though, and it would be a real shame, because for use cases where 100% jinja compatibility isn't needed, this interchangeability is actually super convenient (write a macro, use it as a filter, holy cow, that's awesome!)
- Add some magic to
is
that mangles the test name to prefer some kind of specially-named builtin (ideally one that you cannot produce from within the template). E.g.,foobar is lower
would then parse intoCallE (VarE "is-lower") [(Nothing, VarE "foobar")]
(note theis-
prefix on the variable name). This would work, but I'm really reluctant to add this kind of hard-to-explain magic.
So all the actual solutions kind of suck.
Additionally, I can't really think of a reasonable use case where you'd say "oh, hmm, I need to check whether everything in this string is lowercase" - the only situations I can think of are ones where you'd want to actually convert the string to lowercase anyway.
So I'm inclined to say that we should just accept this incompatibility (is lower
doing something somewhat unintuitive, and deviating from jinja), document it as such, and call it a day.
from ginger.
On further thought, I believe option 3 might not be so bad after all.
The thing is that there are more tests in Jinja2, and many of them do make sense in Ginger, like for example the is number
test.
So here's how I think it should work:
- The
is
syntax construct should take care of prependingis_
to the function name, thusfoo is bar
should be desugared tois_bar(foo)
. This also implies thatis
syntax is going to be a lot more restrictive than filter or function call syntax: only literal function names are allowed for the filter name, so unlike filters or functions, you cannot use arbitrary expressions as tests. This isn't going to be a huge problem, because Jinja2 has the same restriction. It also means that theis
construct should have very high precedence, i.e.,foo + bar is even
should parse asfoo + (bar is even)
, not(foo + bar) is even
. - Built-in tests will be exposed to the template with the
is_
prefix added. This also exposes them as plain old functions, true to the "tests and filters and functions are all the same thing" paradigm, but it avoids name clashes between things likelower
andis_lower
. - Tests, in this approach, can also be user-defined - as long as the
is_
prefix is added, it will work just fine.
from ginger.
Related Issues (20)
- Use Text instead of String in parser HOT 2
- Not using bytestring-builder internally? HOT 2
- Passing local variables while including a template? HOT 5
- Fraction of second in date
- Add support for records as GVal HOT 3
- Need help with (untyped ?) / heterogeneous dictionary as context to template
- a way to display a structured variable HOT 2
- Add documentation for for-loops HOT 2
- don't ignore non-existent variables HOT 3
- Best way to deal with lists and maps as variables HOT 1
- test/Text/Ginger/SimulationTests.hs:1589 "recursive loops" test fails: expected: "a@1(b@2()c@2(d@3()))" but got: "a@1(c@2(d@3())b@2())"
- How to render a template given template text and context with ToJSON? HOT 1
- GHC 9 build fails HOT 1
- Aeson 2 support HOT 8
- Local date handling accounting for DST HOT 5
- Lack of dependency constraints in old versions cause build failures
- join function? HOT 3
- Expired certificate in Ginger Docs
- Publish current version to Hackage? HOT 1
- Expressions in include statements for recursive templates?
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 ginger.