Changeset 409

Show
Ignore:
Timestamp:
08/26/07 13:44:14 (12 months ago)
Author:
Shadowhand
Message:

Fixing View to allow $this to be used in views (and refer to the controller), and enabling view-in-view loading to work. Also updated the Welcome controller to demonstrate how it's done.

Location:
branches/devel
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • branches/devel/application/controllers/welcome.php

    r405 r409  
    11<?php 
    22 
    3 class Welcome extends Controller { 
     3class Welcome_Controller extends Controller { 
    44 
    55    function index() 
    66    { 
    7         $this->load->view('welcome')->set('message', 'testing')->render(TRUE); 
    8         print "hi"; 
     7        print new View('welcome', array('message' => 'testing calls back to the controller')); 
     8         
     9        // $db = new Database(); 
     10        // print_r ($db); 
    911    } 
    1012 
    11     function _index() 
     13    function say_hello() 
    1214    { 
    13         $this->load->helper('url'); 
    14         print "<p>Welcome to ".url::site('welcome')."!</p>\n"; 
    15          
    16         $this->load->model('users'); 
    17         print "<p>Model test: ".$this->users->hi()."!</p>\n"; 
    18  
    19         print "<p>Rendered in {execution_time} seconds</p>\n"; 
     15        return '<strong>OMG HI!!!!1</strong>'; 
    2016    } 
    2117 
    2218} 
    23  
    24 ?> 
  • branches/devel/system/libraries/Controller.php

    r397 r409  
    22 
    33class Controller_Core extends Kohana { 
    4      
     4 
    55    public function __construct() 
    66    { 
     
    1010        // Loader should always be available 
    1111        $this->load = new Loader(); 
    12          
     12 
    1313        // URI should always be available 
    1414        $this->uri = new URI(); 
    1515    } 
    1616 
     17    public function kohana_include_view($kohana_view_filename, $kohana_input_data) 
     18    { 
     19        // Buffering on 
     20        ob_start(); 
     21 
     22        // Import the input variables to local namespace 
     23        extract($kohana_input_data, EXTR_SKIP); 
     24 
     25        // Views are straight HTML pages with embedded PHP, so importing them 
     26        // this way insures that $this can be accessed as if the user was in 
     27        // the controller, which gives the easiest access to libraries in views 
     28        include $kohana_view_filename; 
     29 
     30        // Fetch the HTML output 
     31        $kohana_view_output = ob_get_contents(); 
     32 
     33        // Flush the buffer 
     34        ob_end_clean(); 
     35 
     36        // Return the view, yay! 
     37        return $kohana_view_output; 
     38    } 
     39 
    1740} // End Controller Class 
  • branches/devel/system/libraries/View.php

    r405 r409  
    8282    } 
    8383 
    84     public function render($kohana_display_output = FALSE, $kohana_render_function = FALSE) 
     84    public function render($print = FALSE, $callback = FALSE) 
    8585    { 
    86         ob_start(); 
    87         extract($this->data); 
    88         include $this->kohana_view_filename; 
    89         $kohana_view_output = ob_get_contents(); 
    90         ob_end_clean(); 
     86        $output = Kohana::instance()->kohana_include_view($this->kohana_view_filename, $this->data); 
    9187 
    92         if ($kohana_render_function != FALSE AND function_exists($kohana_render_function)) 
     88        if ($callback != FALSE) 
    9389        { 
    94             $kohana_view_output = $kohana_render_function($kohana_view_output); 
     90            $output = Kohana::callback($callback, $output); 
    9591        } 
    9692 
    97         if ($kohana_display_output == TRUE) 
     93        if ($print == TRUE) 
    9894        { 
    99             print $kohana_view_output; 
     95            print $output; 
    10096        } 
    10197        else 
    10298        { 
    103             return $kohana_view_output; 
     99            return $output; 
    104100        } 
    105101    } 
    106102 
    107 } 
    108  
    109 class NOT_Core_View { 
    110  
    111     var $load     = ''; 
    112     var $data     = array(); 
    113     var $template = 'template'; 
    114  
    115     /** 
    116      * Constructor 
    117      */ 
    118     function Core_View() 
    119     { 
    120         $CORE =& get_instance(); 
    121         $this->load =& $CORE->load; 
    122     } 
    123  
    124     /** 
    125      * Get Variable 
    126      * 
    127      * @access  public 
    128      * @return  string 
    129      */ 
    130     function get($key) 
    131     { 
    132         return (isset($this->data[$key]) ? $this->data[$key] : ''); 
    133     } 
    134  
    135     /** 
    136      * Set Variable(s) 
    137      * 
    138      * @access  public 
    139      * @param   mixed 
    140      * @param   mixed 
    141      * @return  void 
    142      */ 
    143     function set($key, $data = FALSE) 
    144     { 
    145         if (is_array($key)) 
    146         { 
    147             $this->data = array_merge($this->data, $key); 
    148         } 
    149         else 
    150         { 
    151             $this->data[$key] = $data; 
    152         } 
    153     } 
    154  
    155     /** 
    156      * Add to a Variable(s) 
    157      * 
    158      * @access  public 
    159      * @param   string 
    160      * @param   string 
    161      * @return  void 
    162      */ 
    163     function add($key, $data) 
    164     { 
    165         if (! is_string($key) OR ! is_string($data)) 
    166             return; 
    167  
    168         if (isset($this->data[$key])) 
    169         { 
    170             $this->data[$key] .= $data; 
    171         } 
    172         else 
    173         { 
    174             $this->set($key, $data); 
    175         } 
    176     } 
    177  
    178     /** 
    179      * Delete Variables 
    180      * 
    181      * @access  public 
    182      * @param   string 
    183      * @return  void 
    184      */ 
    185     function del($key) 
    186     { 
    187         if (isset($this->data[$key])) 
    188         { 
    189             unset($this->data[$key]); 
    190         } 
    191     } 
    192  
    193     /** 
    194      * Load a View 
    195      * 
    196      * Loads a template for inclusion into the template, or displays the template 
    197      * 
    198      * EXAMPLES: 
    199      * Load the "blog_content" view into the "body" variable: 
    200      *   load('blog_content', 'body') 
    201      * Load the "blog_content" view into the "blog_content" variable: 
    202      *   load('blog_content', TRUE) 
    203      * Load the currently set template and return it as a string: 
    204      *   load(TRUE) 
    205      * Load the current template and display it: 
    206      *   load() 
    207      * 
    208      * @access  public 
    209      * @param   string 
    210      * @param   string 
    211      * @return  void 
    212      */ 
    213     function load($view = FALSE, $partial = FALSE) 
    214     { 
    215         if ($view == FALSE) 
    216         { 
    217             $view = $this->template; 
    218         } 
    219         elseif ($view === TRUE) 
    220         { 
    221             return $this->load->view($this->template, $this->data, TRUE); 
    222         } 
    223  
    224         if ($partial != FALSE) 
    225         { 
    226             $key = ($partial === TRUE) ? $view : $partial; 
    227             $this->data[$key] = $this->load->view($view, $this->data, TRUE); 
    228  
    229             return $this->data[$key]; 
    230         } 
    231         else 
    232         { 
    233             $this->load->view($view, $this->data); 
    234         } 
    235     } 
    236103} // End View Class