3
votes

Rather than customise the templates supplied with the Phoenix Framework, I would like to create a set of my own and have those callable as a custom mix task.

I can create a custom mix task, but cannot figure out:

  1. Where within the phoenix framework directory structure to put the directory of custom templates

  2. How to modify the module Mix.Tasks.???.Gen.Html so that the Mix.Phoenix.copy_from paths(), ... can find the directory where the custom templates are stored. (Where ??? is the string for the name of my customisation. That is a copy of the Mix.Tasks.Phoenix.Gen.Html module that I have renamed and can successfully run with the unmodified code successfully finding the supplied templates)

There was a thread on the Phoenix Github Issues that spoke to this topic somewhat but did not help with the directory paths to use.

Any suggestions or pointers to projects that have implemented their own custom templates would be much appreciated.

1
Graham you can please expand on your question. What is it exactly that you're trying to accomplish?Donovan Dikaio
The templates that are supplied with the standard Phoenix install do not suit the layout that I require - so I would like to create a set of templates that can be used instead of the ones that mix phoenix.gen.html uses. This would allow me to create an app that looked and functioned/navigated how I want it to rather than having to go in and edit the supplied templates.Graham Hay

1 Answers

7
votes

The location for the templates is currently hard coded in the phoenix source, so you will not be able to specify an arbitrary location. It will however first search in the current directory, then inside the phoenix source code. The only thing you have to do is to create the priv/templates/phoenix.gen.html/ directory inside your app. You can then override the templates there.

# create dummy app
$ mix phoenix.new my_app
$ cd my_app
$ mix deps.get
$ mix deps.compile

# override the show template
$ mkdir -p priv/templates/phoenix.gen.html/
$ echo 'TEST <%= 123 %>' > priv/templates/phoenix.gen.html/show.html.eex

# run the generator
$ mix phoenix.gen.html User users name:string

# the show template is now overridden
$ cat web/templates/user/show.html.eex
TEST 123

# other templates are left untouched
$ cat web/templates/user/index.html.eex
<h2>Listing users</h2>
...