Skip to content

Instantly share code, notes, and snippets.

@nrk
Created September 4, 2009 10:47
Show Gist options
  • Select an option

  • Save nrk/180830 to your computer and use it in GitHub Desktop.

Select an option

Save nrk/180830 to your computer and use it in GitHub Desktop.
Haml, Cosmo and string templates in a Mercury application
--[[
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)
<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>
<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>
<html>
<head><title>Mercury + String templates == instant win<title></head>
<body>
This is a test, and foo is always bar.
</body>
</html>
http://127.0.0.1:7654/haml/test
http://127.0.0.1:7654/cosmo/test
http://127.0.0.1:7654/string/test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment