Git Product home page Git Product logo

walrus's Introduction

Walrus - Mustache-like Templating

Walrus is 50% Mustache and 50% large flippered marine mammal.

Lexing is done with Leex and parsing is done with Yecc, both of which are included in newer versions of Erlang.

Most Mustache constructs work, such as variables, unescaped variables, blocks, and inverse blocks. However, The Walrus is an opinionated animal, and partials will never be supported. Also, functions passed into the context are simply evaluated and are not passed the raw template.

You can skip lexing and parsing templates every time you want to render them by using compile/1, which returns a renderer you can then pass a context that renders the template.

Examples

Simple Render

1> Tmpl = "Hello {{{name}}}.
1> 
1> Drinks:
1> 
1> {{#drinks}}
1>     - {{name}}, {{tastiness}}
1> {{/drinks}}".
"Hello {{{name}}}.\n\nDrinks:\n\n{{#drinks}}\n    - {{name}}, {{tastiness}}\n{{/drinks}}"
2> 
2> Ctx = [{name, "Devin & Jane"},
2>        {drinks, [[{name, "Beer"},
2>                   {tastiness, 5}],
2>                  [{name, "Juice"},
2>                   {tastiness, 8}]]}].
[{name,"Devin & Jane"},
 {drinks,[[{name,"Beer"},{tastiness,5}],
          [{name,"Juice"},{tastiness,8}]]}]
3> 
3> walrus:render(Tmpl, Ctx).
<<"Hello Devin & Jane.\n\nDrinks:\n\n\n    - Beer, 5\n\n    - Juice, 8\n">>

Compiled Renderer

1> Renderer = walrus:compile("Hello {{{name}}}.\n\nDrinks:\n\n{{#drinks}}\n    - {{name}}, {{tastiness}}\n{{/drinks}}").
#Fun<walrus.0.90102984>
2> Ctx = [{name, "Devin & Jane"},
2>        {drinks, [[{name, "Beer"},
2>                   {tastiness, 5}],
2>                  [{name, "Juice"},
2>                   {tastiness, 8}]]}].
[{name,"Devin & Jane"},
 {drinks,[[{name,"Beer"},{tastiness,5}],
          [{name,"Juice"},{tastiness,8}]]}]
3> 
3> Renderer(Ctx).
<<"Hello Devin & Jane.\n\nDrinks:\n\n\n    - Beer, 5\n\n    - Juice, 8\n">>

Acknowledgments

Robert Virding helped me tremendously when I struggled to understand Leex. He's also responsible for ([^{}]|({[^{])|(}[^}]))+.

License

All code released into the public domain (see UNLICENSE) except for the file walrus_mochinum.erl, which has it's own license (see LICENSE).

walrus's People

Contributors

blinkov avatar joewilliams avatar

Stargazers

 avatar

Watchers

 avatar  avatar

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.