Permalink
Browse files

Merge pull request #45 from saleyn/variables

Added variables/0 exported function to compiled templates
  • Loading branch information...
2 parents 2f8504a + a6a6983 commit 08200534f5af2182c87d1c881c45caf323dece71 @evanmiller evanmiller committed Oct 21, 2012
Showing with 16 additions and 2 deletions.
  1. +5 −0 README.markdown
  2. +11 −2 src/erlydtl_compiler.erl
View
@@ -169,6 +169,11 @@ List of names/checksums of templates included by the original template
file. Useful for frameworks that recompile a template only when the
template's dependencies change.
+ my_compiled_template:variables() -> [Variable::atom()]
+
+Sorted list of unique variables used in the template's body. The list can
+be used for determining which variable bindings need to be passed to the
+render/3 function.
Differences from standard Django Template Language
--------------------------------------------------
View
@@ -385,6 +385,11 @@ translated_blocks_function(TranslatedBlocks) ->
end,
TranslatedBlocks))])]).
+variables_function(Variables) ->
+ erl_syntax:function(
+ erl_syntax:atom(variables), [erl_syntax:clause([], none,
+ [erl_syntax:list([erl_syntax:atom(S) || S <- lists:usort(Variables)])])]).
+
custom_forms(Dir, Module, Functions, AstInfo) ->
ModuleAst = erl_syntax:attribute(erl_syntax:atom(module), [erl_syntax:atom(Module)]),
ExportAst = erl_syntax:attribute(erl_syntax:atom(export),
@@ -453,6 +458,8 @@ forms(File, Module, {BodyAst, BodyInfo}, {CustomTagsFunctionAst, CustomTagsInfo}
TranslatedBlocksAst = translated_blocks_function(MergedInfo#ast_info.translated_blocks),
+ VariablesAst = variables_function(MergedInfo#ast_info.var_names),
+
BodyAstTmp = erl_syntax:application(
erl_syntax:atom(erlydtl_runtime),
erl_syntax:atom(stringify_final),
@@ -473,11 +480,13 @@ forms(File, Module, {BodyAst, BodyInfo}, {CustomTagsFunctionAst, CustomTagsInfo}
erl_syntax:arity_qualifier(erl_syntax:atom(source), erl_syntax:integer(0)),
erl_syntax:arity_qualifier(erl_syntax:atom(dependencies), erl_syntax:integer(0)),
erl_syntax:arity_qualifier(erl_syntax:atom(translatable_strings), erl_syntax:integer(0)),
- erl_syntax:arity_qualifier(erl_syntax:atom(translated_blocks), erl_syntax:integer(0))
+ erl_syntax:arity_qualifier(erl_syntax:atom(translated_blocks), erl_syntax:integer(0)),
+ erl_syntax:arity_qualifier(erl_syntax:atom(variables), erl_syntax:integer(0))
])]),
[erl_syntax:revert(X) || X <- [ModuleAst, ExportAst, Render0FunctionAst, Render1FunctionAst, Render2FunctionAst,
- SourceFunctionAst, DependenciesFunctionAst, TranslatableStringsAst, TranslatedBlocksAst, RenderInternalFunctionAst,
+ SourceFunctionAst, DependenciesFunctionAst, TranslatableStringsAst,
+ TranslatedBlocksAst, VariablesAst, RenderInternalFunctionAst,
CustomTagsFunctionAst | BodyInfo#ast_info.pre_render_asts]].

0 comments on commit 0820053

Please sign in to comment.