Skip to content

Instantly share code, notes, and snippets.

@tdtds
Created July 7, 2010 06:02
Show Gist options
  • Save tdtds/466364 to your computer and use it in GitHub Desktop.
Save tdtds/466364 to your computer and use it in GitHub Desktop.
#!/home/sho/bin/ruby19
class Template
def initialize( &block )
@child = []
instance_eval( &block )
end
def result
@child.join( "\n" )
end
def method_missing( name, *args, &block )
if block
eval "@child << #{name.to_s.capitalize}::new( &block ).result"
else
eval "@#{name}, = args"
end
end
end
class Html < Template
def result
<<-HTML.gsub( /^\t+/, '' )
<html>
#{@child.join( "\n" )}
</html>
HTML
end
end
class Head < Template
def result
<<-HTML
<head>
<title>#{@title}</title>
<meta keyword="generator" content="#{@generator}">
</head>
HTML
end
end
class Footer < Template
def result
<<-HTML
<div class="footer">Copyright (C) 2010 by TADA Tadashi</div>
HTML
end
end
def html( &block )
puts Html::new( &block ).result
end
class Head < Template
end
class Footer < Template
end
html {
head {
title 'template testing'
generator 'CBCMS Version 0.0'
}
footer {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment