Skip to content

Commit

Permalink
Allow 'undefined' for include templates
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Nov 26, 2024
1 parent 97429f4 commit 235ccf2
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
38 changes: 35 additions & 3 deletions src/template_compiler_runtime_internal.erl
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,41 @@ block_inherit(SrcPos, Module, Block, Vars, BlockMap, Runtime, Context) ->


%% @doc Include a template.
-spec include({File::binary(), Line::integer(), Col::integer()}, normal|optional|all,
template_compiler:template(), list({atom(),term()}), atom(), list(binary()), boolean(), map(), term()) ->
template_compiler:render_result().
-spec include(SrcPos, Method, Template, Args, Runtime, ContextVars, IsContextVars, Vars, Context) -> Output when
SrcPos :: {File::binary(), Line::integer(), Col::integer()},
Method :: normal | optional | all,
Template :: template_compiler:template() | undefined,
Args :: list({atom(),term()}),
Runtime :: atom(),
ContextVars :: list(binary()),
IsContextVars :: boolean(),
Vars :: map(),
Context :: term(),
Output :: template_compiler:render_result().
include(SrcPos, normal, undefined, _Args, _Runtime, _ContextVars, _IsContextVars, _Vars, _Context) ->
{SrcFile, SrcLine, _SrcCol} = SrcPos,
?LOG_ERROR(#{
text => <<"Included template not found">>,
template => undefined,
srcpos => SrcPos,
result => error,
reason => enoent,
at => SrcFile,
line => SrcLine
}),
<<>>;
include(SrcPos, _Method, undefined, _Args, _Runtime, _ContextVars, _IsContextVars, _Vars, _Context) ->
{SrcFile, SrcLine, _SrcCol} = SrcPos,
?LOG_DEBUG(#{
text => <<"Included template not found">>,
template => undefined,
srcpos => SrcPos,
result => error,
reason => enoent,
at => SrcFile,
line => SrcLine
}),
<<>>;
include(SrcPos, Method, Template, Args, Runtime, ContextVars, IsContextVars, Vars, Context) ->
Vars1 = lists:foldl(
fun
Expand Down
6 changes: 6 additions & 0 deletions test/template_compiler_include_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ groups() ->
[include_test
,include_dynamic_test
,include_args_test
,include_undefined_test
,compose_test
,compose_inherit_test
]}].
Expand Down Expand Up @@ -74,6 +75,11 @@ include_args_test(_Config) ->
<<"a3:2:truec">> = iolist_to_binary(Bin1),
ok.

include_undefined_test(_Config) ->
{ok, Bin1} = template_compiler:render("include_undefined.tpl", #{ template => undefined }, [], undefined),
<<"ac">> = iolist_to_binary(Bin1),
ok.

compose_test(_Config) ->
{ok, Bin1} = template_compiler:render("compose.tpl", #{}, [], undefined),
<<"AxB1yC">> = iolist_to_binary(Bin1),
Expand Down
1 change: 1 addition & 0 deletions test/test-data/include_undefined.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
a{% include template %}c

0 comments on commit 235ccf2

Please sign in to comment.