Code Monkey home page Code Monkey logo

mustache.ex's Introduction

Mustache

Module Version Hex Docs Total Download License Last Updated

Minimal templating with {{mustaches}} in Elixir - http://mustache.github.com/

Prerequisites

  • Elixir 1.x

Installation

In your mix.exs, add Mustache as dependency:

defp deps do
  [{:mustache, "~> 0.5.0"}]
end

Example

Mustache.render("Hello, my name is {{name}}", %{name: "Alice"})

Tests

The test folder contains tests showing the currently implemented feature set. The tests are taken from the mustache specs project https://github.com/mustache/spec.

Run currently passing tests:

$ mix test --exclude pending:true

Run all tests:

$ mix test

Copyright and License

Copyright (c) 2023 Jan Schulte

This work is free. You can redistribute it and/or modify it under the terms of the MIT License. See the LICENSE.md file for more details.

mustache.ex's People

Contributors

kianmeng avatar marcelog avatar mtaylor91 avatar mveytsman avatar obahareth avatar schultyy avatar space-pope avatar theodowling 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

Watchers

 avatar  avatar  avatar  avatar  avatar

mustache.ex's Issues

Support for iterating over lists containing maps?

It looks like there is support for iterating over arrays containing objects in the Mustache documentation.

From the docs:

Template:

