Example

Create HTML Code

Author: Carl Sassenrath
Return to REBOL Cookbook

Here is a very handy way to generate HTML code that we use a lot at REBOL Technologies. It is quick to write and quick to run.

First, copy these two lines:


    html: make string! 10000
    emit: func [code] [repend html code]

Now, you can write HTML code easily within REBOL. You can write either a single tag, a string, a block, or any other value.


    emit <TABLE>

    emit "This is a string"

    emit [
        <HTML><BODY>
        "The time is: " now
        </BODY></HTML>
    ]

The result is found in the HTML variable. To see it:


    print html

You will notice that it contains no extra spaces or line breaks that might mess up your HTML formatting.

To save the result to an HTML file:


    write %example.html html

Here is an actual example from one of our web scripts that generates an HTML table:


    emit [
        <H2> "REBOL Cookbook Requests" </H2>
        <TABLE BORDER="1" CELLSPACING="0" CELLPADDING="4">
    ]
    foreach item data [
        emit [
            <TR><TD WIDTH="75%"> item/3 </TD><TD> item/2 </TD></TR>
            newline
        ]
    ]
    emit </TABLE>

The most important thing to notice is that normal text must be put in quotes (otherwise REBOL will think it's code and try to evaluate it.)

To use this method to create multiple HTML files, you can clear the output string with the line:


    clear html

then, start processing the next file.


2006 REBOL Technologies REBOL.com REBOL.net