nrk (owner)

Forks

Revisions

gist: 216044 Download_button fork
public
Description:
Similarities between cosmo and mustache
Public Clone URL: git://gist.github.com/216044.git
Embed All Files: show embed
cosmo-and-mustache.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
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