W3cubDocs

/Elixir 1.9

EEx.SmartEngine

The default engine used by EEx.

It includes assigns (like @foo) and possibly other conveniences in the future.

Examples

iex> EEx.eval_string("<%= @foo %>", assigns: [foo: 1])
"1"

In the example above, we can access the value foo under the binding assigns using @foo. This is useful because a template, after being compiled, can receive different assigns and would not require recompilation for each variable set.

Assigns can also be used when compiled to a function:

# sample.eex
<%= @a + @b %>

# sample.ex
defmodule Sample do
  require EEx
  EEx.function_from_file(:def, :sample, "sample.eex", [:assigns])
end

# iex
Sample.sample(a: 1, b: 2)
#=> "3"

Summary

Functions

handle_begin(state)

Invoked at the beginning of every nesting.

handle_body(state)

Called at the end of every template.

handle_end(state)

Invokes at the end of a nesting.

handle_expr(state, marker, expr)

Called for the dynamic/code parts of a template.

handle_text(state, text)

Called for the text/static parts of a template.

init(opts)

Called at the beginning of every template.

Functions

handle_begin(state)

Invoked at the beginning of every nesting.

It must return a new state that is used only inside the nesting. Once the nesting terminates, the current state is resumed.

Callback implementation for EEx.Engine.handle_begin/1.

handle_body(state)

Called at the end of every template.

It must return Elixir's quoted expressions for the template.

Callback implementation for EEx.Engine.handle_body/1.

handle_end(state)

Invokes at the end of a nesting.

It must return Elixir's quoted expressions for the nesting.

Callback implementation for EEx.Engine.handle_end/1.

handle_expr(state, marker, expr)

Called for the dynamic/code parts of a template.

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 an appropriate implementation raises EEx.SyntaxError.

It must return the updated state.

Callback implementation for EEx.Engine.handle_expr/3.

handle_text(state, text)

Called for the text/static parts of a template.

It must return the updated state.

Callback implementation for EEx.Engine.handle_text/2.

init(opts)

Called at the beginning of every template.

It must return the initial state.

Callback implementation for EEx.Engine.init/1.

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