Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Phoenix: render template that has assigns values inside a template

I'm trying to make my app DRY and modular. But when I tryed to put a component (small template) that receives dynamic values passed when calling/"instanciating" it inside another template (larger module) I got this error:

assign @conn not available in eex template. Available assigns: []

My component (small template) that I inserted inside my module (larger template) is this:

<div class="menuButton main <%= @class %>" id="<%= @id %>">
    <div class="menuButton firstChild linesItem"></div>
    <div class="menuButton firstChild textItem">MENU</div>
</div>

I inserted it in my module using:

<%= render myapp.ComponentView, "menuButton.html", class: nil, id: "menuButtonMenu" %>

and I inserted my module in my page using:

<%= render myapp.ModuleView, "header.html" %>

WHat's the best way of making this work while still keeping this logic of small components/larger modules clean and DRY?

like image 421
Paulo Janeiro Avatar asked Nov 27 '15 18:11

Paulo Janeiro


1 Answers

As AbM said, you need to explicitly pass the assigns that you care about, for example:

<%= render myapp.ModuleView, "header.html", conn: @conn %>

If you want to support assigns conditionally, you can reference them off the assigns like this:

<%= link "a link", to: "/", class: assigns[:class] || "default" %>
like image 196
Chris McCord Avatar answered Oct 16 '22 16:10

Chris McCord