Skip to content

Instantly share code, notes, and snippets.

@fumokmm

fumokmm/markup.ghtml

Created Oct 21, 2010
Embed
What would you like to do?
html {
head {
meta('http-equiv': 'Content-Type', content: 'text/html; charset=UTF-8')
title('MarkupBuilderでHTMLを生成')
style(type:'text/css') {
mkp.yieldUnescaped('''
<!--
body {
color : #000000;
background : #E6DED9;
font-size : var.size;
}
//-->
''')
}
}
body() {
mkp.comment('''
追記:2010-05-14
バージョンが上がってyieldUnescapedやyieldや
ここでも使っているcommentみたいなヘルパーメソッドは
mkp. を付けることが必要になったみたいです。
''')
mkp.yield('MarkupBuilderのテストです。'); br()
a(href:'http://d.hatena.ne.jp/fumokmm/', 'No Programming, No Life'); br()
a(href:'http://d.hatena.ne.jp/fumokmm/20090131/1233428513', 'MarkupBuilderでHTML生成を試してみた'); br()
mkp.yield('↑当ブログはこちらからどうぞ'); br()
}
}
import groovy.xml.MarkupBuilder
MarkupBuilder.metaClass.define {
fromString = { str, var = [:] ->
def code = "{-> ${str} }" // a closure
def received = new GroovyShell([var: var] as Binding).evaluate(code)
received.delegate = delegate // MarkupBuilderにデリゲート
received()
}
fromFile = { file, var = [:] ->
delegate.fromString(file.getText('UTF-8'), var)
}
}
int size = 100
def writer = new File('groovy_htmlbuilder_test.html').newPrintWriter('UTF-8')
def html = new MarkupBuilder(writer)
html.doubleQuotes = true // 属性は ' じゃなくて " で。
html.fromFile(new File('markup.ghtml'), [size: size]) // 引数も渡せる!
writer.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment