zalando / beard Goto Github PK
View Code? Open in Web Editor NEWA lightweight, logicless templating engine, written in Scala and inspired by Mustache
License: Other
A lightweight, logicless templating engine, written in Scala and inspired by Mustache
License: Other
Beard looks like a great library and we would love to use it. Unfortunately we're stuck on Scala 2.12 for the time being, is there any reason this library can't be released for 2.12?
[error] (update) sbt.librarymanagement.ResolveException: unresolved dependency: de.zalando#beard_2.13;0.2.0: not found
๐
Benchmark beard against:
https://github.com/mwunsch/handlebars.scala
And maybe other frameworks.
I was thinking about adding filters like ind handlebars, angular or twig.
basically they are functions for simple text manipulation in order to only have one value in th emodel but various way to represent it.
e.g
{{ name | titleCase }}
{{ value | currency(eur) }}
that was angular and twig style, while handlebars allows functions to be called as values
e.g.
{{ titleCase name }}
{{ currency value "eur" }}
for completeness sake this is the mustache approach to functions:
{{#titleCase}} name {{/titleCase}}
I prefer the twig/angular implementation as it is way clearer that those are functions and the first identifier you see is the actual value to be rendered.
I'm new to scala but I was thinking about having a type alias (Filter) for function with arbitrary arity that should always return a string. So we can strictly check for them and only allow Filters to be used that way avoiding unwanted behaviors.
By some oversight, the parameter of type Identifier
in case class ForStatement
was inappropriately named iterator
:
final case class ForStatement(iterator: Identifier, index: Option[Identifier], collection: CompoundIdentifier,
statements: Seq[Statement] = Seq.empty, addNewLine: Boolean = false) extends Statement
It should be named identifier
.
In the maven repository, the file https://dl.bintray.com/zalando/maven/de/zalando/beard_2.13/0.3.1/beard_2.13-0.3.1.pom use antlr-runtime 4.7.2. But the build.sbt use antlr version is 4.8-1. This is normal?
I have this warning when I try to launch the program.
ANTLR Tool version 4.7.2 used for code generation does not match the current runtime version 4.8
The problem is solved when I use the package beard that I compile in my local machine.
I have an error / warning when I run the tests:
line 1:2 token recognition error at: '9'
the master branch.
This Beard template:
{{for user in users}}
<div>Hello {{user.name}}</div>
{{/for}}
is rendering:
<div>HelloDan</div>
<div>HelloCesar</div>
<div>First</div>
{{# this for should render an end of line after each line in the iteration #}}
<div>Hello</div>
{{# this for is similar with the first one #}}
<div>Last</div>
is rendered
<div>First</div>
<div>Last</div>
instead of
<div>First</div>
<div>Hello</div>
<div>Last</div>
see the div-in-between-comments-bug
branch for failing tests
For example:
{{for user in users}}
{{/for}}
The render result is:
This is fine if the rendering is contained within tags but it cannot be used as a generic listing template. For example to list imports as in previous issue
{{for element in elements}}
{{element.index}}
{{/for}}
{{for element in elements}}
{{loop.index}}
{{/for}}
Consider changing the comment delimiters from {{#
and #}}
to {{-
and -}}
I'm running into this issue trying to use bloop, as it's not compatible with JDK 14, but I imagine anyone trying to use something older than java 14 will encounter it.
Here's a reproduction:
โ bearddebug wget https://dl.bintray.com/zalando/maven/de/zalando/beard_2.13/0.3.1/beard_2.13-0.3.1.jar
โ bearddebug jar xf ./beard_2.13-0.3.1.jar
โ bearddebug javap -verbose de.zalando.beard.BeardLexer | grep major
major version: 57
I am trying to retrieve the package from the Bintray repository but it seems that the service is no longer available.
I am trying to check in maven.zalando.net but seems that is not there either, can you please help me with this?
Thanks
I have the following file structure inside src folder:
.
โโโ main
โย ย โโโ resources
โย ย โย ย โโโ logback.xml
โย ย โย ย โโโ templates
โย ย โย ย โโโ index.html
โย ย โโโ scala
โย ย โโโ org
โย ย โโโ zalando
โย ย โโโ miadidas
โย ย โโโ customizer
โย ย โโโ Application.scala
โโโ test
โย ย โโโ resources
โย ย โโโ scala
The ClasspathTemplateLoader is setup in the following manner:
val templateLoader = new ClasspathTemplateLoader(
templatePrefix = "templates/",
templateSuffix = ".html")
now when I do
templateCompiler.compile(TemplateName("index"))
I get:
java.lang.IllegalStateException: Could not find template with name TemplateName(index)
at de.zalando.beard.renderer.CustomizableTemplateCompiler$$anonfun$1.apply(DefaultTemplateCompiler.scala:39)
at de.zalando.beard.renderer.CustomizableTemplateCompiler$$anonfun$1.apply(DefaultTemplateCompiler.scala:34)
at scala.util.Try$.apply(Try.scala:192)
at de.zalando.beard.renderer.CustomizableTemplateCompiler.compile(DefaultTemplateCompiler.scala:33)
at org.zalando.miadidas.customizer.Application$$anonfun$1.applyOrElse(Application.scala:29)
at org.zalando.miadidas.customizer.Application$$anonfun$1.applyOrElse(Application.scala:24)
at org.http4s.package$HttpService$$anonfun$apply$1.apply(package.scala:56)
at org.http4s.package$HttpService$$anonfun$apply$1.apply(package.scala:56)
at scalaz.Kleisli$$anonfun$flatMap$1.apply(Kleisli.scala:51)
at scalaz.Kleisli$$anonfun$flatMap$1.apply(Kleisli.scala:51)
at org.http4s.server.blaze.Http1ServerStage$$anonfun$runRequest$1.apply(Http1ServerStage.scala:110)
at org.http4s.server.blaze.Http1ServerStage$$anonfun$runRequest$1.apply(Http1ServerStage.scala:110)
at scalaz.concurrent.Task$.Try(Task.scala:403)
at scalaz.concurrent.Task$$anonfun$apply$25.apply(Task.scala:300)
at scalaz.concurrent.Task$$anonfun$apply$25.apply(Task.scala:300)
at scalaz.concurrent.Future$$anonfun$apply$15$$anon$4.call(Future.scala:380)
at scalaz.concurrent.Future$$anonfun$apply$15$$anon$4.call(Future.scala:380)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
The reason for it is that inside ClasspathTemplateLoader
there is a code
val path = s"${templatePrefix}${templateName.name}$templateSuffix"
val resource = Option(getClass.getResourceAsStream(path)) // <-- does lookup relative to the `CustomizableTemplateCompiler.class` name
where getClass.getResourceAsStream("templates/index.html")
returns null
The fix could be to replace getClass.getResourceAsStream
with getClass.getClassLoader.getResourceAsStream
(proven in the debugger that latter returns a non-null value)
Please publish beard for Scala 2.12.
At first glance, all dependencies seem to already exist for Scala 2.12.
Since filters will be part of the language, one could also investigate i18n as a feature.
Regarding AngularJS (since we borrowed the syntax for filters from there), we could also implement i18n according to a very common AngularJS filter called "translate".
What happens is, that the used locale is provided in the global context of the renderer. A JSON-object mapping locales to sets of keys for localized messages is provided. By passing the message keys to the according filter, the localized messages/strings are resolved.
Here is the repository of the mentioned translation filter: https://github.com/angular-translate/angular-translate.
I would like to start a discussion around XSS prevention.
Currently, if one is using untrusted data in the template rendering, than we could have problems with XSS.
What I propose is to escape every value, unless explicitly told not to.
That would be backward incompatible, since what can be seem as an improvement could break current features. Since it's not 1.0 yet, maybe it's not a big problem.
The proposal goes as follow:
{{ safe the.title }}
would not escape the content. Otherwise all content would be escaped.
If you think this is something we should add to the project, I would move on to discussing implementation details.
Why do I get a 401 error when I click the documentation link? Doesn't really inspire confidence.
Provide better errors for the users
unless
should do the opposite of if
Hi,
It seems that there is a problem while chaining filters
The following code
{{test | title | reverse}} <br>
{{test | reverse | title}} <br>
{{test | title}} <br>
{{test | reverse}} <br>
With context { "test": "test me without reverse"}
Results :
esrever tuohtiw em tset
Test Me Without Reverse
Test Me Without Reverse
esrever tuohtiw em tset
At the moment, Beard just throws a massive and clueless Stack Trace (Really useful for the Bard-Developers, but not for the final users).
Add support for the {{ comment }} statement.
Example:
{{comment}}Inline Comment{{/comment}}
{{comment}}
Multiline Comment
{{/comment}}
Allow if statements to evaluate expressions. Consider using paraboiled2
Presence or absence of the empty line(s) before the {{for}} cycle affects contents of the body of the cycle
Please see an example at https://gist.github.com/slavaschmidt/f0b93957f6f26dbab4e8
Hello,
I just noticed that the documentation contains informations on filters when they have not yet been released in 0.0.6
I think it would be better to be able to choose specific release documentation
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.