首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

EEx.Engine

随Elixir一起提供的基本EEx引擎。

引擎需要实现六个功能:

  • init(opts)-在每个文本的开头调用,它必须返回初始状态。
  • handle_body(state)-接收文档的状态,它必须返回引用的表达式。
  • handle_text(state, text)-它接收状态和文本,必须返回一个新的引用表达式。
  • handle_expr(state, marker, expr)-它接收状态、标记、费用,并且必须返回一个新状态。
  • handle_begin(state)-每次需要一个新的状态时调用一个空缓冲区。通常调用do/end块、大小写表达式、匿名函数等。
  • handle_end(state)-相反handle_begin(state)它必须返回引用的表达式。

该标记是紧随其后的<%。例如,<% foo %>有一个空标记,但<%= foo %>"="标记。到目前为止允许的标记是:"""="

handle_expr/3有关此引擎默认实现的标记的更多信息,请参见下面。

EEx.Engine如果您希望使用上述函数的默认实现,则可以直接使用。

类型

state()

函数

handle_assign(arg)

处理引用表达式中的赋值

handle_begin(previous)

返回空字符串作为新缓冲区。

handle_body(quoted)

默认实现只返回给定的表达式。

handle_end(quoted)

End of the new buffer

handle_expr(buffer, binary, expr)

根据标记实现表达式

handle_text(buffer, text)

默认实现只是将文本连接到缓冲区。

init(opts)

返回空字符串作为初始缓冲区。

回调

handle_begin(state)handle_body(state)handle_end(state)handle_expr(state, marker, expr)handle_text(state, text)init(opts)

state()

state() :: term

handle_assign(arg)

handle_assign(Macro.t) :: Macro.t

处理引用表达式中的赋值。

警告将印在遗失的指定上。未来的版本将会升高。

这可以通过调用被添加到任何自定义的发动机handle_assign/1Macro.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)

=

返回空字符串作为新缓冲区。

handle_body(quoted)

默认实现只返回给定的表达式。

handle_end(quoted)

新缓冲区的末尾。

handle_end(quoted)

根据标记实现表达式。

<% Elixir expression - inline with output %>
<%= Elixir expression - replace with result %>

此引擎没有实现所有其他标记。

handle_text(buffer, text)

默认实现只是将文本连接到缓冲区。

init(opts)

返回空字符串作为初始缓冲区。

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

扫码关注腾讯云开发者

领取腾讯云代金券