gpt4 book ai didi

templates - 向 EEx 模板公开函数

转载 作者:行者123 更新时间:2023-12-03 08:21:43 24 4
gpt4 key购买 nike

我正在使用 EEx.eval_fileEEx.eval_string因为我在编译期间无法访问模板,并且想要公开一些可以从模板内部调用的函数。

我是什么可以所以它使用绑定(bind)( test: &test/1 )传递引用,但是我必须这样称呼它们,从用户的角度来看这不是很好:<%= test.("Hello") %>
我还有其他方法可以尝试吗?

最佳答案

它没有记录在 AFAICS 中,而是 EEx.eval_string内部调用 Code.eval_quoted/3第三个参数( options )按原样传递给该函数。 Code.eval_quoted/3接受 functions options 中的关键字参数其中包含要导入的功能:

:functions - a list of tuples where the first element is a module and the second a list of imported function names and arity; the list of function names and arity must be sorted



Source

例子:
iex(1)> EEx.eval_string ~s|<%= length("foo") %>|, [], functions: [{String, [length: 1]}]
"3"

关于templates - 向 EEx 模板公开函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48562489/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com