EEx v1.5.1 EEx.Engine behaviour View Source

Basic EEx engine that ships with Elixir.

An engine needs to implement six functions:

  • init(opts) - called at the beginning of every text and it must return the initial state.

  • handle_body(state) - receives the state of the document and it must return a quoted expression.

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

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

  • handle_begin(state) - called every time there a new state is needed with an empty buffer. Typically called for do/end blocks, case expressions, anonymous functions, etc

  • handle_end(state) - opposite of handle_begin(state) and it must return 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.

Link to this section Summary

Functions

Handles assigns in quoted expressions

Returns an empty string as the new buffer

The default implementation simply returns the given expression

End of the new buffer

Implements expressions according to the markers

The default implementation simply concatenates text to the buffer

Returns an empty string as initial buffer

Link to this section Types

Link to this section Functions

Link to this function handle_assign(arg) View Source
handle_assign(Macro.t()) :: Macro.t()

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

Returns an empty string as the new buffer.

The default implementation simply returns the given expression.

End of the new buffer.

Link to this function handle_expr(buffer, binary, expr) View Source

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.

Link to this function handle_text(buffer, text) View Source

The default implementation simply concatenates text to the buffer.

Returns an empty string as initial buffer.

Link to this section Callbacks

Link to this callback handle_begin(state) View Source
handle_begin(state()) :: state()
Link to this callback handle_body(state) View Source
handle_body(state()) :: Macro.t()
Link to this callback handle_end(state) View Source
handle_end(state()) :: Macro.t()
Link to this callback handle_expr(state, marker, expr) View Source
handle_expr(state(), marker :: String.t(), expr :: Macro.t()) :: state()
Link to this callback handle_text(state, text) View Source
handle_text(state(), text :: String.t()) :: state()
Link to this callback init(opts) View Source
init(opts :: keyword()) :: state()