phoenixframework / phoenix_template Goto Github PK
View Code? Open in Web Editor NEWTemplate rendering for Phoenix
License: MIT License
Template rendering for Phoenix
License: MIT License
When writing a new controller and "html module" (is there an official name for the module that calls use HelloPhxWeb, :html
?) it's possible either forget to define the html module completely or mistype the name, possibly because of not realizing that capitalization is important.
This is something that just happened to me as I was porting one of my simple phoenix apps to 1.7 and the new HTML structures. In my case my html module was named PrsHTML
when it should have been PRsHTML
.
I've also seen it happen to others such as this forum post where the user's module name was MyAppWeb.UserSettingsHtml
instead of MyAppWeb.UserSettingsHTML
https://elixirforum.com/t/random-no-html-template-defined-for-myappweb-html-errors-after-upgrading-to-phoenix-1-7/54814
The current error message looks like: no "game" html template defined for HelloPhxWeb.GameHTML
I think this error handling can be improved by verifying that the module contained in the error message is a real existing module.
My proposed error message in the case that the module is not defined would look something like: Looked for a HelloPhxWeb.GameHTML HTML module, but none was defined
.
Ideally we'd link to a relevant page in the docs to help user's understand what an html module is, the inflection rules, and an example. But I'm not sure if there is a page like that in the docs currently.
Also ideally we could use String.jaro_distance/2
to let the user know if they possibly made a typo when defining their module (especially if it's just a casing issue!).
Here's an example repo that showcases the error: https://github.com/axelson/phoenix_template_error_example
Start the phoenix server and navigate to http://localhost:4000/game to see the error
When building up a tree of templates for rendering templates (like emails in the example below), it would be nice to use a :prefix
option to generate function names in nested directories without having to leave component module files in said nested directories.
defmodule EmailHTML do
use Phoenix.Component
embed_templates "templates/email/admin/*.html", prefix: "admin_", suffix: "_html"
embed_templates "templates/email/user/*.html", prefix: "user_", suffix: "_html"
end
If I am understanding how things are currently implemented, I believe this would involve modifying this section of code here:
phoenix_template/lib/phoenix/template.ex
Lines 102 to 119 in d6098a7
to look something like this:
defmacro embed_templates(pattern, opts \\ []) do
quote bind_quoted: [pattern: pattern, opts: opts] do
Phoenix.Template.compile_all(
&Phoenix.Template.__embed__(&1, opts[:prefix], opts[:suffix]),
Path.expand(opts[:root] || __DIR__, __DIR__),
pattern
)
end
end
@doc false
def __embed__(path, prefix, suffix) do
rootname = path
|> Path.basename()
|> Path.rootname()
|> Path.rootname()
(prefix || "")
|> Kernel.<>(rootname)
|> Kernel.<>(suffix || "")
end
Admittedly, I am relatively new to Phoenix/Elixir rendered views. If I am approaching this problem incorrectly, or am unaware of some far-reaching implications this change may have, I would gladly welcome correction to my approach.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.