Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added escape function

  • Loading branch information...
commit 20f022319825d290c4d92bd85f6ea07d839f6f96 1 parent 7a3d916
Eric Man authored
View
33 README
@@ -0,0 +1,33 @@
+A lua template library. It has django style extends and block/endblock.
+See output.html and main.lua for an example.
+
+Besides the for loop, other controls you can use in templates are:
+
+1. repeat..until
+2. if..[elseif]..[else]..end
+3. while..end
+4. for..end
+
+Their syntax is identical to the lua syntax for the same controls. Controls can be nested within each other.
+
+Quickstart
+
+- Template.load(filename)
+Loads a template at filename
+e.g.
+require 'tier.template'
+local a = tier.Template.load("templates/example2.html")
+
+- template:render(args)
+"args" is a table of arguments to be passed to the template. Returns a string.
+
+e.g.
+require 'tier.template'
+local a = tier.Template.load("templates/example2.html")
+print(a:render{planet="Earth",
+ answer=function() return "42" end,
+ name="example2.html",
+ replaced="--replaced by a block--"})
+
+
+
View
3  main.lua
@@ -1,5 +1,8 @@
require 'tier.template'
+require 'tier.util'
local a = tier.Template.load("templates/example2.html")
+
+
print(a:render{planet="Earth",
answer=function() return "42" end,
name="example2.html",
View
22 templates/example2.html
@@ -86,34 +86,18 @@
<h3>After the nested for loop:</h3>
<h4>The value of x is now {{ x }} </h4>
<div class="src">
- <span>{{</span>
- x
- <span>}}</span>
+ {{ tier.escape_html [[{{ x }}]] }}
</div>
<h4>The value of y is now {{ y }} </h4>
<div class="src">
- <span>{{</span>
- y
- <span>}}</span>
+ {{ tier.escape_html [[{{ y }}]] }}
</div>
<h5>All template variables are global to all templates being rendered. i.e. this template, the parent template and the parent of that template, and any templates that were included anywhere by any of those templates. </h5>
<h5>Also, block names and argument names are in the same namespace; You can replace a block of a template the same way you set variables of a template. See lua source at <a href="#top">top</a>.</h5>
{% block replaced %}gibberish{% endblock %}
<p>
<div class="src">
- <span>{%</span>
- block replaced
- <span>%}</span>
- gibberish
- <span>{%</span>
- endblock
- <span>%}</span>
- </div>
- <h3>The answer to the universe and everything is: {{ answer() }} </h3>
- <div class="src">
- <span>{{</span>
- answer()
- <span>}}</span>
+ {{ tier.escape_html [[{% block replaced %}gibberish{% endblock %}]] }}
</div>
<h3> Time taken to generate this template:</h3>
<pre>
View
2  tier/template.lua
@@ -287,6 +287,8 @@ function Template:parse(str)
table.insert(tree, Text(str:sub(prev, (start or #str+1)-1), self.path, str, prev))
if not start then break end
local tag = str:sub(start, finish)
+ -- a tag split into multiple lines is not a tag
+ -- a tag with an unknown symbol is not a tag, either
if Tag[tag:sub(2, 2)] and not tag:find("\n") then
table.insert(tree, Tag(tag, self.path, str, start))
else
View
17 tier/util.lua
@@ -101,6 +101,21 @@ function serialize(t)
return table.concat(o, ";")
end
+-- escape html
+function escape_html(s)
+ local replacement = {"<", "&lt;",
+ ">", "&gt;",
+ "{{", "\n<span>{{</span>\n",
+ "{%%", "\n<span>{%%</span>\n",
+ "%%}", "\n<span>%%}</span>\n",
+ "}}", "\n<span>}}</span>\n"}
+ local str, n = s, 0;
+ for i=1, #replacement, 2 do
+ str, n = str:gsub(replacement[i], replacement[i+1]);
+ end
+ return str
+end
+
-- Complete clone of a table
function clone(u, copied)
copied = copied or {}
@@ -311,7 +326,7 @@ function List:iter()
local i = 0
local prev = Pair(nil, self.first)
return function()
- node = prev.cdr
+ local node = prev.cdr
if not node then
return nil
end
Please sign in to comment.
Something went wrong with that request. Please try again.