Add custom templates feature

This commit is contained in:
Mikko Ahlroth 2015-09-13 22:34:13 +03:00
parent 450dacb92e
commit 0a684ae6d4
6 changed files with 44 additions and 4 deletions

View file

@ -22,6 +22,7 @@ The engine consists of two parts, both in the `apps/` directory:
* Automatic yearly, monthly and tag archives
* Disqus commenting
* RSS feeds for all posts and for tags
* Override templates by putting replacements into a directory
## Possible future features
@ -29,4 +30,4 @@ The engine consists of two parts, both in the `apps/` directory:
* User's guide
* Script for running and refreshing blog from the command line
* Sitemap
* Override templates and styles easily by putting replacements into a directory
* Override styles

View file

@ -17,3 +17,6 @@ erl_crash.dump
# we ignore priv/static/{css,js}. You may want to
# comment this depending on your deployment strategy.
/priv/static/
# Custom templates should be ignored
/web/templates/custom/

View file

@ -41,7 +41,18 @@ config :blog_config,
ga('send', 'pageview');
</script>
"""
""",
# Custom templates
# Set to true for any view you want to use custom templates for.
# If using custom templates for a view, create a folder web/templates/custom/<viewname>
# and make a copy of all templates for the view inside the folder.
custom_templates: %{
"ErrorView" => false,
"FeedView" => false,
"PageView" => false,
"LayoutView" => false # Base layouts
}
# Configures Elixir's Logger
config :logger, :console,

View file

@ -0,0 +1,7 @@
This directory is for your custom templates. Should you wish to use a custom template
for a view, copy the corresponding view's directory from the templates directory
and modify the files as you see fit.
Note that if a view uses custom templates, each template must have a custom version.
This means you need to copy over every template file. Naturally you can just leave
the templates you do not want to modify untouched.

View file

@ -1,6 +1,6 @@
defmodule MebeWeb.ErrorView do
use MebeWeb.Web, :view
@absolute_url Application.get_env(:blog_config, :absolute_url)
def render("404.html", %{conn: conn}) do

View file

@ -15,6 +15,7 @@ defmodule MebeWeb.Web do
Do NOT define functions inside the quoted expressions
below.
"""
def controller do
quote do
use Phoenix.Controller
@ -26,7 +27,17 @@ defmodule MebeWeb.Web do
def view do
quote do
use Phoenix.View, root: "web/templates"
root = "web/templates"
# Check if we should be using a custom template
custom_templates = Application.get_env :blog_config, :custom_templates
compiled_view = MebeWeb.Web.module_to_str __MODULE__
if Map.get custom_templates, compiled_view do
root = root <> "/custom"
end
use Phoenix.View, root: root
# Import convenience functions from controllers
import Phoenix.Controller, only: [get_flash: 2]
@ -45,4 +56,11 @@ defmodule MebeWeb.Web do
defmacro __using__(which) when is_atom(which) do
apply(__MODULE__, which, [])
end
def module_to_str(module) do
module
|> Atom.to_string
|> String.split(".")
|> List.last
end
end