Skip to content

Commit

Permalink
Add function includes/0 to the generated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
mworrell committed Sep 28, 2023
1 parent ac7b391 commit 3cb7e61
Show file tree
Hide file tree
Showing 16 changed files with 101 additions and 42 deletions.
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ Hello
{% block a %}this is block a{% endblock %}
World
{% block b %}this is block b{% endblock %}
{% optional include "foo.tpl" %}
```

Will be compiled to an Erlang module with the following structure:
Expand All @@ -69,6 +70,7 @@ Will be compiled to an Erlang module with the following structure:
blocks/0,
module/0,
extends/0,
includes/0,
filename/0,
mtime/0,
is_autoid/0,
Expand All @@ -95,11 +97,23 @@ module() -> tpl_7bae8076a5771865123be7112468b79e9d78a640.
%% The template that this template extends on.
extends() -> undefined.

%% The templates that this template includes.
%% Includes which use variable names for the template name are not listed.
includes() -> [
#{
template => <<"foo.tpl">>,
line => 5,
column => 20,
method => optional, % normal | all | optional
is_catinclude => false
}
].

%% The filename of this template
filename() -> <<"foo/bar/a.tpl">>.

%% The modification time of the template file on compilation
mtime() - {{2019,1,31},{11,51,49}}.
mtime() - {{2023,9,28},{11,51,49}}.

%% Flag if the autoid ("#id") construct is used in this template.
is_autoid() -> false.
Expand Down
5 changes: 3 additions & 2 deletions src/filter_template_compiler_test.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Filter used in testing the template compiler. Also
%% an example on how to write filters.
%% @end

%% Copyright 2016 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions src/filter_template_compiler_test_set.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2022 Marc Worrell
%% @copyright 2022-2023 Marc Worrell
%% @doc Filter used in testing the template compiler. Sets a process dict value
%% to trace execution or expressions.
%% @end

%% Copyright 2022 Marc Worrell
%% Copyright 2022-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
15 changes: 8 additions & 7 deletions src/template_compiler.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016-2021 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Main template compiler entry points.
%% @end

%% Copyright 2016-2021 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -295,10 +296,10 @@ compile_binary(Tpl, Filename, Options, Context) when is_binary(Tpl) ->
cs(Module, Filename, Options, Context),
Options)
of
{ok, {Extends, BlockAsts, TemplateAst, IsAutoid}} ->
{ok, {Extends, Includes, BlockAsts, TemplateAst, IsAutoid}} ->
Forms = template_compiler_module:compile(
Module, Filename, Mtime, IsAutoid, Runtime,
Extends, BlockAsts, TemplateAst),
Extends, Includes, BlockAsts, TemplateAst),
compile_forms(Filename, Forms);
{error, _} = Error ->
Error
Expand Down Expand Up @@ -405,17 +406,17 @@ cs(Module, Filename, Options, Context) ->
compile_tokens({ok, {extends, {string_literal, _, Extend}, Elements}}, CState, _Options) ->
Blocks = find_blocks(Elements),
{Ws, BlockAsts} = compile_blocks(Blocks, CState),
{ok, {Extend, BlockAsts, undefined, Ws#ws.is_autoid_var}};
{ok, {Extend, Ws#ws.includes, BlockAsts, undefined, Ws#ws.is_autoid_var}};
compile_tokens({ok, {overrules, Elements}}, CState, _Options) ->
Blocks = find_blocks(Elements),
{Ws, BlockAsts} = compile_blocks(Blocks, CState),
{ok, {overrules, BlockAsts, undefined, Ws#ws.is_autoid_var}};
{ok, {overrules, Ws#ws.includes, BlockAsts, undefined, Ws#ws.is_autoid_var}};
compile_tokens({ok, {base, Elements}}, CState, _Options) ->
Blocks = find_blocks(Elements),
{Ws, BlockAsts} = compile_blocks(Blocks, CState),
CStateElts = CState#cs{blocks = BlockAsts},
{Ws1, TemplateAsts} = template_compiler_element:compile(Elements, CStateElts, Ws),
{ok, {undefined, BlockAsts, TemplateAsts, Ws1#ws.is_autoid_var}};
{ok, {undefined, Ws1#ws.includes, BlockAsts, TemplateAsts, Ws1#ws.is_autoid_var}};
compile_tokens({error, {Loc, template_compiler_parser, Msg}}, #cs{ filename = Filename }, Options) ->
% Try format the Yecc error
Err = split_loc(Loc),
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_admin.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016-2020 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Administrate all compiled templates and compilers in flight.
%% @end

%% Copyright 2016-2020 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_app.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Template compiler application.
%% @end

%% Copyright 2016 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
26 changes: 21 additions & 5 deletions src/template_compiler_element.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Compile main block elements to erl_syntax trees.
%% @end

%% Copyright 2016 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -678,7 +679,8 @@ include({_, SrcPos, _}, Method, Template, ArgsList, IsContextVars, #cs{runtime=R
{context_vars, erl_syntax:abstract(CState#cs.context_vars)},
{is_context_vars, erl_syntax:abstract(IsContextVars)}
]),
{Ws1, Ast}.
Ws2 = maybe_add_include(Template, Method, false, Ws1),
{Ws2, Ast}.


catinclude({_, SrcPos, _}, Method, Template, IdAst, ArgsList, IsContextVars, #cs{runtime=Runtime} = CState, Ws) when is_atom(Method) ->
Expand All @@ -702,9 +704,23 @@ catinclude({_, SrcPos, _}, Method, Template, IdAst, ArgsList, IsContextVars, #cs
{context, erl_syntax:variable(CState#cs.context_var)},
{context_vars, erl_syntax:abstract(CState#cs.context_vars)}
]),
{Ws1, Ast}.

Ws2 = maybe_add_include(Template, Method, true, Ws1),
{Ws2, Ast}.

maybe_add_include({string_literal, SrcPos, Text}, Method, IsCatinclude, Ws) ->
{_, Line, Column} = SrcPos,
Tpl = #{
template => Text,
line => Line,
column => Column,
method => Method,
is_catinclude => IsCatinclude
},
Ws#ws{
includes = [ Tpl | Ws#ws.includes ]
};
maybe_add_include(_Token, _Method, _IsCatinclude, Ws) ->
Ws.

expr_list(ExprList, CState, Ws) ->
lists:foldr(
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_expr.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016-2020 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Compile expressions to erl_syntax trees.
%% @end

%% Copyright 2016-2020 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
8 changes: 5 additions & 3 deletions src/template_compiler_internal.hrl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Template compiler internal definitions.
%% @end

%% Copyright 2016 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -44,7 +45,8 @@
nr = 1 :: integer(),
custom_tags = [],
is_forloop_var = false :: boolean(),
is_autoid_var = false :: boolean()
is_autoid_var = false :: boolean(),
includes = [] :: [ binary() ]
}).

%% @doc State for the compiler. Also records the current block's arguments variable, and context variable.
Expand Down
15 changes: 9 additions & 6 deletions src/template_compiler_module.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Build the template module from the parts.
%% @end

%% Copyright 2016 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand All @@ -20,16 +21,16 @@
-author('Marc Worrell <marc@worrell.nl>').

-export([
compile/8
compile/9
]).

-include_lib("syntax_tools/include/merl.hrl").
-include("template_compiler.hrl").
-include("template_compiler_internal.hrl").

compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, BlockAsts, undefined) ->
compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, BlockAsts, erl_syntax:abstract(<<>>));
compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, BlockAsts, TemplateAst) ->
compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, Includes, BlockAsts, undefined) ->
compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, Includes, BlockAsts, erl_syntax:abstract(<<>>));
compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, Includes, BlockAsts, TemplateAst) ->
Now = os:timestamp(),
BlockNames = [ BN || {BN, _Tree, _Ws} <- BlockAsts ],
Forms = lists:flatten(
Expand All @@ -41,6 +42,7 @@ compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, BlockAsts, Template
"blocks/0,",
"module/0,",
"extends/0,",
"includes/0,",
"filename/0,",
"mtime/0,",
"is_autoid/0,"
Expand All @@ -51,6 +53,7 @@ compile(Module, Filename, Mtime, IsAutoid, Runtime, Extends, BlockAsts, Template
"blocks() -> _@BlockNames@.",
"module() -> _@Module@.",
"extends() -> _@Extends@.",
"includes() -> _@Includes@.",
"filename() -> _@Filename@.",
"mtime() -> _@Mtime@.",
"is_autoid() -> _@IsAutoid@.",
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_operators.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2010-2022 Marc Worrell
%% @copyright 2010-2023 Marc Worrell
%% @doc Operators for expression evaluation in templates
%% @end

%% Copyright 2010-2022 Marc Worrell
%% Copyright 2010-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_runtime.erl
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016-2020 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Simple runtime for the compiled templates. Needs to be
%% copied and adapted for different environments.
%% @end

%% Copyright 2016-2020 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_runtime_internal.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016-2022 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Callback routines for compiled templates.
%% @end

%% Copyright 2016-2022 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_sup.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Supervisor for the template compiler and administration.
%% @end

%% Copyright 2016 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
5 changes: 3 additions & 2 deletions src/template_compiler_utils.erl
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
%% @author Marc Worrell <marc@worrell.nl>
%% @copyright 2016 Marc Worrell
%% @copyright 2016-2023 Marc Worrell
%% @doc Misc support routines.
%% @end

%% Copyright 2016 Marc Worrell
%% Copyright 2016-2023 Marc Worrell
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
Expand Down
13 changes: 13 additions & 0 deletions test/template_compiler_include_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

-include_lib("common_test/include/ct.hrl").

-include("../include/template_compiler.hrl").

-compile(export_all).


Expand Down Expand Up @@ -40,6 +42,17 @@ include_test(_Config) ->
% io:format(user, "~p", [Config]),
{ok, Bin1} = template_compiler:render("include.tpl", #{}, [], undefined),
<<"abc">> = iolist_to_binary(Bin1),

{ok, #template_file{ filename = Filename }} = template_compiler_runtime:map_template(<<"include.tpl">>, [], undefined),
{ok, Mod} = template_compiler:lookup(Filename, [], undefined),
[ #{
template := <<"include_b.tpl">>,
line := 1,
column := _,
is_catinclude := false,
method := normal
} ] = Mod:includes(),

ok.

include_dynamic_test(_Config) ->
Expand Down

0 comments on commit 3cb7e61

Please sign in to comment.