{{#repo}}
  <b>{{name}}</b>
{{/repo}}
Hash:

{
  "repo": [
    { "name": "resque" },
    { "name": "hub" },
    { "name": "rip" }
  ]
}
Output:

<b>resque</b>
<b>hub</b>
<b>rip</b>

However, it does not look like this library supports iterating over lists containing maps when rendering the template. Just wondering if this is indeed the case? Thank you

Escape characters using triple brackets

I have this test

template_attrs = %{
  # notice here how I print `partials.marketing_text`
  html_body:
    "<b>Welcome</b> {{data.username}}!, enjoy a good reputation, {{partials.marketing_text}}",
  text_body:
    "Welcome {{data.username}}!, enjoy a good reputation, {{partials.marketing_text}}"
}

partial_attrs = %{
  name: "marketing_text",
  html: "<b>Purchase Now!</b>: {{data.address}}",
  text: "Purchase Now!: {{data.address}}"
}

template = insert(:template, template_attrs)
partial = insert(:partial, partial_attrs)

assert {:ok, template} = Template.add_partials(template, [partial])

{:ok, email} =
  @from
  |> Email.new(@to)
  |> Email.with_template(template.name, @options)

assert email.html_body ==
         "<b>Welcome</b> tokarev!, enjoy a good reputation, <b>Purchase Now!</b>: POBOX 54634"

assert email.text_body ==
         "Welcome tokarev!, enjoy a good reputation, Purchase Now!: POBOX 54634"

What I need you to notice from here is that I have this code

  html_body:
    "<b>Welcome</b> {{data.username}}!, enjoy a good reputation, {{partials.marketing_text}}",

Where basically I take the html rendered text of the partial and renderer in that section.

The issue is this one.

This code should fail because only when I use {{{partials.marketing_text}}} is when the html should be escaped.

Doesn't work for non-atom keys

When used with non-atom keys, it will not substitute anything and simply treat the string as if it didn't exist:

iex(1)> Mustache.render("Hello {{person}}!", %{"person" => "John"})
"Hello !"

Correct behavior only when used with an atom key:

iex(2)> Mustache.render("Hello {{person}}!", %{:person => "John"})
"Hello John!"

Rendering fails with no errors

I'm building a Base16 builder in Elixir and I've come across a case where rendering is failing with no errors. Here's my test case:

defmodule TestMustache do 
  def test_mustache() do
    data = %{
      "base04-rgb-b" => 109.0,
      "base07-rgb-g" => 23.0,
      "base00-dec-b" => 0.9568627450980393,
      "scheme-name" => "Atelier Cave Light",
      "base0C-rgb-r" => 57.0,
      "base00-dec-r" => 0.9372549019607843,
      "base0C-dec-g" => 0.5450980392156862,
      "base0B-rgb-r" => 42.0,
      "base04-rgb-r" => 101.0,
      "base04-hex-r" => "65",
      "base0B-hex" => "2a9292",
      "base0E-hex" => "955ae7",
      "base0E-dec-b" => 0.9058823529411765,
      "base0F-dec-b" => 0.7490196078431373,
      "base0D-hex-r" => "57",
      "base07-hex-b" => "1c",
      "base08-dec-g" => 0.27450980392156865,
      "base08-hex-r" => "be",
      "base05-rgb-b" => 96.0,
      "base0E-dec-g" => 0.35294117647058826,
      "base01-dec-r" => 0.8862745098039215,
      "base0F-rgb-r" => 191.0,
      "base09-hex-g" => "57",
      "base09-rgb-r" => 170.0,
      "base01-hex-r" => "e2",
      "base01-hex" => "e2dfe7",
      "base06-dec-r" => 0.14901960784313725,
      "base05-hex-r" => "58",
      "base05-dec-g" => 0.3215686274509804,
      "base01-rgb-b" => 231.0,
      "base0A-rgb-r" => 160.0,
      "base00-hex-g" => "ec",
      "base0C-hex-r" => "39",
      "base02-dec-r" => 0.5450980392156862,
      "base0C-dec-b" => 0.7764705882352941,
      "base07-dec-r" => 0.09803921568627451,
      "base04-dec-g" => 0.37254901960784315,
      "base0B-hex-g" => "92",
      "base00-rgb-g" => 236.0,
      "base07-hex-r" => "19",
      "base0D-rgb-g" => 109.0,
      "base07-hex-g" => "17",
      "base0C-hex-b" => "c6",
      "base09-rgb-b" => 60.0,
      "scheme-author" => "Bram de Haan (http://atelierbramdehaan.nl)",
      "base04-hex-b" => "6d",
      "base02-rgb-r" => 139.0,
      "base00-hex" => "efecf4",
      "base03-hex-r" => "7e",
      "base08-hex-b" => "78",
      "base03-hex-g" => "78",
      "base0A-rgb-b" => 59.0,
      "base06-rgb-b" => 42.0,
      "base0C-rgb-g" => 139.0,
      "base0B-dec-r" => 0.16470588235294117,
      "base04-rgb-g" => 95.0,
      "base0E-rgb-r" => 149.0,
      "base06-hex-b" => "2a",
      "base0D-rgb-r" => 87.0,
      "base0B-rgb-g" => 146.0,
      "base09-dec-b" => 0.23529411764705882,
      "base00-hex-r" => "ef",
      "base02-dec-g" => 0.5294117647058824,
      "base08-dec-r" => 0.7450980392156863,
      "base0A-hex-r" => "a0",
      "base09-hex" => "aa573c",
      "base06-dec-b" => 0.16470588235294117,
      "base07-hex" => "19171c",
      "base05-dec-r" => 0.34509803921568627,
      "base04-hex" => "655f6d",
      "base01-rgb-g" => 223.0,
      "base02-dec-b" => 0.5725490196078431,
      "base0A-hex" => "a06e3b",
      "base0F-rgb-b" => 191.0,
      "base07-dec-b" => 0.10980392156862745,
      "base06-rgb-g" => 35.0,
      "base08-rgb-g" => 70.0,
      "base07-rgb-b" => 28.0,
      "base0F-dec-g" => 0.25098039215686274,
      "base05-dec-b" => 0.3764705882352941,
      "base0C-dec-r" => 0.2235294117647059,
      "base02-hex" => "8b8792",
      "base0A-dec-b" => 0.23137254901960785,
      "base0E-rgb-g" => 90.0,
      "base02-hex-b" => "92",
      "base06-hex-g" => "23",
      "base0F-rgb-g" => 64.0,
      "base06-hex" => "26232a",
      "base0C-rgb-b" => 198.0,
      "base04-dec-b" => 0.42745098039215684,
      "base0A-rgb-g" => 110.0,
      "base05-hex-g" => "52",
      "base01-dec-b" => 0.9058823529411765,
      "base0B-rgb-b" => 146.0,
      "base07-dec-g" => 0.09019607843137255,
      "base01-hex-b" => "e7",
      "base00-hex-b" => "f4",
      "base03-hex" => "7e7887",
      "base0E-hex-b" => "e7",
      "base05-hex-b" => "60",
      "base03-rgb-b" => 135.0,
      "base0A-hex-b" => "3b",
      "base02-hex-r" => "8b",
      "base03-hex-b" => "87",
      "base02-rgb-b" => 146.0,
      "base07-rgb-r" => 25.0,
      "base0F-hex-r" => "bf",
      "base01-rgb-r" => 226.0,
      "base03-rgb-g" => 120.0,
      "base02-rgb-g" => 135.0,
      "base0F-hex-g" => "40",
      "base0E-hex-g" => "5a",
      "base0B-hex-r" => "2a",
      "base06-hex-r" => "26",
      "base04-hex-g" => "5f",
      "base0A-hex-g" => "6e",
      "base09-dec-g" => 0.3411764705882353,
      "base08-hex-g" => "46",
      "base09-hex-r" => "aa",
      "base0F-hex" => "bf40bf",
      "base03-dec-b" => 0.5294117647058824,
      "base09-rgb-g" => 87.0,
      "base0C-hex" => "398bc6",
      "base0E-hex-r" => "95",
      "base0D-hex-b" => "db",
      "base06-rgb-r" => 38.0,
      "base0E-rgb-b" => 231.0,
      "base0A-dec-r" => 0.6274509803921569,
      "base01-dec-g" => 0.8745098039215686,
      "base06-dec-g" => 0.13725490196078433,
      "base0E-dec-r" => 0.5843137254901961,
      "base09-dec-r" => 0.6666666666666666,
      "base05-rgb-g" => 82.0,
      "base0B-dec-g" => 0.5725490196078431,
      "base03-dec-r" => 0.49411764705882355,
      "base05-rgb-r" => 88.0,
      "base0A-dec-g" => 0.43137254901960786,
      "base00-rgb-b" => 244.0,
      "base0F-hex-b" => "bf",
      "base0B-hex-b" => "92",
      "base08-rgb-b" => 120.0,
      "base08-rgb-r" => 190.0,
      "base0C-hex-g" => "8b",
      "base0D-rgb-b" => 219.0,
      "base08-dec-b" => 0.47058823529411764,
      "scheme-slug" => "atelier-cave-light",
      "base05-hex" => "585260",
      "base02-hex-g" => "87",
      "base01-hex-g" => "df",
      "base03-dec-g" => 0.47058823529411764,
      "base0D-hex" => "576ddb",
      "base0F-dec-r" => 0.7490196078431373,
      "base04-dec-r" => 0.396078431372549,
      "base0D-dec-g" => 0.42745098039215684,
      "base00-rgb-r" => 239.0,
      "base0B-dec-b" => 0.5725490196078431,
      "base0D-hex-g" => "6d",
      "base00-dec-g" => 0.9254901960784314,
      "base09-hex-b" => "3c",
      "base03-rgb-r" => 126.0,
      "base0D-dec-r" => 0.3411764705882353,
      "base08-hex" => "be4678",
      "base0D-dec-b" => 0.8588235294117647
    }

    template = """
    # Base16 {{scheme-name}} - alacritty color config
    # {{scheme-author}}
    colors:
      # Default colors
      primary:
        background: '0x{{base00-hex}}'
        foreground: '0x{{base05-hex}}'

      # Colors the cursor will use if `custom_cursor_colors` is true
      cursor:
        text: '0x{{base00-hex}}'
        cursor: '0x{{base05-hex}}'

      # Normal colors
      normal:
        black:   '0x{{base00-hex}}'
        red:     '0x{{base08-hex}}'
        green:   '0x{{base0B-hex}}'
        yellow:  '0x{{base0A-hex}}'
        blue:    '0x{{base0D-hex}}'
        magenta: '0x{{base0E-hex}}'
        cyan:    '0x{{base0C-hex}}'
        white:   '0x{{base05-hex}}'

      # Bright colors
      bright:
        black:   '0x{{base03-hex}}'
        red:     '0x{{base09-hex}}'
        green:   '0x{{base01-hex}}'
        yellow:  '0x{{base02-hex}}'
        blue:    '0x{{base04-hex}}'
        magenta: '0x{{base06-hex}}'
        cyan:    '0x{{base0F-hex}}'
        white:   '0x{{base05-hex}}'

    """
    Mustache.render(template, data)
  end
end

and the output is not interpolated and look like this:

"# Base16 {{scheme-name}} - alacritty color config\n# {{scheme-author}}\ncolors:\n  # Default colors\n  primary:\n    background: '0x{{base00-hex}}'\n    foreground: '0x{{base05-hex}}'\n\n  # Colors the cursor will use if `custom_cursor_colors` is true\n  cursor:\n    text: '0x{{base00-hex}}'\n    cursor: '0x{{base05-hex}}'\n\n  # Normal colors\n  normal:\n    black:   '0x{{base00-hex}}'\n    red:     '0x{{base08-hex}}'\n    green:   '0x{{base0B-hex}}'\n    yellow:  '0x{{base0A-hex}}'\n    blue:    '0x{{base0D-hex}}'\n    magenta: '0x{{base0E-hex}}'\n    cyan:    '0x{{base0C-hex}}'\n    white:   '0x{{base05-hex}}'\n\n  # Bright colors\n  bright:\n    black:   '0x{{base03-hex}}'\n    red:     '0x{{base09-hex}}'\n    green:   '0x{{base01-hex}}'\n    yellow:  '0x{{base02-hex}}'\n    blue:    '0x{{base04-hex}}'\n    magenta: '0x{{base06-hex}}'\n    cyan:    '0x{{base0F-hex}}'\n    white:   '0x{{base05-hex}}'\n\n"

(I'm on version 0.3.1 this time)

Remove deprecated function calls

warning: String.strip/1 is deprecated. Use String.trim/1 instead
Found at 3 locations:
  lib/mustache.ex:23: Mustache.double_mustaches/2
  lib/mustache.ex:26: Mustache.double_mustaches/2
  lib/mustache.ex:104: Mustache.triple_mustaches/2

and

warning: String.strip/1 is deprecated. Use String.trim/1 instead
  test/mustache_integration_test.exs:45: MustacheTest."test Renders HTML document"/1

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.