1

I have a class which loads the template and then replaces the certain variables within my view. My question is, which is the best way to render the output? I have simply used echo for now, but is this the right way to output it to the browser?

Here is my template class:

class Template
{

    /*
     * Set the file name for our templating engine
     *
     * @type        string
     * @visibility  protected
     */

    protected $file;


    /*
     * All of the output for our rendering
     *
     * @type        string
     * @visibility  protected
     */

    private static $output;


    /*
     * An array for our template variables
     *
     * @type        array
     * @visibility  protected
     */

    protected $variables = array();


    /*
     * Initiate the class and set the file name
     *
     * @param       string  $file
     * @visibility  public
     */

    public function __construct( $file )
    {
        $this->file = $file;
    }


    /*
     * Set variables to replace in our template
     *
     * @param       string $key
     * @param       string $value
     */

    public function set( $key, $value )
    {
        $this->variables[ $key ] = $value;
    }


    /*
     * Create all the standard environment variables
     * for easy templating.
     *
     * @visibility public
     */

    public function setupEnvironment()
    {
        $this->set( 'css', 'app/Skins/Main/css' );
        $this->set( 'img', 'app/Skins/Main/img' );
        $this->set( 'template', 'app/Skins/Main/templates' );
        $this->replaceTag( 'template', 'header', SKINS . '/Main/templates/header.php' );
        $this->replaceTag( 'template', 'footer', SKINS . '/Main/templates/footer.php' );
    }


    /*
     * Used by $instance->buffer() for
     * replacing tags with values
     *
     * @param   string  $tag
     * @param   string  $key
     * @param   string  $value
     *
     * @return  string
     */

    public function replaceTag( $tag, $key, $replace )
    {
        self::$output = str_replace( '@' . $tag . '(\'' . $key . '\')', file_get_contents( $replace ), self::$output );
        self::$output = str_replace( '@' . $tag . '("' . $key . '")', file_get_contents( $replace ), self::$output );

        return self::$output;
    }


    /*
     * Output an error
     *
     * @param       $message
     *
     * @visibility  public static
     */

    public static function error( $message )
    {
        die( "<div style='position: absolute; top: 40%; left: 3%; width: 90%; padding: 10px 20px; background: #0d405a; color: #FFF;'><p><strong>Error: </strong>{$message}</p>" );
    }

    /*
     * Render the content of the view
     *
     * @visibility  public
     */

    public function buffer()
    {
        ob_start();

        require_once( $this->file );

        self::$output = ob_get_clean();

        $this->setupEnvironment();

        foreach( $this->variables as $key => $variable )
        {
            $tag = "{$key}";
            self::$output = str_replace( '{' . $tag . '}', $variable, self::$output );
        }

        return self::$output;
    }

    public static function render()
    {
        echo self::$output;
    }

}
Jarrod
  • 322
  • 1
  • 3
  • 13
  • its really hard to say right or wrong....imo.....nonetheless, heres an post from SO about MVC view, http://stackoverflow.com/questions/16594907/understanding-mvc-views-in-php, maybe it can five you direction – Andrew Jan 22 '16 at 16:26
  • Thanks for the comment @Andrew that's definitely interesting:) – Jarrod Jan 22 '16 at 19:08

0 Answers0