Skip to content

Commit b0a0d84

Browse files
committed
Different model_call return format.
1 parent b6f2088 commit b0a0d84

File tree

3 files changed

+24
-8
lines changed

3 files changed

+24
-8
lines changed

src/template_compiler.erl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
%% @author Marc Worrell <marc@worrell.nl>
2-
%% @copyright 2016 Marc Worrell
2+
%% @copyright 2016-2021 Marc Worrell
33
%% @doc Main template compiler entry points.
44

5-
%% Copyright 2016 Marc Worrell
5+
%% Copyright 2016-2021 Marc Worrell
66
%%
77
%% Licensed under the Apache License, Version 2.0 (the "License");
88
%% you may not use this file except in compliance with the License.
@@ -53,6 +53,9 @@
5353
-type template_key() :: {ContextName::term(), Runtime::atom(), template()}.
5454
-type render_result() :: binary() | string() | term() | list(render_result()).
5555

56+
-type model_return() :: {ok, {term(), list()}}
57+
| {error, term()}.
58+
5659
-type builtin_tag() :: image
5760
| image_url
5861
| image_data_url
@@ -77,7 +80,9 @@
7780
template_file/0,
7881
template_key/0,
7982
builtin_tag/0,
80-
translation_message/0
83+
translation_message/0,
84+
render_result/0,
85+
model_return/0
8186
]).
8287

8388

src/template_compiler_expr.erl

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,23 +137,33 @@ compile({apply_filter, Expr, {filter, {identifier, SrcPos, Filter}, FilterArgs}}
137137
erl_syntax:atom(FilterName),
138138
Args)),
139139
{Ws2, Ast};
140-
compile({model, [{identifier, SrcPos, Model} | Path ], OptPayload}, #cs{runtime=Runtime} = CState, Ws) ->
140+
compile({model, [{identifier, SrcPos, Model} | Path ], OptPayload}, #cs{runtime=Runtime, vars_var=Vars} = CState, Ws) ->
141141
{Ws1, PathAsts} = value_lookup_asts(Path, CState, Ws, []),
142142
{Ws2, PayloadAst} = case OptPayload of
143143
none -> {Ws1, erl_syntax:abstract(undefined)};
144144
Expr -> compile(Expr, CState, Ws1)
145145
end,
146+
{Ws3, V1} = template_compiler_utils:var(Ws2),
147+
{Ws4, V2} = template_compiler_utils:var(Ws3),
146148
Ast = merl:qquote(
147149
template_compiler_utils:pos(SrcPos),
148-
"_@runtime:model_call(_@model, _@path, _@payload, _@context)",
150+
"case _@runtime:model_call(_@model, _@path, _@payload, _@context) of "
151+
" {ok, {_@v1, []}} -> _@v1;"
152+
" {ok, {_@v1, _@v2}} when is_list(_@v2) -> "
153+
" _@runtime:find_nested_value(_@v1, _@v2, _@vars, _@context); "
154+
" {error, _} -> undefined "
155+
"end",
149156
[
150157
{model, erl_syntax:atom(binary_to_atom(Model, 'utf8'))},
151158
{path, erl_syntax:list(PathAsts)},
152159
{payload, PayloadAst},
160+
{v1, erl_syntax:variable(V1)},
161+
{v2, erl_syntax:variable(V2)},
162+
{vars, erl_syntax:variable(Vars)},
153163
{context, erl_syntax:variable(CState#cs.context_var)},
154164
{runtime, erl_syntax:atom(Runtime)}
155165
]),
156-
{Ws2, Ast}.
166+
{Ws4, Ast}.
157167

158168

159169
find_value_lookup([{identifier, SrcPos, <<"now">>}], _CState, Ws) ->

src/template_compiler_runtime.erl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@
6666
-callback get_translations(Text :: binary(), Context :: term()) -> binary() | {trans, [{atom(), binary()}]}.
6767
-callback lookup_translation({trans, list({atom(), binary()})}, TplVars :: map(), Context :: term()) -> binary().
6868

69+
-callback model_call(Model::atom(), Path::list(), Payload::term(), Context::term()) -> template_compiler:model_return().
6970
-callback custom_tag(Module::atom(), Args::list(), TplVars::map(), Context::term()) -> template_compiler:render_result().
7071
-callback builtin_tag(template_compiler:builtin_tag(), term(), Args::list(), TplVars::map(), Context::term()) -> template_compiler:render_result().
7172
-callback cache_tag(Seconds::integer(), Name::binary(), Args::list(), function(), TplVars::map(), Context::term()) -> template_compiler:render_result().
@@ -289,9 +290,9 @@ lookup_translation({trans, Tr}, TplVars, _Context) when is_map(TplVars) ->
289290
end.
290291

291292
%% @doc A model call with optional payload. Compiled from m.model.path!payload
292-
-spec model_call(Model::atom(), Path::list(), Payload::term(), Context::term()) -> template_compiler:render_result().
293+
-spec model_call(Model::atom(), Path::list(), Payload::term(), Context::term()) -> template_compiler:model_return().
293294
model_call(Model, Path, Payload, _Context) ->
294-
io_lib:format("model:~p ~p :: ~p", [ Model, Path, Payload ]).
295+
{ok, {io_lib:format("model:~p ~p :: ~p", [ Model, Path, Payload ]), []}}.
295296

296297
%% @doc Render a custom tag (Zotonic scomp) - this can be changed to more complex runtime lookups.
297298
-spec custom_tag(Tag::atom(), Args::list(), Vars::map(), Context::term()) -> template_compiler:render_result().

0 commit comments

Comments
 (0)