EEx.Engine behaviour

Basic EEx engine that ships with Elixir.

An engine needs to implement three functions:

  • init(opts) - returns the initial buffer

  • handle_body(quoted) - receives the final built quoted expression, should do final post-processing and return a quoted expression.

  • handle_text(buffer, text) - it receives the buffer, the text and must return a new quoted expression.

  • handle_expr(buffer, marker, expr) - it receives the buffer, the marker, the expr and must return a new quoted expression.

    The marker is what follows exactly after <%. For example, <% foo %> has an empty marker, but <%= foo %> has "=" as marker. The allowed markers so far are: "" and "=".

    Read handle_expr/3 below for more information about the markers implemented by default by this engine.

EEx.Engine can be used directly if one desires to use the default implementations for the functions above.

Summary

Functions

handle_assign(arg)

Handles assigns in quoted expressions

handle_body(quoted)

The default implementation simply returns the given expression

handle_expr(buffer, binary, expr)

Implements expressions according to the markers

handle_text(buffer, text)

The default implementation simply concatenates text to the buffer

init(opts)

Returns an empty string as initial buffer

Callbacks

handle_body(arg0)
handle_expr(arg0, arg1, arg2)
handle_text(arg0, arg1)
init(arg0)

Functions

handle_assign(arg)

Handles assigns in quoted expressions.

A warning will be printed on missing assigns. Future versions will raise.

This can be added to any custom engine by invoking handle_assign/1 with Macro.prewalk/2:

def handle_expr(buffer, token, expr) do
  expr = Macro.prewalk(expr, &EEx.Engine.handle_assign/1)
  EEx.Engine.handle_expr(buffer, token, expr)
end

handle_body(quoted)

The default implementation simply returns the given expression.

handle_expr(buffer, binary, expr)

handle_expr(Macro.t, String.t, Macro.t) :: Macro.t

Implements expressions according to the markers.

<% Elixir expression - inline with output %>
<%= Elixir expression - replace with result %>

All other markers are not implemented by this engine.

handle_text(buffer, text)

The default implementation simply concatenates text to the buffer.

init(opts)

Returns an empty string as initial buffer.

Callbacks

handle_body(arg0)

handle_body(Macro.t) :: Macro.t

handle_expr(arg0, arg1, arg2)

handle_expr(Macro.t, String.t, Macro.t) :: Macro.t

handle_text(arg0, arg1)

handle_text(Macro.t, String.t) :: Macro.t

init(arg0)

init(Keyword.t) :: Macro.t

© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.
https://hexdocs.pm/eex/1.3.4/EEx.Engine.html