GithubHelp home page GithubHelp logo

stevencorona / liquex Goto Github PK

View Code? Open in Web Editor NEW

This project forked from handlecommerce/liquex

0.0 0.0 0.0 173 KB

Liquid template processor for Elixir

License: MIT License

Ruby 0.11% Elixir 99.89%

liquex's Introduction

Liquex

A Liquid template parser for Elixir.

Liquid template renderer for Elixir with 100% compatibility with the Liquid gem by Shopify. If you find that this library is not byte for byte equivalent to liquid, please open an issue.

Installation

The package is available in Hex and can be installed by adding liquex to your list of dependencies in mix.exs:

def deps do
  [
    {:liquex, "~> 0.7"}
  ]
end

Documentation can be found at https://hexdocs.pm/liquex.

Basic Usage

iex> {:ok, template_ast} = Liquex.parse("Hello {{ name }}!")
iex> {content, _context} = Liquex.render(template_ast, %{"name" => "World"})

iex> content |> to_string()
"Hello World!"

Supported features

Currently, all standard Liquid tags, filters, and types are fully supported. Liquex can be considered a drop in replacement of the Liquid gem, but in Elixir.

Supported:

  • All standard tags and filters
  • Custom tags and filters
  • Lazy variable resolvers
  • Date processing parity with Ruby
  • Access maps with atom or string keys
  • Whitespace control

Lazy variables

Liquex allows resolver functions for variables that may require some extra work to generate. For example, Shopify has variables for things like available products. Pulling all products every time would be too expensive to do on every render. Instead, it would be better to lazily pull that information as needed.

Instead of adding the product list to the context variable map, you can add a function to the variable map. If a function is accessed in the variable map, it is executed.

products_resolver = fn _parent -> Product.all() end

with {:ok, document} <- Liquex.parse("There are {{ products.size }} products"),
    {result, _} <- Liquex.render(document, %{products: products_resolver}) do
  result
end

"There are 5 products"

Indifferent access

By default, Liquex accesses your maps and structs that may have atom or string (or other type) keys. Liquex will try a string key first. If that fails, it will fall back to using an atom keys. This is similar to how Ruby on Rails handles many of its hashes.

This allows you to pass in your structs without having to replace all your keys with string keys.

iex> {:ok, template_ast} = Liquex.parse("Hello {{ name }}!")
iex> {content, _context} = Liquex.render(template_ast, %{name: "World"})
iex> content |> to_string()
"Hello World!"

Custom filters

Liquex contains the full suite of standard Liquid filters, but you may find that there are still filters that you may want to add.

Liquex supports adding your own custom filters to the render pipeline. When creating the context for the renderer, set the filter module to your own module.

defmodule CustomFilter do
  # Import all the standard liquid filters
  use Liquex.Filter

  def scream(value, _), do: String.upcase(value) <> "!"
end

context = Liquex.Context.new(%{}, filter_module: CustomFilter)
{:ok, template_ast} = Liquex.parse("{{'Hello World' | scream}}"

{result, _} = Liquex.render(template_ast, context)
result |> to_string()

iex> "HELLO WORLD!"

Custom tags

One of the strong points for Liquex is that the tag parser can be extended to support non-standard tags. For example, Liquid used internally for the Shopify site includes a large range of tags that are not supported by the base Ruby gem. These tags could also be added to Liquex by extending the liquid parser.

defmodule CustomTag do
  @moduledoc false

  @behaviour Liquex.Tag

  import NimbleParsec

  @impl true
  # Parse <<Custom Tag>>
  def parse() do
    text =
      lookahead_not(string(">>"))
      |> utf8_char([])
      |> times(min: 1)
      |> reduce({Kernel, :to_string, []})
      |> tag(:text)

    ignore(string("<<"))
    |> optional(text)
    |> ignore(string(">>"))
  end

  @impl true
  def render(contents, context) do
    {result, context} = Liquex.render(contents, context)
    {["Custom Tag: ", result], context}
  end
end

defmodule CustomParser do
  use Liquex.Parser, tags: [CustomTag]
end

iex> document = Liquex.parse!("<<Hello World!>>", CustomParser)
iex> {result, _} = Liquex.render(document, context)
iex> result |> to_string()
"Custom Tag: Hello World!"

liquex's People

Contributors

markglenn avatar ouven avatar cipater avatar resterle 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.