HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

Replacing variables with actual data

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
actualwithreplacingvariablesdata

Problem

I've written a class for loading a template file and replacing the variables with actual data. Please review my code and suggest improvements.

dir = $dir;
    }

    /**
     * To load the specified file from directory
     * @param $file
     */
    function loadFile($file) {
        try {
            if (file_exists ( $this->dir . "/" . $file )) {
                $this->text = file_get_contents ( $this->dir . "/" . $file );
            } else {
                throw new Exception ( $this->dir . "/" . $file . " does not exist" );
            }
        } catch ( Exception $e ) {
            error_log ( $e->getMessage () );
            die ();
        }
    }
    /**
     * To get the text from a file
     * @return text
     */
    function getText() {
        return $this->text;
    }

    /**
     * To replace the variables
     * @param unknown_type $var
     * @param unknown_type $text
     */
    function replace($var, $text) {
        $this->text = str_replace ( "{\$var}", $text, $this->text );
    }
}

?>

Solution

Note: I'm sorry if this is completely irrelevant but I'm not entirely sure what you are trying to accomplish. If I'm way off base please let me know.

When I create a template loader I generally use output buffering with php include. This allows you to run the page as a php file without displaying its content before you are ready. The advantage to "parsing" your php files this way is that you can still run loops and functions.

This is how your loadFile method would look.

dir . "/" . $file )) {

          // start output buffering
          ob_start();

          // create variables from the key/value pairs in the array
          if( is_array($variables) ) {
              extract($variables);
          }

          include_once $this->dir . "/" . $file;

          // store the output
          $this->text = ob_get_clean();

          // $this->text = file_get_contents ( $this->dir . "/" . $file );
      } else {
          throw new Exception ( $this->dir . "/" . $file . " does not exist" );
      }
  } catch ( Exception $e ) {
      error_log ( $e->getMessage () );
      die ();
  }
}

?>


As I said before, I'm not sure what kind of templates you are trying to load, but hopefully this will help.

Code Snippets

<?php

function loadFile($file, array $variables = null) {
  try {
      if (file_exists ( $this->dir . "/" . $file )) {

          // start output buffering
          ob_start();

          // create variables from the key/value pairs in the array
          if( is_array($variables) ) {
              extract($variables);
          }

          include_once $this->dir . "/" . $file;

          // store the output
          $this->text = ob_get_clean();

          // $this->text = file_get_contents ( $this->dir . "/" . $file );
      } else {
          throw new Exception ( $this->dir . "/" . $file . " does not exist" );
      }
  } catch ( Exception $e ) {
      error_log ( $e->getMessage () );
      die ();
  }
}

?>

Context

StackExchange Code Review Q#12211, answer score: 6

Revisions (0)

No revisions yet.