Proposal: allow to create custom markers in EEx Engines

36 views
Skip to first unread message

Grych

unread,
Jul 1, 2017, 6:29:13 AM7/1/17
to elixir-lang-core
Hi all,
While building my own EEx Engine I encountered an issue, which might be easily resolved, if EEx.Engine allow to use custom markers.
As it stands in the documentation of EEx.Engine behaviour:

The allowed markers so far are: "" and "=".

The EExEngine API is prepared for having custom markers, as handle_expr/3 receives the marker, but currently it receives only "=" or "". Trying to put own marker treats it as a part of the expression.

My proposition is to create a list of additional markers to use by a developer who writes own EEx Engine. For a begin, it could be:
  • "|"
  • "/"
(none of those markers should confuse the "" marker, as they should not appear at the beginning of any Elixr expression)

By default, those markers may behave as "=". But when you create your own EEx Engine, you would be able to override handle_expr/3 for those markers.

-----------
Rough example:

defmodule ExtendedEngine do
  use EEx.Engine

  def handle_expr(buffer, "/", expr) do
    super(buffer, "=", modified_expression(expr))
  end

  def handle_expr(buffer, mark, expr) do
    super(buffer, mark, expr)
  end
end

Current behaviour:

iex> EEx.eval_string "foo <%/ bar %>", [bar: "baz"], engine: ExtendedEngine
** (SyntaxError) nofile:1: syntax error before: '/'
             lib/eex/compiler.ex:36: EEx.Compiler.generate_buffer/4
             lib/eex.ex:196: EEx.eval_string/3
    (stdlib) erl_eval.erl:670: :erl_eval.do_apply/6
       (iex) lib/iex/evaluator.ex:182: IEx.Evaluator.handle_eval/6
       (iex) lib/iex/evaluator.ex:175: IEx.Evaluator.do_eval/4

Expected behaviour:

iex> EEx.eval_string "foo <%/ bar %>", [bar: "baz"], engine: ExtendedEngine
"foo BAZ"


José Valim

unread,
Jul 1, 2017, 6:33:58 AM7/1/17
to elixir-l...@googlegroups.com
Yes, we can support more markers. The default behaviour though should be to raise (and not be equiavlent to "=").

PR please?



José Valim
Skype: jv.ptec
Founder and Director of R&D

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-core+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/41ec2e19-e249-4a16-bcf3-def6bb1bc7b2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Grych

unread,
Jul 1, 2017, 9:32:53 AM7/1/17
to elixir-lang-core, jose....@plataformatec.com.br
Hi José,


W dniu sobota, 1 lipca 2017 12:33:58 UTC+2 użytkownik José Valim napisał:
Yes, we can support more markers. The default behaviour though should be to raise (and not be equiavlent to "=").

Good point, thanks!
 
PR please?
Will do.
 
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages