From: "Ara.T.Howard" Date: 2003-12-20T09:51:55+09:00 Subject: Re: Amrita question(s) On Sat, 20 Dec 2003, Michael Campbell wrote: > Perhaps my example is overloaded too much on terms; let me rephrase. > > I want the following to display IFF my model contains the data element > "element". > > Field Label: element > > Now, I can do this like so: > > code: {:element => "Field Label: element" } html: > > or something like > > code: {:elementlabel => "Field Label: ", :element => "element" } html: id=elementlabel> > > but neither is very satisfying. I don't like either solution as they put my > markup directly into the code (and my model data), and in the second case, > requires that I manipulate 2 values for every labelled output. > > What I'd *LIKE* somehow is to have > > html: Field Label: > > and some version of code that omits the "Field Label:" part if :element > doesn't exist; I understand the actual value referenced by the :element key > already won't show. In other words, "sometag" is just a conditional "is it > there or not?" test, where "span" tests for existence AND displays it. amrita can accept a proc as it's model data, with it you can do pretty much whatever you want: ~/eg/ruby > cat c.rb require 'amrita/template' include Amrita t = TemplateText.new <<-html Label : html # here's one way data = { :element => lambda{|e| e.body + 'foobar'} } t.expand STDOUT, data # you could simply automate this too def append txt; lambda{|e| e.body + txt}; end data = {:element => append('foobar')} t.expand STDOUT, data # and of course it doesn't show up at all if no key is given data = {:element => nil} t.expand STDOUT, data ~/eg/ruby > ruby c.rb Label : foobar Label : foobar ~/eg/ruby > # note blank line above for last case other ways i can think of include things like def label_maker key, txt {key => {:label=>true,:txt=>txt}} end and then something like: t = TemplateText.new <<-html Label : html data = { :element => label_maker(:element, 'foobar'), } > As for your comment about blocking out big chunks of HTML, how do you do > that? If you have sections separated by
... >
, how do you make the Amrita parser not replace the HTML code with the > value of the :commentoutthischunk value? YOu can't use > {:commentoutthischunk => nil}, but any other non-nil value you use will not > only eliminate your HTML, but be printed in its place. (Or do you use a > space or something innocuous?)
i just meant nil or not. my usage is something like: t = TemplateText.new <<-html ... ... ... html and i simply do page # => :main_page, :page_0, :page_1 data = Hash[page => {}][page] # begin in a sub hash for this page then proceed normally -a -- ATTN: please update your address books with address below! =============================================================================== | EMAIL :: Ara [dot] T [dot] Howard [at] noaa [dot] gov | PHONE :: 303.497.6469 | ADDRESS :: E/GC2 325 Broadway, Boulder, CO 80305-3328 | STP :: http://www.ngdc.noaa.gov/stp/ | NGDC :: http://www.ngdc.noaa.gov/ | NESDIS :: http://www.nesdis.noaa.gov/ | NOAA :: http://www.noaa.gov/ | US DOC :: http://www.commerce.gov/ | | The difference between art and science is that science is what we | understand well enough to explain to a computer. | Art is everything else. | -- Donald Knuth, "Discover" | | /bin/sh -c 'for l in ruby perl;do $l -e "print \"\x3a\x2d\x29\x0a\"";done' ===============================================================================