[REBOL] Re: Looking For Help
From: joel:neely:fedex at: 29-Jan-2003 7:32
Hi, Jim,
No "should", just lots of possibilities... ;-)
Jim Richards wrote:
> Content-Type: Text/Plain;
> charset="iso-8859-1"
> Content-Transfer-Encoding: quoted-printable
>
> Hi All,
>
> I am looking for some help on a simple problem I am having.
>
> I am trying to read in an html template file and do variable
> substitution.
> In what format should the template file be in for this to be
> accomplished? I have tried several different methods but I
> cannot get the variables to print.
>
Here's one possibility:
8<---- begin "html-template.r" ----
REBOL []
html-template: make object! [
var-name: none
var-rule: [{variable name="} copy var-name to {"} to end]
template: none
load-template: func [f [file! string!]] [
template: load/markup to-file f
]
fill-template: func [data [block!] /local result] [
result: copy ""
foreach item template [
append result either all [
tag? item parse item var-rule
][
any [select data var-name ""]
][
item
]
]
result
]
process: func [f [file! string!] vars [block!]] [
load-template f
fill-template vars
]
]
8<---- end "html-template.r" ----
8<---- begin "sample.html" ----
<html>
<head><title><variable name="title"/></title></head>
<body>
<h1><variable name="title"/></h1>
<p><variable name="para1"/></p>
<p><variable name="para2"/></p>
</body>
</html>
8<---- end "sample.html" ----
... which behaves as follows ...
>> do %html-template.r
Script: "Untitled" (none)
>> print html-template/process %sample.html [
[ "title" "Testing 1, 2, 3"
[ "para1" "This is a test."
[ "para2" "Hello, world!"
[ ]
<html>
<head><title>Testing 1, 2, 3</title></head>
<body>
<h1>Testing 1, 2, 3</h1>
<p>This is a test.</p>
<p>Hello, world!</p>
</body>
</html>
>>
Of course, you can build the name/value block in whatever fashion
is appropriate for your application, and then call the PROCESS
method on HTML-TEMPLATE once you've built all of your content
pieces.
HTH!
-jn-