Browse Source

Examples: Include templates

Peter Colberg 4 years ago
parent
commit
d0e9f23278

+ 8
- 2
Makefile View File

@@ -12,8 +12,10 @@ INSTALL_D = mkdir -p
12 12
 INSTALL_F = install -m 644
13 13
 
14 14
 FILES_LUA = init.lua
15
-FILES_DOC = index.mdwn INSTALL.mdwn README.mdwn reference.mdwn CHANGES.mdwn
16
-FILES_DOC_HTML = index.html INSTALL.html README.html reference.html CHANGES.html pandoc.css lua-templet.png
15
+FILES_DOC = index.mdwn INSTALL.mdwn README.mdwn examples.mdwn reference.mdwn CHANGES.mdwn
16
+FILES_DOC_HTML = index.html INSTALL.html README.html examples.html reference.html CHANGES.html pandoc.css lua-templet.png
17
+FILES_EXAMPLES_INCLUDE = include.lua
18
+FILES_EXAMPLES_INCLUDE_TEST = included.lua main.lua
17 19
 
18 20
 all: doc
19 21
 
@@ -28,6 +30,10 @@ install:
28 30
 	cd templet && $(INSTALL_F) $(FILES_LUA) $(DESTDIR)$(LUADIR)/templet
29 31
 	$(INSTALL_D) $(DESTDIR)$(DOCDIR)
30 32
 	cd doc && $(INSTALL_F) $(FILES_DOC) $(FILES_DOC_HTML) $(DESTDIR)$(DOCDIR)
33
+	$(INSTALL_D) $(DESTDIR)$(DOCDIR)/examples/include
34
+	cd examples/include && $(INSTALL_F) $(FILES_EXAMPLES_INCLUDE) $(DESTDIR)$(DOCDIR)/examples/include
35
+	$(INSTALL_D) $(DESTDIR)$(DOCDIR)/examples/include/test
36
+	cd examples/include/test && $(INSTALL_F) $(FILES_EXAMPLES_INCLUDE_TEST) $(DESTDIR)$(DOCDIR)/examples/include/test
31 37
 
32 38
 clean:
33 39
 	@$(MAKE) -C doc clean

+ 1
- 0
doc/CHANGES.html View File

@@ -15,6 +15,7 @@
15 15
 <li><a href="index.html">Templet for Lua</a></li>
16 16
 <li><a href="INSTALL.html">Installing</a></li>
17 17
 <li><a href="README.html">User's Guide</a></li>
18
+<li><a href="examples.html">Examples</a></li>
18 19
 <li><a href="reference.html">Reference</a></li>
19 20
 <li><a href="https://lists.colberg.org/listinfo/lua-templet">Mailing list</a></li>
20 21
 <li><a href="CHANGES.html">Releases</a></li>

+ 1
- 0
doc/INSTALL.html View File

@@ -15,6 +15,7 @@
15 15
 <li><a href="index.html">Templet for Lua</a></li>
16 16
 <li><a href="INSTALL.html">Installing</a></li>
17 17
 <li><a href="README.html">User's Guide</a></li>
18
+<li><a href="examples.html">Examples</a></li>
18 19
 <li><a href="reference.html">Reference</a></li>
19 20
 <li><a href="https://lists.colberg.org/listinfo/lua-templet">Mailing list</a></li>
20 21
 <li><a href="CHANGES.html">Releases</a></li>

+ 1
- 1
doc/Makefile View File

@@ -1,4 +1,4 @@
1
-html: CHANGES.html INSTALL.html README.html index.html reference.html
1
+html: index.html INSTALL.html README.html examples.html reference.html CHANGES.html
2 2
 
3 3
 %.html: %.mdwn pandoc.html
4 4
 	pandoc --template=pandoc.html --css=pandoc.css --toc --toc-depth=2 -o $@ $<

+ 1
- 0
doc/README.html View File

@@ -34,6 +34,7 @@ code > span.er { color: #ff0000; font-weight: bold; }
34 34
 <li><a href="index.html">Templet for Lua</a></li>
35 35
 <li><a href="INSTALL.html">Installing</a></li>
36 36
 <li><a href="README.html">User's Guide</a></li>
37
+<li><a href="examples.html">Examples</a></li>
37 38
 <li><a href="reference.html">Reference</a></li>
38 39
 <li><a href="https://lists.colberg.org/listinfo/lua-templet">Mailing list</a></li>
39 40
 <li><a href="CHANGES.html">Releases</a></li>

+ 1
- 0
doc/contents.mdwn View File

@@ -7,6 +7,7 @@ date: "2012-2014"
7 7
 - [Templet for Lua](index.html)
8 8
 - [Installing](INSTALL.html)
9 9
 - [User's Guide](README.html)
10
+- [Examples](examples.html)
10 11
 - [Reference](reference.html)
11 12
 - [Mailing list](https://lists.colberg.org/listinfo/lua-templet)
12 13
 - [Releases](CHANGES.html)

+ 82
- 0
doc/examples.html View File

@@ -0,0 +1,82 @@
1
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2
+<html xmlns="http://www.w3.org/1999/xhtml">
3
+<head>
4
+  <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5
+  <meta http-equiv="Content-Style-Type" content="text/css" />
6
+  <meta name="generator" content="pandoc" />
7
+  <meta name="author" content="Peter Colberg" />
8
+  <title>Examples &mdash; Templet for Lua</title>
9
+  <style type="text/css">code{white-space: pre;}</style>
10
+  <style type="text/css">
11
+table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
12
+  margin: 0; padding: 0; vertical-align: baseline; border: none; }
13
+table.sourceCode { width: 100%; line-height: 100%; }
14
+td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
15
+td.sourceCode { padding-left: 5px; }
16
+code > span.kw { color: #007020; font-weight: bold; }
17
+code > span.dt { color: #902000; }
18
+code > span.dv { color: #40a070; }
19
+code > span.bn { color: #40a070; }
20
+code > span.fl { color: #40a070; }
21
+code > span.ch { color: #4070a0; }
22
+code > span.st { color: #4070a0; }
23
+code > span.co { color: #60a0b0; font-style: italic; }
24
+code > span.ot { color: #007020; }
25
+code > span.al { color: #ff0000; font-weight: bold; }
26
+code > span.fu { color: #06287e; }
27
+code > span.er { color: #ff0000; font-weight: bold; }
28
+  </style>
29
+  <link rel="stylesheet" href="pandoc.css" type="text/css" />
30
+</head>
31
+<body>
32
+<div class="header">
33
+<ul>
34
+<li><a href="index.html">Templet for Lua</a></li>
35
+<li><a href="INSTALL.html">Installing</a></li>
36
+<li><a href="README.html">User's Guide</a></li>
37
+<li><a href="examples.html">Examples</a></li>
38
+<li><a href="reference.html">Reference</a></li>
39
+<li><a href="https://lists.colberg.org/listinfo/lua-templet">Mailing list</a></li>
40
+<li><a href="CHANGES.html">Releases</a></li>
41
+</ul>
42
+</div>
43
+<div class="body">
44
+<h1>Examples</h1>
45
+<div class="toc">
46
+<ul>
47
+<li><a href="#include-templates">Include templates</a></li>
48
+</ul>
49
+</div>
50
+<h2 id="include-templates">Include templates</h2>
51
+<p>This example shows how to implement an <code>include</code> function that evaluates template files included within a template.</p>
52
+<p><code>templet.loadfile</code> loads template files relative to the current directory. Instead we call <a href="http://www.lua.org/manual/5.2/manual.html#pdf-package.searchpath">package.searchpath</a> with <a href="http://www.lua.org/manual/5.2/manual.html#pdf-package.path">package.path</a> to determine the absolute path of a template file from the Lua modules path, both for loading the main and included template files.</p>
53
+<pre class="sourceCode lua"><code class="sourceCode lua"><span class="kw">local</span> templet <span class="ot">=</span> <span class="fu">require</span><span class="ot">(</span><span class="st">&quot;templet&quot;</span><span class="ot">)</span>
54
+
55
+<span class="kw">local</span> <span class="kw">function</span> template<span class="ot">(</span>filename<span class="ot">,</span> env<span class="ot">)</span>
56
+  <span class="kw">local</span> <span class="kw">function</span> include<span class="ot">(</span>filename<span class="ot">)</span>
57
+    <span class="kw">local</span> filename<span class="ot">,</span> err <span class="ot">=</span> package<span class="ot">.</span>searchpath<span class="ot">(</span>filename<span class="ot">,</span> package<span class="ot">.</span>path<span class="ot">)</span>
58
+    <span class="kw">if</span> <span class="kw">not</span> filename <span class="kw">then</span> <span class="kw">return</span> <span class="fu">error</span><span class="ot">(</span>err<span class="ot">)</span> <span class="kw">end</span>
59
+    <span class="kw">local</span> template <span class="ot">=</span> templet<span class="ot">.</span>loadfile<span class="ot">(</span>filename<span class="ot">)</span>
60
+    <span class="kw">return</span> template<span class="ot">(</span>env<span class="ot">)</span>
61
+  <span class="kw">end</span>
62
+  env <span class="ot">=</span> <span class="fu">setmetatable</span><span class="ot">({</span>include <span class="ot">=</span> include<span class="ot">},</span> <span class="ot">{</span>__index <span class="ot">=</span> env<span class="ot">})</span>
63
+  <span class="kw">return</span> include<span class="ot">(</span>filename<span class="ot">)</span>
64
+<span class="kw">end</span></code></pre>
65
+<p>Suppose we have a template file <code>test/included.lua</code>:</p>
66
+<pre class="sourceCode lua"><code class="sourceCode lua"><span class="fu">print</span><span class="ot">(</span><span class="st">&quot;${hello}, ${world}!&quot;</span><span class="ot">)</span></code></pre>
67
+<p>We include <code>test/included.lua</code> in the template file <code>test/main.lua</code>:</p>
68
+<pre class="sourceCode lua"><code class="sourceCode lua">|<span class="kw">for</span> i <span class="ot">=</span> <span class="dv">1</span><span class="ot">,</span> <span class="dv">3</span> <span class="kw">do</span>
69
+$<span class="ot">{</span>include <span class="st">&quot;test.included&quot;</span><span class="ot">}</span>
70
+|<span class="kw">end</span></code></pre>
71
+<p>We evaluate <code>test/main.lua</code> using the function defined above:</p>
72
+<pre class="sourceCode lua"><code class="sourceCode lua"><span class="fu">io.write</span><span class="ot">(</span>template<span class="ot">(</span><span class="st">&quot;test.main&quot;</span><span class="ot">,</span> <span class="ot">{</span>hello <span class="ot">=</span> <span class="st">&quot;Ciao&quot;</span><span class="ot">,</span> world <span class="ot">=</span> <span class="st">&quot;mondo&quot;</span><span class="ot">}))</span></code></pre>
73
+<pre><code>print(&quot;Ciao, mondo!&quot;)
74
+print(&quot;Ciao, mondo!&quot;)
75
+print(&quot;Ciao, mondo!&quot;)</code></pre>
76
+</div>
77
+<div class="footer">
78
+&copy; Copyright 2012-2014, Peter Colberg.
79
+Created using <a href="http://johnmacfarlane.net/pandoc/">Pandoc</a>.
80
+</div>
81
+</body>
82
+</html>

+ 58
- 0
doc/examples.mdwn View File

@@ -0,0 +1,58 @@
1
+---
2
+title: Examples
3
+---
4
+
5
+Include templates
6
+-----------------
7
+
8
+This example shows how to implement an `include` function that evaluates
9
+template files included within a template.
10
+
11
+`templet.loadfile` loads template files relative to the current directory.
12
+Instead we call [package.searchpath] with [package.path] to determine the
13
+absolute path of a template file from the Lua modules path, both for loading
14
+the main and included template files.
15
+
16
+[package.searchpath]: http://www.lua.org/manual/5.2/manual.html#pdf-package.searchpath
17
+[package.path]: http://www.lua.org/manual/5.2/manual.html#pdf-package.path
18
+
19
+~~~ {.lua}
20
+local templet = require("templet")
21
+
22
+local function template(filename, env)
23
+  local function include(filename)
24
+    local filename, err = package.searchpath(filename, package.path)
25
+    if not filename then return error(err) end
26
+    local template = templet.loadfile(filename)
27
+    return template(env)
28
+  end
29
+  env = setmetatable({include = include}, {__index = env})
30
+  return include(filename)
31
+end
32
+~~~
33
+
34
+Suppose we have a template file `test/included.lua`:
35
+
36
+~~~ {.lua}
37
+print("${hello}, ${world}!")
38
+~~~
39
+
40
+We include `test/included.lua` in the template file `test/main.lua`:
41
+
42
+~~~ {.lua}
43
+|for i = 1, 3 do
44
+${include "test.included"}
45
+|end
46
+~~~
47
+
48
+We evaluate `test/main.lua` using the function defined above:
49
+
50
+~~~ {.lua}
51
+io.write(template("test.main", {hello = "Ciao", world = "mondo"}))
52
+~~~
53
+
54
+~~~
55
+print("Ciao, mondo!")
56
+print("Ciao, mondo!")
57
+print("Ciao, mondo!")
58
+~~~

+ 1
- 0
doc/index.html View File

@@ -34,6 +34,7 @@ code > span.er { color: #ff0000; font-weight: bold; }
34 34
 <li><a href="index.html">Templet for Lua</a></li>
35 35
 <li><a href="INSTALL.html">Installing</a></li>
36 36
 <li><a href="README.html">User's Guide</a></li>
37
+<li><a href="examples.html">Examples</a></li>
37 38
 <li><a href="reference.html">Reference</a></li>
38 39
 <li><a href="https://lists.colberg.org/listinfo/lua-templet">Mailing list</a></li>
39 40
 <li><a href="CHANGES.html">Releases</a></li>

+ 1
- 0
doc/reference.html View File

@@ -34,6 +34,7 @@ code > span.er { color: #ff0000; font-weight: bold; }
34 34
 <li><a href="index.html">Templet for Lua</a></li>
35 35
 <li><a href="INSTALL.html">Installing</a></li>
36 36
 <li><a href="README.html">User's Guide</a></li>
37
+<li><a href="examples.html">Examples</a></li>
37 38
 <li><a href="reference.html">Reference</a></li>
38 39
 <li><a href="https://lists.colberg.org/listinfo/lua-templet">Mailing list</a></li>
39 40
 <li><a href="CHANGES.html">Releases</a></li>

+ 14
- 0
examples/include/include.lua View File

@@ -0,0 +1,14 @@
1
+local templet = require("templet")
2
+
3
+local function template(filename, env)
4
+  local function include(filename)
5
+    local filename, err = package.searchpath(filename, package.path)
6
+    if not filename then return error(err) end
7
+    local template = templet.loadfile(filename)
8
+    return template(env)
9
+  end
10
+  env = setmetatable({include = include}, {__index = env})
11
+  return include(filename)
12
+end
13
+
14
+io.write(template("test.main", {hello = "Ciao", world = "mondo"}))

+ 1
- 0
examples/include/test/included.lua View File

@@ -0,0 +1 @@
1
+print("${hello}, ${world}!")

+ 3
- 0
examples/include/test/main.lua View File

@@ -0,0 +1,3 @@
1
+|for i = 1, 3 do
2
+${include "test.included"}
3
+|end

Loading…
Cancel
Save