Including html.eex file onto another html.eex file

How do I include/require a html.eex file onto another html.eex file in phoenix just like we do in php?

You don’t.

Though as you mention #phoenix as a tag, you can use render/* to render another template from the visible search path into the current position. Be aware that you need to pass necessary assigns manually/explicitely.

1 Like

how can i use rendering to include a file

You do not include files, thats now how it is supposed to work. You render an external template and embedd the result into your current template.

<%= render "other_template.html", data %> or
<%= render OtherView, "other_template.html", data %>

This is conceptionally quite different to how php’s include/require work though. The concept of including/requiring files is not present in elixir, because on the beam compiled modules are in a known location and there’s no code not in modules at runtime.


Thank you!

I just want to add that you can also directly call another view inside your template like this:

<%= OtherView.render "other_template.html", @assigns %>

It is just a small detail but I find this more explicit and clear when rendering with a different view inside another.

1 Like