输出ob_get_clean()的最佳方法

时间:2016-01-22 16:13:54

标签: php oop model-view-controller

我有一个类加载模板,然后替换视图中的某些变量。我的问题是,哪个是渲染输出的最佳方法?我现在只使用echo,但这是将它输出到浏览器的正确方法吗?

这是我的模板类:

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;
    }

}

0 个答案:

没有答案