Skip to content

Commit b3c18ca

Browse files
authored
Allow 'undefined' for include templates (#52)
* Allow 'undefined' for include templates * Run test on 25, 26 and 27
1 parent 97429f4 commit b3c18ca

File tree

4 files changed

+43
-4
lines changed

4 files changed

+43
-4
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313

1414
strategy:
1515
matrix:
16-
otp_version: [24,25,26]
16+
otp_version: [25,26,27]
1717
os: [ubuntu-latest]
1818

1919
container:

src/template_compiler_runtime_internal.erl

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,9 +201,41 @@ block_inherit(SrcPos, Module, Block, Vars, BlockMap, Runtime, Context) ->
201201

202202

203203
%% @doc Include a template.
204-
-spec include({File::binary(), Line::integer(), Col::integer()}, normal|optional|all,
205-
template_compiler:template(), list({atom(),term()}), atom(), list(binary()), boolean(), map(), term()) ->
206-
template_compiler:render_result().
204+
-spec include(SrcPos, Method, Template, Args, Runtime, ContextVars, IsContextVars, Vars, Context) -> Output when
205+
SrcPos :: {File::binary(), Line::integer(), Col::integer()},
206+
Method :: normal | optional | all,
207+
Template :: template_compiler:template() | undefined,
208+
Args :: list({atom(),term()}),
209+
Runtime :: atom(),
210+
ContextVars :: list(binary()),
211+
IsContextVars :: boolean(),
212+
Vars :: map(),
213+
Context :: term(),
214+
Output :: template_compiler:render_result().
215+
include(SrcPos, normal, undefined, _Args, _Runtime, _ContextVars, _IsContextVars, _Vars, _Context) ->
216+
{SrcFile, SrcLine, _SrcCol} = SrcPos,
217+
?LOG_ERROR(#{
218+
text => <<"Included template not found">>,
219+
template => undefined,
220+
srcpos => SrcPos,
221+
result => error,
222+
reason => enoent,
223+
at => SrcFile,
224+
line => SrcLine
225+
}),
226+
<<>>;
227+
include(SrcPos, _Method, undefined, _Args, _Runtime, _ContextVars, _IsContextVars, _Vars, _Context) ->
228+
{SrcFile, SrcLine, _SrcCol} = SrcPos,
229+
?LOG_DEBUG(#{
230+
text => <<"Included template not found">>,
231+
template => undefined,
232+
srcpos => SrcPos,
233+
result => error,
234+
reason => enoent,
235+
at => SrcFile,
236+
line => SrcLine
237+
}),
238+
<<>>;
207239
include(SrcPos, Method, Template, Args, Runtime, ContextVars, IsContextVars, Vars, Context) ->
208240
Vars1 = lists:foldl(
209241
fun

test/template_compiler_include_SUITE.erl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ groups() ->
2222
[include_test
2323
,include_dynamic_test
2424
,include_args_test
25+
,include_undefined_test
2526
,compose_test
2627
,compose_inherit_test
2728
]}].
@@ -74,6 +75,11 @@ include_args_test(_Config) ->
7475
<<"a3:2:truec">> = iolist_to_binary(Bin1),
7576
ok.
7677

78+
include_undefined_test(_Config) ->
79+
{ok, Bin1} = template_compiler:render("include_undefined.tpl", #{ template => undefined }, [], undefined),
80+
<<"ac">> = iolist_to_binary(Bin1),
81+
ok.
82+
7783
compose_test(_Config) ->
7884
{ok, Bin1} = template_compiler:render("compose.tpl", #{}, [], undefined),
7985
<<"AxB1yC">> = iolist_to_binary(Bin1),

test/test-data/include_undefined.tpl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
a{% include template %}c

0 commit comments

Comments
 (0)