Skip to content

Repository files navigation

Solid

Build StatusModule VersionHex DocsTotal DownloadLicenseLast Updated

Solid is an implementation in Elixir of the template language Liquid. It uses nimble_parsec to generate the parser.

Basic Usage

iex>template="My name is {{ user.name }}"iex>{:ok,template}=Solid.parse(template)iex>Solid.render(template,%{"user"=>%{"name"=>"José"}})|>to_string"My name is José"

Installation

The package can be installed with:

defdepsdo[{:solid,"~> 0.12"}]end

Custom tags

To implement a new tag you need to create a new module that implements the Tag behaviour:

defmoduleMyCustomTagdoimportNimbleParsec@behaviourSolid.Tag@impltruedefspec(_parser)dospace=Solid.Parser.Literal.whitespace(min: 0)ignore(string("{%"))|>ignore(space)|>ignore(string("my_tag"))|>ignore(space)|>ignore(string("%}"))end@impltruedefrender(tag,_context,_options)do[text: "my first tag"]endend
  • spec defines how to parse your tag;
  • render defines how to render your tag.

Now we need to add the tag to the parser

defmoduleMyParserdouseSolid.Parser.Base,custom_tags: [MyCustomTag]end

And finally pass the custom parser as an option:

"{% my_tag %}"|>Solid.parse!(parser: MyParser)|>Solid.render()

Custom filters

While calling Solid.render one can pass a module with custom filters:

defmoduleMyCustomFiltersdodefadd_one(x),do: x+1end"{{ number | add_one }}"|>Solid.parse!()|>Solid.render(%{"number"=>41},custom_filters: MyCustomFilters)|>IO.puts()# 42

Extra options can be passed as last argument to custom filters if an extra argument is accepted:

defmoduleMyCustomFiltersdodefasset_url(path,opts)doopts[:host]<>pathendendopts=[custom_filters: MyCustomFilters,host: "http://example.com"]"{{ file_path | asset_url }}"|>Solid.parse!()|>Solid.render(%{"file_path"=>"/styles/app.css"},opts)|>IO.puts()# http://example.com/styles/app.css

Contributing

When adding new functionality or fixing bugs consider adding a new test case here inside test/cases. These cases are tested against the Ruby gem so we can try to stay as close as possible to the original implementation.

TODO

  • Integration tests using Liquid gem to build fixtures; #3
  • All the standard filters #8
  • Support to custom filters #11
  • Tags (if, case, unless, etc)
    • for
      • else
      • break
      • continue
      • limit
      • offset
      • Range (3..5)
      • reversed
      • forloop object
    • raw#18
    • cycle#17
    • capture#19
    • increment#16
    • decrement#16
  • Boolean operators #2
  • Whitespace control #10

Copyright and License

Copyright (c) 2016 Eduardo Gurgel Pinho

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.

About

Liquid template engine in Elixir

Resources

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages