Source

Ray / Ray / View / File.php

<?php

class Ray_View_File implements Ray_View{
    
    private $config;
    
    private $options = array(
        'ext' => '.php',
        'layout' => 'default',
        'actionDir' => 'action',
        'action' => '404',
        'params' => array()
    );
    
    public function __construct(array $response, $config){
        
        $this->config = $config;
        
        $this->options = array_merge($this->options, $response);
        
    }
    
    private function view($file, $params=array()){
        $view = new Ray_View_File_View($file, $params, $this->config, $this->options);
        return $view->render();
    }
    
    public function render(){
        
        $layout = $this->config['templates'] . DS . $this->options['layout'] . $this->options['ext'];
        
        $action = $this->config['templates'] . DS . $this->options['actionDir'] . DS . $this->options['action'] . $this->options['ext'];
        
        if(file_exists($layout)){
            
            if(file_exists($action)){
                
                $params = $this->options['params'];
                
                $params['action'] = $this->view($action, $params);
                
                return $this->view($layout, $params);
            }else{
                throw new Ray_Exception_View_File('Action template file not found. Path: '. $action);
            }
            
        }else{
            throw new Ray_Exception_View_File('Template file not found. Path: ' . $layout);
        }
        
        return;
    }
    
}