NAME tagz.rb SYNOPSIS require Tagz include Tagz.globally a_(:href => "/foo"){ "bar" } #=> bar DESCRIPTION tagz.rb is generates html, xml, or any sgml variant like a small ninja running across the backs of a herd of giraffes swatting of heads like a mark-up weedwacker. weighing in at less than 200 lines of code tagz.rb adds an html syntax to ruby that is both unobtrusive, safe, and available globally to objects without the need for any builder or superfluous objects. tagz.rb is designed for applications that generate html to be able to do so easily in any context without heavyweight syntax or scoping issues, like a ninja sword through butter. INSTALL gem install tagz HISTORY 4.2.0 - general lib cleanup - introduction of dual-mixin technique (Tagz.globally) - few small bug fixes - ninja tales SAMPLES <========< samples/a.rb >========> ~ > cat samples/a.rb # # in the simplest case tagz generates html using a syntax which safely mixes # in to any object # require 'tagz' include Tagz.globally class GiraffeModel def link a_(:href => "/giraffe/neck/42"){ "whack!" } end end puts GiraffeModel.new.link ~ > ruby samples/a.rb whack! <========< samples/b.rb >========> ~ > cat samples/b.rb # # tagz.rb mixes quite easily with your favourite templating engine, avoiding # the need for '<% rows.each do |row| %> ... <% row.each do |cell| %> ' # madness and other types of logic to be coded in the templating language, # leaving templating to template engines and logic and looping to ruby - # unencumbered by extra funky syntax # require 'tagz' include Tagz.globally require 'erb' rows = %w( a b c ), %w( 1 2 3 ) template = ERB.new <<-ERB <%= if rows table_{ rows.each do |row| tr_{ row.each do |cell| td_{ cell } end } end } end %> ERB puts template.result(binding) ~ > ruby samples/b.rb
abc
123
<========< samples/c.rb >========> ~ > cat samples/c.rb # # once you've learned to generate html using tagz you're primed to generate # xml too # require 'tagz' include Tagz.globally doc = xml_{ giraffe_{ 'large' } ninja_{ 'small' } } puts doc ~ > ruby samples/c.rb largesmall <========< samples/d.rb >========> ~ > cat samples/d.rb # # tagz.rb doesn't cramp your style, allowing even invalid html to be # generated. note the use of the 'tagz' method, which can be used both to # capture output and to append content to the top of the stack. # require 'tagz' include Tagz.globally def header tagz{ html_ body_(:class => 'ninja-like', :id => 'giraffe-slayer') tagz << "\n\n" } end def footer tagz{ tagz << "\n\n" body_ html_ } end puts header, footer ~ > ruby samples/d.rb <========< samples/e.rb >========> ~ > cat samples/e.rb # # tagz.rb allows a safer method of mixin which requires any tagz methods to be # insider a tagz block - tagz generating methods outside a tagz block with # raise an error if tagz is included this way. also notice that the error is # reported from where it was raised - not from the bowels of the the tagz.rb # lib. # require 'tagz' include Tagz puts tagz{ html_{ 'works only in here' } } begin html_{ 'not out here' } rescue Object => e p :backtrace => e.backtrace end ~ > ruby samples/e.rb works only in here {:backtrace=>["samples/e.rb:17"]}