az (arizona v0.4.0)

View Source

Short alias module for arizona_template and arizona_stateful.

Provides the same runtime API surface as arizona_template under a shorter prefix so handcrafted templates that don't use the macros can be written as az:get(...), az:html(...), etc. It also re-exports the common types so users writing handler specs can reference them through a single module: az:bindings(), az:mount_ret(), etc.

The parse transform recognizes both arizona_template:html/1 and az:html/1 (same for each/2), so either alias compiles to the same optimized template map.

Parse transform stubs

html/1, native/1, terminal/1 and each/2 are stubs -- they exist so callers can write az:html({...}) and the parse transform can detect and replace the call. They never actually execute at runtime; calling them directly errors with parse_transform_not_applied.

Layout helper

inner_content/1 is the only function that does real runtime work not delegated to arizona_template. It extracts the inner_content binding, used by stateless layouts to render the wrapped page. The value is an opaque nested template for a content slot -- see the function's own docs.

Summary

Functions

Parse transform stub. The parse transform replaces az:each(Fun, Source) with compiled iteration code; this runtime body is only reached if the parse transform did not run, in which case it errors with parse_transform_not_applied like the other stubs.

Parse transform stub. Calling this at runtime errors -- the parse transform replaces az:html(...) (or arizona_template:html(...)) calls with compiled template maps before code generation.

Returns the inner_content binding from a layout's bindings map -- the rendered page this layout wraps. Used by stateless layout modules, normally through the ?inner_content macro, to place the wrapped page content.

Parse transform stub. The parse transform replaces az:native(...) (or arizona_template:native(...)) calls with compiled JSON template maps before code generation.

Parse transform stub. The parse transform replaces az:terminal(...) (or arizona_template:terminal(...)) calls with compiled ANSI template maps before code generation.

Types

bindings()

-type bindings() :: arizona_template:bindings().

effect()

-type effect() :: arizona_stateful:effect().

effects()

-type effects() :: arizona_stateful:effects().

error_info()

-type error_info() :: arizona_http:error_info().

event_name()

-type event_name() :: arizona_stateful:event_name().

event_payload()

-type event_payload() :: arizona_stateful:event_payload().

handle_drain_ret()

-type handle_drain_ret() :: arizona_stateful:handle_drain_ret().

handle_event_ret()

-type handle_event_ret() :: arizona_stateful:handle_event_ret().

handle_info_ret()

-type handle_info_ret() :: arizona_stateful:handle_info_ret().

handle_update_ret()

-type handle_update_ret() :: arizona_stateful:handle_update_ret().

mount_ret()

-type mount_ret() :: arizona_stateful:mount_ret().

render_fun()

-type render_fun() :: arizona_template:render_fun().

render_ret()

-type render_ret() :: arizona_stateful:render_ret().

request()

-type request() :: arizona_req:request().

resets()

-type resets() :: arizona_stateful:resets().

template()

-type template() :: arizona_template:template().

unmount_ret()

-type unmount_ret() :: arizona_stateful:unmount_ret().

Functions

each(Fun, Source)

-spec each(Fun, Source) -> no_return() when Fun :: fun(), Source :: term().

Parse transform stub. The parse transform replaces az:each(Fun, Source) with compiled iteration code; this runtime body is only reached if the parse transform did not run, in which case it errors with parse_transform_not_applied like the other stubs.

get(Key, Bindings)

-spec get(Key, Bindings) -> term() when Key :: term(), Bindings :: map().

Alias for arizona_template:get/2.

get(Key, Bindings, Default)

-spec get(Key, Bindings, Default) -> term() when Key :: term(), Bindings :: map(), Default :: term().

Alias for arizona_template:get/3.

get_lazy(Key, Bindings, DefaultFun)

-spec get_lazy(Key, Bindings, DefaultFun) -> term()
                  when Key :: term(), Bindings :: map(), DefaultFun :: fun(() -> term()).

Alias for arizona_template:get_lazy/3.

html(Elems)

-spec html(term()) -> no_return().

Parse transform stub. Calling this at runtime errors -- the parse transform replaces az:html(...) (or arizona_template:html(...)) calls with compiled template maps before code generation.

inner_content(Bindings)

-spec inner_content(Bindings) -> term() when Bindings :: map().

Returns the inner_content binding from a layout's bindings map -- the rendered page this layout wraps. Used by stateless layout modules, normally through the ?inner_content macro, to place the wrapped page content.

Opaque, and it goes in a content slot whole. The value is a nested template (#{s := [Page], d := []}) -- not iodata, not a binary. That is what lets a content slot splice the page verbatim instead of copying and UTF-8-re-decoding it once per layout layer.

Placements that work, whether the slot is the layout's own or one it hands the page on to (the page renders as itself, unescaped, in all of them):

?html([~"<main>", ?inner_content, ~"</main>"])                      %% the slot
?html([case Hide of true -> ~""; false -> ?inner_content end])      %% a case tail
?html([?stateless(fun body/1, #{content => ?inner_content})])       %% a child's prop

Placements that raise, because the value is opaque:

  • an attribute value -- bad_template_value, carrying the whole page in the error term (a rendered page inside an attribute has no meaning);
  • measured or inspected -- iolist_size(az:inner_content(Bindings)) raises badarg, as does anything else expecting iodata;
  • wrapped in ?raw/1 -- bad_template_value. There is nothing to opt out of: a content slot already splices the page unescaped, so ?raw(?inner_content) is redundant. Drop the wrapper;
  • inside a list the template cannot see -- a comprehension, or a list built at runtime, is one opaque value and raises. A literal content-slot list such as [~"<hr>", ?inner_content] is fine: it compiles as a fragment, so the page gets a slot of its own.

The value stays opaque rather than paying the per-layer copy that would make those degrade quietly.

Errors with {badkey, inner_content} when the bindings carry no page, i.e. when the module was rendered as something other than a layout.

local(Key, Init)

-spec local(Key, Init) -> map() when Key :: binary() | atom(), Init :: term().

Alias for arizona_template:local/2.

native(Elems)

-spec native(term()) -> no_return().

Parse transform stub. The parse transform replaces az:native(...) (or arizona_template:native(...)) calls with compiled JSON template maps before code generation.

raw(Value)

-spec raw(Value) -> {arizona_raw, term()} when Value :: term().

Alias for arizona_template:raw/1.

stateful(Handler, Props)

-spec stateful(Handler, Props) -> arizona_template:stateful_descriptor()
                  when Handler :: module(), Props :: map().

Alias for arizona_template:stateful/2.

stateless(Callback, Props)

-spec stateless(Callback, Props) -> arizona_template:stateless_descriptor()
                   when Callback :: fun((map()) -> arizona_template:template()), Props :: map().

Alias for arizona_template:stateless/2.

stateless(Handler, Fun, Props)

-spec stateless(Handler, Fun, Props) -> arizona_template:stateless_descriptor()
                   when Handler :: module(), Fun :: atom(), Props :: map().

Alias for arizona_template:stateless/3.

terminal(Elems)

-spec terminal(term()) -> no_return().

Parse transform stub. The parse transform replaces az:terminal(...) (or arizona_template:terminal(...)) calls with compiled ANSI template maps before code generation.

track(Key)

-spec track(Key) -> ok when Key :: term().

Alias for arizona_template:track/1.

with(Keys, Bindings)

-spec with(Keys, Bindings) -> map() when Keys :: [term()], Bindings :: map().

Alias for arizona_template:with/2.