在我的基础布局中,我有这个:
<%= render_existing(@view_module, "sub_template1." <> @view_template, assigns) %>
我只想在某些页面上呈现。为此目的我有“shared / sub_template1.html.eex”子模板,我想在“articles / index”中呈现它。因此我有这个:
defmodule MyApp.ArticleView do
#....
def render("sub_template1.index.html", _assigns) do
# var1 = "something"
# var2 = "something2"
# render("shared/sub_template1.html", var1: "var1", var2: "var2") #doesn't work! exception!
# this works -- rendering html manually
# but I don't want this
raw """
<div class="class1">
<div class="class2">
#{var1} #{var2}
<!-- ............... -->
"""
我想从“shared / sub_template1.html”渲染 - 读取和返回 - html,而不是从MyApp.ArticleView / render手动返回html。我怎么能这样做?
答案 0 :(得分:0)
只需传递/
的路径,就无法从其他文件夹渲染模板。为了呈现web/templates/shared/sub_template1.html.eex
(注意:如果文件以.html
结尾,您还必须将其更改为.html.eex
),您必须创建名为{{的视图1}}:
MyApp.SharedView
然后,在# web/views/shared_view.ex
defmodule MyApp.SharedView do
use MyApp.Web, :view
end
中添加以下内容:
ArticleView