W3cubDocs

/Elixir 1.6

EEx.Engine behaviour

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:

    • ""
    • "="
    • "/"
    • "|"

    Markers "/" and "|" are only for use in custom EEx engines and are not implemented by default. Using them without the implementation raises EEx.SyntaxError.

    If your engine does not implement all markers, please ensure that handle_expr/3 falls back to EEx.Engine.handle_expr/3 to raise the proper error message.

    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

Types

state()

Functions

handle_assign(arg)

Handles assigns in quoted expressions

handle_begin(previous)

Returns an empty string as the new buffer

handle_body(quoted)

The default implementation simply returns the given expression

handle_end(quoted)

End of the new buffer

handle_expr(buffer, marker, 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_begin(state)
handle_body(state)
handle_end(state)
handle_expr(state, marker, expr)
handle_text(state, text)
init(opts)

Types

state()

state() :: term()

Functions

handle_assign(arg)

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

handle_begin(previous)

Returns an empty string as the new buffer.

handle_body(quoted)

The default implementation simply returns the given expression.

handle_end(quoted)

End of the new buffer.

handle_expr(buffer, marker, expr)

Implements expressions according to the markers.

<% Elixir expression - inline with output %>
<%= Elixir expression - replace with result %>
<%/ Elixir expression - raise EEx.SyntaxError, to be implemented by custom engines %>
<%| Elixir expression - raise EEx.SyntaxError, to be implemented by custom engines %>

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_begin(state)

handle_begin(state()) :: state()

handle_body(state)

handle_body(state()) :: Macro.t()

handle_end(state)

handle_end(state()) :: Macro.t()

handle_expr(state, marker, expr)

handle_expr(state(), marker :: String.t(), expr :: Macro.t()) :: state()

handle_text(state, text)

handle_text(state(), text :: String.t()) :: state()

init(opts)

init(opts :: keyword()) :: state()

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