Giter Site home page Giter Site logo

zalando / beard Goto Github PK

View Code? Open in Web Editor NEW
121.0 121.0 30.0 462 KB

A lightweight, logicless templating engine, written in Scala and inspired by Mustache

License: Other

Scala 89.86% ANTLR 4.00% FreeMarker 1.41% HTML 3.33% Handlebars 0.52% Pug 0.35% Mustache 0.53%
web

beard's People

Contributors

andreyfurdylo avatar boopathi avatar cesarla avatar danpersa avatar endeneu avatar fokusferit avatar igneus avatar jmhofer avatar kaygorodov avatar lappleapple avatar leviferreira avatar michaelkrog avatar mo-gr avatar ngbinh avatar perploug avatar rweyand avatar slavaschmidt avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

beard's Issues

0.3 on Scala 2.12

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?

Need support to scala 2.13..

[error] (update) sbt.librarymanagement.ResolveException: unresolved dependency: de.zalando#beard_2.13;0.2.0: not found

๐Ÿ˜‚

filters

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.

Inappropriate named parameter in ForStatement

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.

Parser error

I have an error / warning when I run the tests:

line 1:2 token recognition error at: '9'

the master branch.

Blocks in between comments are removed

<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

Add a way to get the index of an element within a {{for}} loop

  • Option A
    • ๐Ÿ‘ Follows the beard notation
    • ๐Ÿ‘Ž It can collide with already existing attributes
{{for element in elements}}
{{element.index}}
{{/for}}
  • Option B (Jinja2-alike)
    • ๐Ÿ‘ It doesn't populate the namespace
    • ๐Ÿ‘Ž Adds a new keyword
{{for element in elements}}
{{loop.index}}
{{/for}}

Comments Feature

Consider changing the comment delimiters from {{# and #}} to {{- and -}}

Published maven pacakge not compatible with JVM older than Java 14

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

ClasspathTemplateLoader doesn't resolve paths relative to the root folder

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)

http://stackoverflow.com/questions/13269556/strange-behavior-of-class-getresource-and-classloader-getresource-in-executa

Publish for Scala 2.12

Please publish beard for Scala 2.12.

At first glance, all dependencies seem to already exist for Scala 2.12.

i18N in beard

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.

XSS prevention

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.

401 error on docs

Why do I get a 401 error when I click the documentation link? Doesn't really inspire confidence.

Chaining filters

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

Documentation

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

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.