Jump to content

can't figure out why recursive replace is not working


mctrivia

Recommended Posts

    if ($template->load($maintemplatefile,$values)) {
       //load every php file in the plugin folder

    //Handle invalid page Requests
    } else {
      echo '404 File Not Found';
      //exit;
    }

    //print page code
    echo $template->html();
  }

 

I call the above code it prints out the template file but it does not recursively replace the <TEMPLATE_LOADTEMPLATE_$1> that is in the file even though it properly parses both the $tag and $value variables going to the $this->replace($tag,$value); line.  Any idea why this does not work?

 

<?php

  class Template {
    var $code='';

    function load($file,$values) {
      if (file_exists($file)) {
        //get template directory
        $dir=substr($file,0,strrpos($file,'/')) . '/';
       

        //get template
        $this->code=file_get_contents($file);

        //check if any recurseve templates to add
        $tags=$this->tags();

        //process all internal template tags
        $newValues=array_slice($values,1);
        foreach ($tags as $tag) {

          //handle inserting new template file
          if (substr($tag,0,13)=='LOADTEMPLATE_') {
            $file=substr($tag,13);
            if ($file[0]=='$') {
              $file=$values[(substr($file,1)-1)];
            }
            $temp=new Template;
            $filename=$dir . $file . '.xhtml';
            if ($temp->load($filename,$newValues)) {
              $value=$temp->html();
            } else {
              $value='File Not Found';
            }
            $this->replace($tag,$value);
          }
        }


        return true;
      } else {
        return false;
      }
    }

    function tags() {
      preg_match_all('/<TEMPLATE_[^<]+?>/', $this->code, $tags);
      $tags=$tags[0];
      foreach ($tags as $key=>$value) {
        $tags[$key]=substr($value,10,-1);
      }
      return array_unique($tags);
    }

    function replace($tag,$value) {
      $this->code = ereg_replace('<TEMPLATE_' . $tag . '>',$value . '',$this->code);
    }

    function replaceSection($tag,$value) {
      $replace='~<TEMPLATE_' . $tag . '>[\s\S]*?</TEMPLATE_' . $tag . '>~';
      $this->code = preg_replace($replace,$value . '',$this->code);
    }

    function compress() {
      $this->code = ereg_replace("\n",'',$this->code);
      $this->code = ereg_replace("  ",' ',$this->code);
      $this->code = ereg_replace("\r",'',$this->code);
    }

    function html() {
      //remove <TEMPLATE_*> & </TEMPLATE_*> tags then return html.
      return preg_replace('/(<\/?)TEMPLATE_[^<]+?>/','',$this->code);
    }
  }

?>

 

 

All code listed is available for use under GNU public license.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.