Asp Forum
Home
|
Login
|
Register
|
Search
Forums
>
comp.lang.ruby
Unnamed::Template
John W. Long
1/22/2005 4:56:00 AM
I've been working on a yet-to-be-named templating system similar to in
fashion to textpattern's (
http://textp...
).
--
#Currently you can define a template:
template = <<-TEMPLATE
<jwl:cart>
<ul>
<jwl:item limit="5">
<li><jwl:name /> <jwl:price /></li>
</txp:item>
</ul>
Total: <jwl:total />
</jwl:cart>
TEMPLATE
#Define a Context class:
class CartContext < Template::Context
def initalize
@cart = Shopping::Cart.new
@item = nil
end
def cart(attr)
yield
end
def item(attr)
result = ""
@cart.items.each_with_index do |item, index|
@item = item
result << yield
break unless attr["limit"] && (index < attr["limit"](
end
result
end
def name(attr)
@item.name if @item
end
def price(attr)
@item.price if @item
end
def total(attr)
@cart.total
end
end
#Then merge context with template for the expected results:
parser = Template::Parser.new( :pre => "jwl", :context => CartContext )
puts parser.parse(template)
--
I would like to work out a way to force tags to comply to a certain
hierarchy. In this instance, you shouldn't be able to use the "item" tag
out side of a "cart", or "name" or "price" out side of "item".
My current thought is to use a couple of class methods to make this work:
--
my_context = Context.new do
tag "cart" do
tag "item" do
tag "name"
def name(attr)
@item.name if @item
end
tag "price"
def price(attr)
@item.price if @item
end
end
def item(attr)
result = ""
@cart.items.each_with_index do |item, index|
@item = item
result << yield
break unless index < attr["limit"]
end
result
end
end
def cart(attr)
yeild
end
end
--
The flaw of this implementation is that there is a fair amount of
duplication. Does anybody have a better idea for a pseudo language for
defining tag heirarchy?
You can find a version of the project attached to this message:
http://rubyforge.org/pipermail/chicagogroup-members-list/2005-January/0...
--
John Long
http://wiseheart...
Servizio di avviso nuovi messaggi
Ricevi direttamente nella tua mail i nuovi messaggi per
Unnamed::Template
Inserendo la tua e-mail nella casella sotto, riceverai un avviso tramite posta elettronica ogni volta che il motore di ricerca troverà un nuovo messaggio per te
Il servizio è completamente GRATUITO!
x
Login to ForumsZone
Login with Google
Login with E-Mail & Password