How To Dynamic Web Rendering?

Adam D. Ruppe destructionator at gmail.com
Sun May 15 08:04:34 PDT 2011


Alexander wrote:
>  Say, somewhere on the page you set a variable, and it's values is
> visible on all other pages.

My simple implementation does that.

The way it works is say you have this file:

===
<?d auto title = "hello"; ?>
<title><?= title ?></title>
===

It scans through and turns the stuff outside <??> into string
literals and wraps the whole thing in a main function. So that
becomes:

/* snip imports */
void main() {
   auto title = "hello";
   write("<title>");
   write(title);
   write ("</title>");
}


When it runs, you get the result you expect coming from PHP. Thank's
to D's nested functions, classes, etc., you can do just about
anything with the language, despite it all being inside one big
main function once generated.


In PHP, pages share data by having one include the other. Even
Wordpress templates ultimately work that way - the main code
includes the template's code after defining a bunch of functions
and variables.

If you did the same here, the variables from before would be
visible in your scope.

   auto title = "hello";
   write("<title>");
   write(title);
   write ("</title>");
// include a similar file that says <?= replace(title, "h", "H"); ?>
   write(replace(title, "h", "H");

And it'd work - the title variable hasn't disappeared.



There is one thing Wordpress does that would *not* work here
though: including a file based on a dynamic variable.

include("file.dph"); // works fine - the string can be pulled out
                     // by the preprocessor and compiled

include(file-from-user); // wouldn't work


You could make it work by providing a pool of available files
though, then picking one of them from the list.


I only wrote that little program as a toy a while ago. Someone
on the newsgroup said it was impossible... I rather like showing
the possible in D instead, even if I find it useless.

But since I do find it pretty useless, I never finished it. Still,
even in it's unfinished state, most of this works already. Sharing
data is easy.


More information about the Digitalmars-d-learn mailing list