On 02/03/2016 04:45 AM,
pj.beta...@gmail.com wrote:
> for (i = 0; i < <%= @sliclGal_iter %>; i++) {
> <img class="slickGal" src="<%= @slickGal_imgPath[i] %>">
> }
> On 02/03/2016 06:53 AM,
pj.beta...@gmail.com wrote:
> Thank you Steve but I'm not (unless that's the only way of doing this
> with Elixir/Phoenix).
The only way? Certainly not, but it is the most idiomatic way. The code
in your example shows that you are trying to force a square peg into a
round hole here.
In your example, you are using @slicGal_imgPath with numerical indices,
so you have something you can iterate on here. You should normally be
transforming this collection into something you can iterate on and
remove logic from the template.
While there are likely easier ways to do this depending on what the
underlying structure of @sliclGal_iter (looks like a typo there?) is,
knowing absolutely nothing, here is a general solution. You can put this
function in the view.
defp image_paths(path_collection, element_count) do
for n <- 0..(element_count - 1), do: path_collection[n]
end
then you can add the following in your template:
<%= for path <- image_paths(@slickGal_imgPath, @sliclGal_iter) do %>
<%= tag(:a, class: "slickGal", src: path %>
<% end %>
There are certainly optimizations to be made here depending on all sorts
of details from your use case.
Gilbert