Skip to content

Instantly share code, notes, and snippets.

@mascarenhas
Forked from nrk/cosmo-and-mustache.lua
Created October 22, 2009 16:59
Show Gist options
  • Save mascarenhas/216085 to your computer and use it in GitHub Desktop.
Save mascarenhas/216085 to your computer and use it in GitHub Desktop.
require 'luarocks.require'
require 'cosmo'
require 'mustache'
-- This needs the Cosmo in source control to work (because of the nested long strings and
-- cosmo.last - install with luarocks install cosmo --from=http://luarocks.org/repositories/rocks-cvs
do
local template = "$cards[[$if{show}[[$rank of $suit$if{not _last}[[, ]]]]]]"
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, { cards = cosmo.last(view), ["if"] = cosmo.cif })
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