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:
"|" are only for use in custom EEx engines and are not implemented by default. Using them without the implementation raises
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.
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
state() :: term()
Handles assigns in quoted expressions.
A warning will be printed on missing assigns. Future versions will raise.
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.
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.
The default implementation simply concatenates text to the buffer.
Returns an empty string as initial buffer.
init(opts :: keyword()) :: state()
© 2012 Plataformatec
Licensed under the Apache License, Version 2.0.