Browse Source

Add auto_escape option.

In order to be Django compatible, the `auto_escape` option should be
used when compiling the template.

Fixes #80.
Andreas Stenius 11 years ago
parent
commit
3462bd24d4
3 changed files with 22 additions and 8 deletions
  1. 13 7
      README.markdown
  2. 4 0
      src/erlydtl_compiler.erl
  3. 5 1
      tests/src/erlydtl_unittests.erl

+ 13 - 7
README.markdown

@@ -17,10 +17,10 @@ Template Languge, there are still a few
 Compilation
 Compilation
 -----------
 -----------
 
 
-To compile ErlyDTL, run 
+To compile ErlyDTL, run
 
 
     make
     make
-    
+
 in this directory.
 in this directory.
 
 
 
 
@@ -40,15 +40,15 @@ Usage:
 Result:
 Result:
 
 
     ok %% existing compiled template is up to date.
     ok %% existing compiled template is up to date.
-    
+
     {ok, Module}
     {ok, Module}
     {ok, Module, Warnings}
     {ok, Module, Warnings}
     {ok, Module, Binary}
     {ok, Module, Binary}
     {ok, Module, Binary, Warnings}
     {ok, Module, Binary, Warnings}
-    
+
     error
     error
     {error, Errors, Warnings}
     {error, Errors, Warnings}
-    
+
 Options is a proplist possibly containing:
 Options is a proplist possibly containing:
 
 
 * `out_dir` - Directory to store generated .beam files. If not
 * `out_dir` - Directory to store generated .beam files. If not
@@ -125,6 +125,9 @@ Options is a proplist possibly containing:
 * `no_env` - Do not read additional options from the OS environment
 * `no_env` - Do not read additional options from the OS environment
   variable `ERLYDTL_COMPILER_OPTIONS`.
   variable `ERLYDTL_COMPILER_OPTIONS`.
 
 
+* `auto_escape` - Turn on auto escape by default (this is on by
+  default in Django).
+
 * `no_load` - Do not load the compiled template.
 * `no_load` - Do not load the compiled template.
 
 
 * `binary` - Include the compiled template binary code in the result
 * `binary` - Include the compiled template binary code in the result
@@ -165,7 +168,7 @@ conjunction with the `custom_tags_module` option above. They can be
 created from a directory of templates thusly:
 created from a directory of templates thusly:
 
 
     erlydtl:compile_dir("/path/to/dir", my_helper_module_name)
     erlydtl:compile_dir("/path/to/dir", my_helper_module_name)
-    
+
     erlydtl:compile_dir("/path/to/dir", my_helper_module_name, Options)
     erlydtl:compile_dir("/path/to/dir", my_helper_module_name, Options)
 
 
 The resulting module will export a function for each template
 The resulting module will export a function for each template
@@ -188,7 +191,7 @@ can be atoms, strings, binaries, or (nested) variables.
 
 
 IOList is the rendered template.
 IOList is the rendered template.
 
 
-    my_compiled_template:render(Variables, Options) -> 
+    my_compiled_template:render(Variables, Options) ->
             {ok, IOList} | {error, Err}
             {ok, IOList} | {error, Err}
 
 
 Same as `render/1`, but with the following options:
 Same as `render/1`, but with the following options:
@@ -233,6 +236,9 @@ Same as `render/1`, but with the following options:
 Differences from standard Django Template Language
 Differences from standard Django Template Language
 --------------------------------------------------
 --------------------------------------------------
 
 
+* `auto_escape` is not enabled by default in ErlyDTL. Pass the
+  `auto_escape` option when compiling your template if you need this.
+
 * `csrf_token` The
 * `csrf_token` The
   [Cross Site Request Forgery](https://docs.djangoproject.com/en/1.6/ref/contrib/csrf/)
   [Cross Site Request Forgery](https://docs.djangoproject.com/en/1.6/ref/contrib/csrf/)
   tag is not implemented.
   tag is not implemented.

+ 4 - 0
src/erlydtl_compiler.erl

@@ -392,6 +392,10 @@ init_context(ParseTrail, DefDir, Module, Options) ->
     Ctx = #dtl_context{},
     Ctx = #dtl_context{},
     Context = #dtl_context{
     Context = #dtl_context{
                  all_options = Options,
                  all_options = Options,
+                 auto_escape = case proplists:get_bool(auto_escape, Options) of
+                                   true -> on;
+                                   false -> off
+                               end,
                  parse_trail = ParseTrail,
                  parse_trail = ParseTrail,
                  module = Module,
                  module = Module,
                  doc_root = proplists:get_value(doc_root, Options, DefDir),
                  doc_root = proplists:get_value(doc_root, Options, DefDir),

+ 5 - 1
tests/src/erlydtl_unittests.erl

@@ -48,7 +48,11 @@ tests() ->
                       [{var1, "<b>bold</b>"}], <<"&lt;b&gt;bold&lt;/b&gt;">>},
                       [{var1, "<b>bold</b>"}], <<"&lt;b&gt;bold&lt;/b&gt;">>},
                      {"Nested autoescape",
                      {"Nested autoescape",
                       <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
                       <<"{% autoescape on %}{{ var1 }}{% autoescape off %}{{ var1 }}{% endautoescape %}{% endautoescape %}">>,
-                      [{var1, "<b>"}], <<"&lt;b&gt;<b>">>}
+                      [{var1, "<b>"}], <<"&lt;b&gt;<b>">>},
+                     {"Autoescape by default (using compile option)",
+                      <<"{{ var1 }}">>,
+                      [{var1, "<b>bold</b>"}], [], [auto_escape],
+                      <<"&lt;b&gt;bold&lt;/b&gt;">>}
                     ]},
                     ]},
      {"string literal", [
      {"string literal", [
                          {"Render literal",
                          {"Render literal",