GithubHelp home page GithubHelp logo

elk.erl's Introduction

Build status: http://travis-ci.org/vladimir-vg/elk.erl

Background

Initially this project was started as walrus improvement, but then I (Gordeev Vladimir) decided to use neotoma. This turned me to start project from scratch.

Features and compatability

elk.erl almost fully responds to mustache specs. It doesn't support custom delimeters ({{=<% %>=}}) and lambdas (as described in specs). Everything other supported as well (including partials, dotted keys {{person.name}} and dot-tag {{.}}).

Using elk.erl you can pass function to evaluate, instead of actual value. But this is not actually "lambdas" as described in specs.

Source =
    "Hello {{name}}\n"
    "You have just won {{value}} dollars!\n"
    "{{#in_ca}}\n"
    "Well, {{taxed_value}} dollars, after taxes.\n"
    "{{/in_ca}}",

{ok, Template} = elk:compile(Source),
Taxed = fun (State) -> elk:get(value, [<<"value">>], State)*0.6 end,
Context = {proplist, [
    {<<"name">>, "Chris"},
    {<<"value">>, 10000},
    {<<"in_ca">>, true},
    {<<"taxed_value">>, Taxed}]},
elk:render(Template, Context).

will output:

Hello Chris
You have just won 10000 dollars!
Well, 6000.0 dollars, after taxes.

Context format

By default you can pass your mappings as proplists ({proplist, YourPropList}). Also you can use other data structures. To do so you should write module that implements specific interface and put module name into config.

For example, if you want use erlang dict datastructure:

-module(elk_dict_context).

-export([get/2]).

get(Key, Dict) ->
    case dict:is_key(Key, Dict) of
        false -> undefined;
        true -> dict:fetch(Key, Dict)
    end.

and put in config:

{elk, [{contexts, [{proplist, elk_proplist_context}, {dict, elk_dict_context}]}]}

Now you can pass dicts as follows: elk:render(Template, {dict, YourDict}).

By default only one datastructure is defined:

{elk, [{contexts, [{proplist, elk_proplist_context}]}]}

Context module get/2 function should return undefined if no value found for given key and value otherwise.

Contribution

I'm (Gordeev Vladimir) not a native english speaker, so very possible I done a lot of grammar and lexical mistakes. Feel free to send a pull-request.

Also, if you think that some context datastructures should be available by default, you also can send a pull-request.

License

See LICENSE.MIT file.

File src/elk_mochinum.erl was taken from mochiweb repository. (Licensed on MIT, same license as for whole project).

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.