Pages: [1]   Go Down
Send this topic | Print
Author Topic: Adding option capability to my template engine? [RESOLVED]  (Read 388 times)
soren121
I eat lots of those
Advanced Authority Member
*****
Offline Offline

Posts: 1476


I shall stare you down with mah kitteh powahs!


WWW
« on: September 15, 2008, 04:26:05 AM »

Never mind, fixed it.  Duh

I've been trying to figure out how to have options in my template engine like: {language=english} or something like that. The option needs to be turned into a PHP variable for use elsewhere. Here's the template engine and implementation:

Template Engine Class
Quote
class Page
{
  // Start $page variable for easy use
  var $page;
  // Make the template path easier to read and write Wink
  $current_theme_dir = $theme_dir.$current_theme.'/';

  function Page($template) {
    if (file_exists($current_theme_dir.$template.'.template.html'))
      $this->page = join("", file($current_theme_dir.$template.'.template.html'));
    else
      die("Template file $template not found.");
  }

  // File parser function
  function parse($file) {
    ob_start();
   $file = $current_theme_dir.$file.'.template.html';
    include($file);
    $buffer = ob_get_contents();
    ob_end_clean();
    return $buffer;
  }

  // Tag replacer function
  function replace_tags($tags = array()) {
    if (sizeof($tags) > 0) {
      foreach ($tags as $tag => $data) {
        $data = (file_exists($data)) ? $this->parse($data) : $data;
        $this->page = eregi_replace("{" . $tag . "}", $data, $this->page);
      }
   }
   
    else {
      die("No tags designated for replacement.");
   }
  }

  // Function to output the page to the browser
  function output() {
    echo $this->page;
  }
}

Implementation
Quote
// This function compiles and loads themes
function loadTemplate(strtolower(ucwords($input))) {
   // Start up the template compiler!
   require_once('TemplateCompiler.php');
   $page = new Page($input);
   // Replace the tags and send it out!
   loadLanguage($current_language);
   $page->replace_tags(array(
      "title" => "Test"
   ));
   return $output;
}
[/s]
« Last Edit: September 18, 2008, 09:36:50 AM by soren121 » Logged

LightBlog Developer -- Fueled by Linkin Park, Sprite, and LOLCats
Pages: [1]   Go Up
Send this topic | Print
Jump to: