Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Hogan.groovy examples
@Grab(group='com.github.plecong', module='hogan-groovy', version='3.0')
import com.github.plecong.hogan.Hogan
def expected = ''
def template = null
def data = [:]
def template_s = ''
// {{{ basic compile()
data = [ screenName: 'plecong' ]
template = Hogan.compile('Follow @{{screenName}}.')
expected = 'Follow @plecong.'
assert expected == template.render(data)
println expected
// }}}
// {{{ basic list and map assigning and nested loops
data = [
l1: [ 10, 20, 30, ],
m1: [
[k1: 100, k2: 'abc'],
[k1: 200, k2: 'def'],
[k1: 300, k2: 'ghi'],
],
m2: [
[
k1: 10,
k2: 'ABC',
k3: [
[
k31: 1234,
k32: 2345,
],
[
k31: 3456,
k32: 4567,
],
],
],
[
k1: 20,
k2: 'DEF',
k3: [
[
k31: 'hello',
k32: 'foo',
],
],
],
],
]
template_s = """
------------
{{#l1}}{{.}},{{/l1}}
{{#m1}}
m1[k1]={{k1}}
m1[k2]={{k2}}
------------
{{/m1}}
{{#m2}}
m2[k1]={{k1}}
m2[k2]={{k2}}
{{#k3}}
m2[k3][k31]={{k31}}
m2[k3][k32]={{k32}}
{{/k3}}
------------
{{/m2}}
"""
template = Hogan.compile(template_s)
expected = """
------------
10,20,30,
m1[k1]=100
m1[k2]=abc
------------
m1[k1]=200
m1[k2]=def
------------
m1[k1]=300
m1[k2]=ghi
------------
m2[k1]=10
m2[k2]=ABC
m2[k3][k31]=1234
m2[k3][k32]=2345
m2[k3][k31]=3456
m2[k3][k32]=4567
------------
m2[k1]=20
m2[k2]=DEF
m2[k3][k31]=hello
m2[k3][k32]=foo
------------
"""
assert expected == template.render(data)
println expected
// }}}
// {{{ map miss!! (list as variables)
data = [
m1: [[k1: 100, k2: 'abc']],
]
template_s = """
------------
{{m1}}
------------
"""
template = Hogan.compile(template_s)
expected = """
------------
[[k1:100, k2:abc]]
------------
"""
assert expected == template.render(data)
println expected
// }}}
// {{{ basic if-then-else
data = [
cond1: true,
item1: 'hello',
cond2: false,
item2: 'bonjour',
cond3: '',
item3: 'morning',
cond4: [],
item4: 'afternoon',
cond5: null,
item5: 'evening',
]
template_s = """
{{#cond1}}
item1={{item1}}
{{/cond1}}
{{^cond1}}
empty(item1)
{{/cond1}}
{{#cond2}}item2={{item2}}{{/cond2}}{{^cond2}}empty(item2){{/cond2}}
{{#cond3}}item3={{item3}}{{/cond3}}{{^cond3}}empty(item3){{/cond3}}
{{#cond4}}item4={{item4}}{{/cond4}}{{^cond4}}empty(item4){{/cond4}}
{{#cond5}}item5={{item5}}{{/cond5}}{{^cond5}}empty(item5){{/cond5}}
"""
template = Hogan.compile(template_s)
expected = """
item1=hello
empty(item2)
item3=morning
empty(item4)
empty(item5)
"""
assert expected == template.render(data)
println expected
// }}}
// {{{ change delimiter & default html escape
data = [ name: '<"\'&>' ]
template = Hogan.compile('name : <{name}>', [delimiters: '<{ }>'])
expected = 'name : &lt;&quot;&#39;&amp;&gt;'
assert expected == template.render(data)
println expected
// }}}
// {{{ default delimiter & use unescape vars explicitly
data = [ name: '<"\'&>' ]
template = Hogan.compile('name2a : {{{name}}}')
expected = 'name2a : <"\'&>'
assert expected == template.render(data)
println expected
// }}}
// {{{ default delimiter & use unescape vars explicitly (2)
data = [ name: '<"\'&>' ]
template = Hogan.compile('name2b : {{&name}}, {{& name}}, {{& name}}')
expected = 'name2b : <"\'&>, <"\'&>, <"\'&>'
assert expected == template.render(data)
println expected
// }}}
// {{{ change delimiter & use unescape vars explicitly
data = [ name: '<"\'&>' ]
template = Hogan.compile('name3 : <{{name}}>', [delimiters: '<{ }>'])
expected = 'name3 : <"\'&>'
assert expected == template.render(data)
println expected
// }}}
// {{{ change delimiter & use unescape vars explicitly (2)
data = [ name: '<"\'&>' ]
template = Hogan.compile('name4 : <{& name}}>', [delimiters: '<{ }>'])
expected = 'name4 : '
// !!!! when delimiter changed, "[left-delimiter]& [tag][right-delimiter]" doesn't work !!!!
assert expected == template.render(data)
println expected
// }}}
// {{{ Groovy Closure as lambda
data = [
name: 'Willy',
wrapped: {
return { text -> "<b>${text}</b>" }
}
]
template = Hogan.compile('{{#wrapped}}{{name}} is awesome.{{/wrapped}}')
expected = '<b>Willy is awesome.</b>'
assert expected == template.render(data)
println expected
// }}}
// {{{ basic partial
data = [ users: [ [name: 'foo', age: 10], [name: 'bar', age: 20] ] ]
partials = [
p1: Hogan.compile('partial-1 : name = {{name}}\n'),
p2: Hogan.compile('partial-2 : age = {{age}}\n'),
]
template_s = """
--------
{{#users}}
{{>p1}}
{{>p2}}
{{/users}}
--------
"""
template = Hogan.compile(template_s)
expected = """
--------
partial-1 : name = foo
partial-2 : age = 10
partial-1 : name = bar
partial-2 : age = 20
--------
"""
assert expected == template.render(data, partials)
println expected
// }}}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.