PJextra

PJextra

With Phoenix 1.2 we get the ability to do this but I’m trying it without success. I posted a question here:
http://stackoverflow.com/questions/39043777/how-to-render-a-template-inside-a-web-templates-folder-subfolder

I think this is a very good improvement for folder/components organization so I post this question here too.

Showing Posts 1 to 10

OvermindDL1

OvermindDL1

Easiest way, just make a new view that is module-spaced to the directory you want the files to be. :slight_smile:

I do precisely that with a lot of my re-usable HTML components so I can easily embed them around.

PJextra

PJextra OP

What do you mean by module-spaced?
I was trying to use the new :path and :pattern options to allow wildcard template inclusion as well as customized template directory locations, but your suggestion seems to solve that without the need of these.
Thanks!

OvermindDL1

OvermindDL1

The ‘name’ of a View module defines the directory path it loads. :slight_smile:

For example I have a helper view so I can embed other eex parts into larger areas:

defmodule MyServer.ComponentsView do
  # Stripped code that does not matter
  use MyServer.Web, :view
end

This defines a view that will load eex from my web/templates/components directory.

You can customize how it loads further by instead of 'use’ing your Web’s :view you can just use Phoenix.View, root: "web/templates", namespace: MyServer and change whatever you want. Or you can go even lower level and call the EEX generator to generate functions in your module. I just make new View’s with appropriate names. :slight_smile:

ibgib

ibgib

Could you point to a repo online that uses this technique? I also had a similar issue, but I just share a single SharedView without much customization at this point. So I have multiple a.html.eex, b.html.eex, etc. templates with a single view.

chrismccord

chrismccord

Creator of Phoenix

See the pattern option to Phoenix.View, i.e. in your web.ex you can set:

use Phoenix.View, root: "web/templates", pattern: "**/*"

Which would then precompile all templates within the view’s folder as before, as well as all nested directories within the template folder.

29
Post #5
PJextra

PJextra OP

I tried both approaches (OvermindDL1 & ChrisMcCord) and they both work.
Nevertheless, using “pattern” (see Chris answer) avoids having to create new views for the subfolders, and, as such, makes our “web/views” folder much cleaner and simple.
Thank you both.

smithaitufe

smithaitufe

I have made the change to the view part of web.ex as suggested by mccordchris.

I have a folder that I created under lib/web/templates/layout. The name of the folder is member. In it, I have a layout file that I called member.html.eex.

Then in my controller function, I wrote this

render conn, "faqs.html", layout: {Donations.Web.LayoutView, "member/member.html"}

I am having this error

Could not render "member/member.html" for Donations.Web.LayoutView, please define a matching clause for render/2 or define a template at "lib/donations/web/templates/web/layout". No templates were compiled for this module. Assigns:

Thanks for your help. I am using phoenix 1.3

OvermindDL1

OvermindDL1

Those do not match? ^.^

chrismccord

chrismccord

Creator of Phoenix

Also, note the glob pattern needs to be pattern: "**/*

zazaian

zazaian

Hey guys - thanks to both @chrismccord and @OvermindDL1 for the thorough overview here. I have a question about this pattern, though. Are there any drawbacks to using the root use Phoenix.View approach here in lieu of the use MyApp.Web , :view approach. Is there a way to do this with MyApp.Web or are these mutually exclusive?

Thanks in advance for any comments here.

Mike Zazaian

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
Onor.io
I have what I’ve heard referred to as a “lookup table” in my database. This is a way of assigning codes to common values. One common lo...
New
Trolleger
What approach to take when sending live updates to “random” users Hi! I have a question, I have a little chat app, and when I create a DM...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews