nrk (owner)

Revisions

gist: 180830 Download_button fork
public
Description:
Haml, Cosmo and string templates in a Mercury application
Public Clone URL: git://gist.github.com/180830.git
Embed All Files: show embed
haml_test.lua #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
--[[
Extremely simple demo of a Mercury application that uses a various template
engines to render the final html output. The Haml support is provided by
Norman Clarke's excellent lua-haml (http://github.com/norman/lua-haml/).
]]
 
require 'luarocks.require'
require 'mercury'
require 'haml'
require 'cosmo'
 
module('greetings', package.seeall, mercury.application)
 
local templates = {
    haml = [[
%html(xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en")
%head
%title Mercury - Haml templates
%body
#content
Mercury + Haml templates == instant win
%p= params.word
%p= foo
]],
 
    cosmo = [[
<html>
<head><title>Mercury + Cosmo templates == instant win<title></head>
<body>
<table>$print_words[=[<tr><td>$it</td></tr>]=]</table>
</body>
</html>
]],
 
    string = [[
<html>
<head><title>Mercury + String templates == instant win<title></head>
<body>
This is a %s, and foo is always %s.
</body>
</html>
]],
}
 
get('/haml/:word/', function()
    t.haml(templates.haml, {}, { foo = 'bar' })
end)
 
get('/cosmo/:word/', function()
    local words = { 'this', 'is', 'a', 'test' }
    t.cosmo(templates.cosmo, { print_words = function()
        for _, word in pairs(words) do cosmo.yield(word) end
    end})
end)
 
get('/string/:word/', function()
    t.string(templates.string, params.word, "bar")
end)
 
output-cosmo.html #
1
2
3
4
5
6
7
<html>
  <head><title>Mercury + Cosmo templates == instant win<title></head>
  <body>
    <table><tr><td>this</td></tr><tr><td>is</td></tr><tr><td>a</td></tr><tr><td>test</td></tr></table>
  </body>
</html>
 
output-haml.html #
1
2
3
4
5
6
7
8
9
10
11
12
13
<html lang='en' xml:lang='en' xmlns='http://www.w3.org/1999/xhtml'>
  <head>
    <title>Mercury - Haml templates</title>
  </head>
  <body>
    <div id='content'>
      Mercury + Haml templates == instant win
      <p>test</p>
      <p>bar</p>
    </div>
  </body>
</html>
 
output-string-format.html #
1
2
3
4
5
6
7
<html>
  <head><title>Mercury + String templates == instant win<title></head>
  <body>
    This is a test, and foo is always bar.
  </body>
</html>
 
urls.txt #
1
2
3
4
http://127.0.0.1:7654/haml/test
http://127.0.0.1:7654/cosmo/test
http://127.0.0.1:7654/string/test