Coder Social home page Coder Social logo

phoenix_template's People

Contributors

alaadahmed avatar axelson avatar chrismccord avatar josevalim avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

phoenix_template's Issues

No "*" html template defined module is sometimes misleading

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

RFC - Adding a `:prefix` option to `embedded_templates/2`

Use Case

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

Implementation

If I am understanding how things are currently implemented, I believe this would involve modifying this section of code here:

defmacro embed_templates(pattern, opts \\ []) do
quote bind_quoted: [pattern: pattern, opts: opts] do
Phoenix.Template.compile_all(
&Phoenix.Template.__embed__(&1, opts[:suffix]),
Path.expand(opts[:root] || __DIR__, __DIR__),
pattern
)
end
end
@doc false
def __embed__(path, suffix),
do:
path
|> Path.basename()
|> Path.rootname()
|> Path.rootname()
|> Kernel.<>(suffix || "")

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

Acknowledgements

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.