<?php

include_once SERVER_ROOT . '/library/viewtpl/extSmarty.php';
include_once SERVER_ROOT . '/library/viewtpl/iDisplay.php';

class DisplaySmarty implements iDisplay {
    private $smarty;
    private $template;

    public function __construct($template, $tpl_dir='') {
        // 私有路径传递
        $this->smarty = new extSmarty($tpl_dir);
        $this->template = $template;
    }

    public function setView($view) {
        $this->view = $view;
    }

    public function execute() {
        if(is_array($this->view)) {
            foreach($this->view as $key => $value ) {
                $this->smarty->assign($key, $value);
            }
        }
        $this->smarty->display($this->template);
    }

    public function save($html_path){
        if(is_array($this->view)) {
            foreach($this->view as $key => $value ) {
                $this->smarty->assign($key, $value);
            }
        }

        if(!file_exists(dirname($html_path))) mkdir(dirname($html_path), 0755, true);
        file_put_contents($html_path, $this->smarty->fetch($this->template));
    }
}