From 235ccf23b16f1879e6b4b9947f4e9d2ab4d1d11e Mon Sep 17 00:00:00 2001 From: Marc Worrell Date: Tue, 26 Nov 2024 11:19:17 +0100 Subject: [PATCH] Allow 'undefined' for include templates --- src/template_compiler_runtime_internal.erl | 38 ++++++++++++++++++++-- test/template_compiler_include_SUITE.erl | 6 ++++ test/test-data/include_undefined.tpl | 1 + 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 test/test-data/include_undefined.tpl diff --git a/src/template_compiler_runtime_internal.erl b/src/template_compiler_runtime_internal.erl index d767e2a..b9d17c1 100644 --- a/src/template_compiler_runtime_internal.erl +++ b/src/template_compiler_runtime_internal.erl @@ -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 diff --git a/test/template_compiler_include_SUITE.erl b/test/template_compiler_include_SUITE.erl index bbadeeb..3bb9bc7 100644 --- a/test/template_compiler_include_SUITE.erl +++ b/test/template_compiler_include_SUITE.erl @@ -22,6 +22,7 @@ groups() -> [include_test ,include_dynamic_test ,include_args_test + ,include_undefined_test ,compose_test ,compose_inherit_test ]}]. @@ -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), diff --git a/test/test-data/include_undefined.tpl b/test/test-data/include_undefined.tpl new file mode 100644 index 0000000..73e7833 --- /dev/null +++ b/test/test-data/include_undefined.tpl @@ -0,0 +1 @@ +a{% include template %}c \ No newline at end of file