Skip to content

Instantly share code, notes, and snippets.

@nrk
Created October 22, 2009 16:00
Show Gist options
  • Save nrk/216044 to your computer and use it in GitHub Desktop.
Save nrk/216044 to your computer and use it in GitHub Desktop.
Similarities between cosmo and mustache
require 'luarocks.require'
require 'cosmo'
require 'mustache'
do
local template = "$do_cards[[$rank of $suit, ]]"
local view = {
{ rank = "Ace", suit = "Spades", show = true },
{ rank = "Queen", suit = "Diamonds", show = false },
{ rank = "10", suit = "Hearts", show = true },
}
local result = cosmo.fill(template, {
do_cards = function()
for i,v in ipairs(view) do
if v.show then cosmo.yield(v) end
end
end
})
print(result) -- Ace of Spades, 10 of Hearts,
end
do
local template = "{{#cards}}{{#if_show}}{{rank}} of {{suit}}, {{/if_show}}{{/cards}}"
local view = {
if_show = function() return show end,
cards = {
{ rank = "Ace", suit = "Spades", show = true },
{ rank = "Queen", suit = "Diamonds", show = false },
{ rank = "10", suit = "Hearts", show = true },
}
}
local result = mustache.render(template, view)
print(result) -- Ace of Spades, 10 of Hearts,
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment