遍历模板中具有两个参数的函数时出错 [英] Error when iterating over a function with two arguments in my template
本文介绍了遍历模板中具有两个参数的函数时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
视图中有一个函数,可用于在模板中动态构建HTML元素:
I have a function in my view that I use to dynamically build HTML elements in my templates:
defmodule Recursion do
def buildElements(element,n) when n <= 1 do
element
end
def buildElements(element, n) do
[element | buildElements(element, n - 1)]
end
end
除非我尝试在模板中对其进行迭代,否则它将非常有效:
It is working great unless I try to iterate it in my template:
<%= for element <- MyProject.PageView.Recursion.buildElements("LOL", 2) do %>
<%= element %>
<% end %>
我收到此错误:
no function clause matching in Enum."-reduce/3-lists^foldl/2-0-"/3
出什么问题了?
推荐答案
这只是解决您问题的一个小改动.
It's only a small change to solve your problem.
您尝试使用[foo | bar]
表示法将元素添加到列表中. bar
必须是列表,而foo
是单个项目.
You try to add a element to a list with the [foo | bar]
notation. bar
must be a list while foo
is a single item.
将您的element
更改为[element]
即可.
def buildElements(element,n) when n <= 1 do
[element]
end
def buildElements(element, n) do
[element | buildElements(element, n - 1)]
end
这篇关于遍历模板中具有两个参数的函数时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文