From: Eric Hodel Date: 2002-12-28T17:25:31+09:00 Subject: Re: Another take on RDoc's templates --GV0iVqYguTV4Q9ER Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable Tom Sawyer (transami@transami.net) wrote: > On Saturday 28 December 2002 12:22 am, Eric Hodel wrote: > > I was looking around for a decent template system, and RDoc's fit the > > bill for me, almost. I had some perfectly good objects lying around > > with perfectly good methods on them, and didn't want to go through the > > extra (tedious) step of converting them all to Hash/Array combinations, > > so I did this to (a prior version of) RDoc's template system: >=20 > you can use RDoc for general purpose templating? i thought it was only fo= r=20 > documenting Ruby code. (I checked RDoc web page but didn't see any refere= nce=20 > to this type of use.) Well, no, but I attended RubyConf, and Dave hinted at such a thing in RDoc, so I looked in there, found it, stole it, and hacked it up :) > what do the templates look like in doing this? Using my hacked version, you do something like this: # Usage: # # T1 =3D %{header # !INCLUDE! # footer} # # T2 =3D %{%name% # %state% # letters in name: %name.size%} # # values =3D { "name" =3D> "Dave", "state" =3D> "TX" } # # t =3D TemplatePage.new(T1, T2) # # res =3D '' # t.write_html_on(res, values) # # Which will result in the following # output: # # header # Dave # TX # letters in name: 4 # footer For a fancier template, you could do something like: require 'template' T =3D %{

Object contains these WITH:Object#methods %Array#length% ENDWITH:Object#methods methods:

} res =3D '' TemplatePage.new(T).write_html_on(res, Object.new) puts res > currently i use eRuby. I would have liked to use Amrita, but it seems the= =20 > attribute templating is still considered experimental.=20 eRuby is very slow, as it eval's a tempfile to generate the page. > i'm curious as to the relative performace comparisons between the various= =20 > templating options. anyone have any experience to draw upon for this? Well, anything in pure ruby is going to be slower than a pure C version. How much faster or slower will be dependent upon features offered. RDoc's and mine are pretty much brain-dead simple, and I hacked mine to up the speed (I took out a call to Array#join that really sucked down performance, and eliminated RegExp as much as convenient, in favor of String#[]). --=20 Eric Hodel - drbrain@segment7.net - http://segment7.net All messages signed with fingerprint: FEC2 57F1 D465 EB15 5D6E 7C11 332A 551C 796C 9F04 --GV0iVqYguTV4Q9ER Content-Type: application/pgp-signature Content-Disposition: inline -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.2.0 (FreeBSD) iD8DBQE+DV/5MypVHHlsnwQRAtipAJ97wpjypWMPm1UWdeq40bRbqH9HUACdHf5h lYsKN6ttPIYQo9/KTnq21EU= =CpKR -----END PGP SIGNATURE----- --GV0iVqYguTV4Q9ER--