How To Dynamic Web Rendering?

Adam D. Ruppe destructionator at gmail.com
Sun May 15 08:36:48 PDT 2011


Alexander wrote:
> XML templates look horrible - but this is just a question of taste

But Wordpress' code *is* an xml template, just with other crap mixed
in.

One of the downsides of me writing my document in notepad is adding
code examples in a pain. I didn't want to manually write <div>
over and over again, so I just sidestepped it.

But, here's a template from my D demo:
http://arsdnet.net/apidemo-document.html

It's standard HTML, nothing more. The same stuff you'd write in
an ugly PHP file, but without the ugly PHP.

The main difference is I didn't have to repeat myself over and
over. Didn't have to separate the header, the footer, and the
content (though, I could have. Most my real sites actually do
that.)

I didn't have to write

        <div id="page-content">
            <?php output_page_content(); ?>
        </div>


And a corresponding function in the code. The id on the div
is everything it needed to know.


Another one:

        <table class="striped" id="our-data">
        </table>


That's all that's needed to be said in the template. The program
uses the ID to fill in the data, and the class is caught in a
generic post-processor to add class="odd" to every other row.
(that is like unobstructive javascript for progressive enhancement,
but done server side instead of on page load.)

Such with PHP's Smarty templates would look like this:
       <table>
       {foreach from=$rows item=row}
       <tr class="{cycle values="odd,even"}">
          <!-- output data here -->
       </tr>
       {/foreach}
       </table>


Ugh. (and I forgot a closing tag there in my first draft! Would
have broken the site. With the DOM, such things are rare since you
write less xml (ironically), cleaner code, and if you mess up
anyway, you get a clear message on where the missing tag is.)


The D side, btw, looks like so:

auto table = cast(Table) document.getElementById("our-data");
foreach(row; data)
   table.appendRow(row...);


Much easier than the PHP equivalent too - it's all clear. And all
properly closed and entity encoded with zero effort.


More information about the Digitalmars-d-learn mailing list