Web pages
Public pages from www/ templates, with get_context in Elixir, route rules and redirects.
A file www/<route>.html.j2 is served at /<route> on every site that has the app installed. Templates are Jinja, escape HTML, and extend web/base.html.j2 unless they say otherwise.
A page
{% block title %}Hello{% endblock %}
{% block content %}
<h1>Hello {{ n }}</h1>
{% endblock %}This page answers /hello?n=Ada. Query parameters reach the template.
get_context
A module <App>.Www.<Route> adds values to the page. www/crm-form.html.j2 is Crm.Www.CrmForm:
defmodule Crm.Www.CrmForm do
def get_context(ctx, params) do
case Crm.Controllers.WebForm.page(ctx, params["route"]) do
{:ok, context} -> {:ok, Map.put(context, "embed", params["embed"] in ["1", "true", "yes"])}
:not_found -> :not_found
end
end
endget_context(ctx, params) runs in the request's transaction, read-only on GET. params holds the query string and a route rule's variables, with string keys.
| Return | Response |
|---|---|
{:ok, map} | Renders with map merged into the context |
{:redirect, path} | 302 to a path on this site |
{:forbidden, message} | 403 with a link to the login page |
:not_found | 404 |
Define sitemap/0 returning true to list the page in /sitemap.xml.
Route rules and redirects
route_rules in app.exs send a path pattern to a page. CRM serves every published form from one template:
route_rules: [%{from_route: "/crm-form/<route>", to_route: "crm-form"}]redirects take a regex source and an optional status (301 by default, or 302, 307 or 308):
redirects: [%{source: "/old-hello/(.*)", target: "/hello?n=\\1"}]Shared templates
Files in templates/ ending in .j2 can be extended or included by name. A later app's file replaces an earlier app's file of the same name. A RecType with has_web_view: true renders rectypes/<rectype>.web.html.j2 when the app ships one.