Page 1 of 1

True Theme'in / Templating

Posted: Mon Jun 10, 2002 5:03 pm
by Damien_G
Hi there... howdy... hallo...

I've been really interested in a true templating system for some time now, such things as "headers" and "footers" really arn't my thing... So I've decided to get my head down and code one.

Basically, what I plan to do is create a phraser... "index.php"... which works like your average templating system... e.g. index.php?page=/chat/signup.php etc...

BUT, this one will then check the users MySQL table to fetch the theme, (e.g. chromonoma.tpl - don't ask :P) then, in index.php it'll phrase all the "{ }"'s and then check the MySQL database for the equvilant code...

e.g. For the news posting... Lots of global variables are set, like {news_author}, {news_post_size}, {news_post_date} etc.

Then, the designer can make several themes placing the certain variables where he wishes... and surrounding them in some type of logic... ({while news top 10} etc).

Of course, it'll be compressed output, cached and blah blah blah... I'm just wondering if anyone has any feedback, any past TRUE examples of this type... anyone made anything like this that they haven't yet shared out ? If so, please say if you wish to trade some "notes" (FULL LENGTH CODE :P) :)

Well... I hope I've been annoying enough for my first post... what 'ya recon', 7 outta' 10? 9? WOW... Geeeeeeee THNX! :S


P.S... this is something I'm working on, it's not related DIRECTLY to this, but I dunno... maybe someone will follow my logic....


Code: Select all

<?php 
$fp = fopen("./templates/news/display.tpl", "r");
$contents = fread($fp, filesize("./templates/news/display.tpl"));
fclose($fp);

preg_match_all("/\&#123;.*?\&#125;/s", $contents, $variable_match);

foreach($variable_match&#1111;0] as $variable) &#123;
  $parts = explode(" ", substr($variable, 1, -1));

  if ($parts&#1111;0] == "if") &#123;
    $reply = "<?php if (" . $parts&#1111;1] . " " . $parts&#1111;2] . " " . $parts&#1111;3] . ") &#123; ?>";
  &#125; elseif ($parts&#1111;0] == "elseif") &#123;
    $reply = "<?php &#125; elseif (" . $parts&#1111;1] . " " . $parts&#1111;2] . " " . $parts&#1111;3] . ") &#123; ?>";
  &#125; elseif ($parts&#1111;0] == "else") &#123;
    $reply = "<?php &#125; else &#123; ?>";
  &#125; elseif ($parts&#1111;0] == "/if") &#123;
    $reply = "<?php &#125; ?>";
  &#125; else &#123;
    $reply = "<?php echo ($" . "results&#1111;"" . $parts&#1111;0] . ""]); ?>";
  &#125;

  $contents = str_replace($variable, $reply, $contents);
&#125;

$fp = fopen("./templates/news/display.php", "w");
fputs($fp, $contents);
fclose($fp);
?>

Just a update....

Posted: Tue Jun 11, 2002 9:37 am
by Damien_G
Just updating this... a "litte" progress a.t.m...

Using the code (index.php)...

Code: Select all

<?php
$fp = fopen("./news.tpl", "r");
$contents = fread($fp, filesize("./news.tpl"));
fclose($fp);

preg_match_all("/\&#123;.*?\&#125;/s", $contents, $variable_match);

foreach($variable_match&#1111;0] as $variable) &#123;
  $parts = explode(" ", substr($variable, 1, -1));

  if ($parts&#1111;0] == "while") &#123;
    $reply = "<?php while(" . $parts&#1111;1] . ") &#123; ?>";
  &#125; elseif ($parts&#1111;0] == "/while") &#123;
    $reply = "<?php &#125; ?>";
  &#125; else &#123;
    if ($parts&#1111;1] == "link") &#123;
      $reply = "<a href="<?php echo ($" . $parts&#1111;2] . "); ?>">Linkkkkkkkkkkkkk</a>";
    &#125; else &#123;
      $reply = "<?php echo ($" . $parts&#1111;0] . "_results&#1111;"" . $parts&#1111;1] . ""]); ?>";
    &#125;
  &#125;

  $contents = str_replace($variable, $reply, $contents);
&#125;

echo ($contents);
?>
I've transformed the template file (news.tpl)... from this...

Code: Select all

&#123;while mysql_fetch_array($results)&#125;
&#123;news date&#125;
&#123;news username&#125;
&#123;news realname&#125;
&#123;news category&#125;
&#123;news id&#125;

&#123;news bytes&#125;
&#123;news words&#125;
&#123;news sentences&#125;
&#123;news paragraphs&#125;

&#123;news bytesleft&#125;
&#123;news wordsleft&#125;
&#123;news sentencesleft&#125;
&#123;news paragraphsleft&#125;

&#123;news comments&#125;
&#123;news views&#125;

&#123;news link print&#125;
&#123;news link readmore&#125;
&#123;news link email&#125;
&#123;/while&#125;
to this...

Code: Select all

<?php while(mysql_fetch_array($results)) &#123; ?>
<?php echo ($news_results&#1111;"date"]); ?>
<?php echo ($news_results&#1111;"username"]); ?>
<?php echo ($news_results&#1111;"realname"]); ?>
<?php echo ($news_results&#1111;"category"]); ?>
<?php echo ($news_results&#1111;"id"]); ?>

<?php echo ($news_results&#1111;"bytes"]); ?>
<?php echo ($news_results&#1111;"words"]); ?>
<?php echo ($news_results&#1111;"sentences"]); ?>
<?php echo ($news_results&#1111;"paragraphs"]); ?>

<?php echo ($news_results&#1111;"bytesleft"]); ?>
<?php echo ($news_results&#1111;"wordsleft"]); ?>
<?php echo ($news_results&#1111;"sentencesleft"]); ?>
<?php echo ($news_results&#1111;"paragraphsleft"]); ?>

<?php echo ($news_results&#1111;"comments"]); ?>
<?php echo ($news_results&#1111;"views"]); ?>

<a href="<?php echo ($print); ?>">Linkkkkkkkkkkkkk</a>
<a href="<?php echo ($readmore); ?>">Linkkkkkkkkkkkkk</a>
<a href="<?php echo ($email); ?>">Linkkkkkkkkkkkkk</a>
<?php &#125; ?>
Just thought i'd update you people who don't seem to like this post ;)

-Damien

P.S... a more pratical example...

(updated too... :P)...

[News.tpl]

Code: Select all

&#123;for i=0 i<5 i++&#125;
<table width="60%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#000000">
  <tr> 
    <th bgcolor="#999999">&nbsp;&#123;news subject&#125;</th>
  </tr>
  <tr>
    <td bgcolor="#CCCCCC"><table width="100%" border="0" cellspacing="0" cellpadding="0">
        <tr> 
          <td>&nbsp;&#123;news body&#125;</td>
        </tr>
        <tr>
          <td>&nbsp;&#123;news link print&#125; - &#123;news link readmore&#125;</td>
        </tr>
      </table></td>
  </tr>
</table>
<br>
&#123;/for&#125;
The above shows logic etc, the for while loop is for benchmarking/variable testing!

[Index.php]

Code: Select all

<?php
$start_time = explode(" ", microtime());
$start_time = $start_time&#1111;1] + $start_time&#1111;0];

/////////////////////////
$news_results = array();
/////////////////////////
$news_results&#1111;"subject"] = "Subject" . rand(1, 100);
$news_results&#1111;"body"] = "Body" . rand(1, 100);
$print = "/print.php?id=" . rand(1, 100);
$readmore = "/coments.php?id=" . rand(1, 100);
/////////////////////////

$fp = fopen("./news.tpl", "r");
$contents = fread($fp, filesize("./news.tpl"));
fclose($fp);

preg_match_all("/\&#123;.*?\&#125;/s", $contents, $variable_match);

foreach($variable_match&#1111;0] as $variable) &#123;
  $parts = explode(" ", substr($variable, 1, -1));

  if ($parts&#1111;0] == "while") &#123;
    $reply = "<?php while(" . $parts&#1111;1] . ") &#123; ?>";
  &#125; elseif ($parts&#1111;0] == "/while") &#123;
    $reply = "<?php &#125; ?>";
  &#125; elseif ($parts&#1111;0] == "for") &#123;
    $reply = "<?php for($" . $parts&#1111;1] . "; $" . $parts&#1111;2] . "; $" . $parts&#1111;3] . ") &#123; ?>";
  &#125; elseif ($parts&#1111;0] == "/for") &#123;
    $reply = "<?php &#125; ?>";
  &#125; else &#123;
    if ($parts&#1111;1] == "link") &#123;
      $reply = "<a href="<?php echo ($" . $parts&#1111;2] . "); ?>">Link</a>";
    &#125; else &#123;
      $reply = "<?php echo ($" . $parts&#1111;0] . "_results&#1111;"" . $parts&#1111;1] . ""]); ?>";
    &#125;
  &#125;

  $contents = str_replace($variable, $reply, $contents);
&#125;

eval ("?>" . $contents);

$end_time = explode(" ", microtime());
$end_time = $end_time&#1111;1] + $end_time&#1111;0];
$total_time = ($end_time - $start_time); 
echo ("<br>Exe time: <b>" . $total_time . "</b>");
?>