Views and Templates

To finally show something in the browser, the view must be rendered.

class Manual extends Controller{
    public static function views(){
        //renders the file in views/Manual/views.php
        self::render();
    }
}

The render function should only be called from a static controller function. It is checked if there is a folder with the same name as the calling controller in the views folder, and if there is a php file with the same name as the calling function.

The render call should always be the end of our request. Everything is done, data is collected and we flush it our to the browser.

Templates

Assuming the view is found, it is basicly just included in the index.php.

As it's not that productive if we need to write our headers etc. in every view, we can write Templates.

A template is a php file, located in the template folder. The core-template named index.php, contains all the code in it, we need in every view.

In every template you need to specify where the content should be inserted. This is done by a small call which can be placed everywhere in the template.

//templates/index.php
<div id="content">
<?php $template->_yield(); ?>
</div>

Now we only need to tell our view in which template it should be rendered. For that we have 2 diffrent ways:

1. We tell it every single view

//views/myController/myView.php
//must be on top of the file
<?php PLAIN_PHP\Template::extend("index") ?>

Sidenote: The template name is the same as the filename.

2. We can tell our controller, that every render() call should extend always the same template. Best place for this is the always function

//controllers/App.php
public static function always(){
    self::extendFromTemplate("index");
}

Of course, you can use the extendFromTemplate function in every controller function before the render call, but this would basicaly be the same like Template::extend in every single view.

Templates can extend other templates, but we need to finish an extending template explicitly

    //file: templates/subtemplate.php
    <?php PLAIN_PHP\Template::extend("index"); ?>
    <div style="background: red;">
        <?php $template->_yield(); ?>
    </div>
    <?php PLAIN_PHP\Template::_finish(); ?>

render

As shown above, we can render a view.

In order to send data to the view of a controller, an associative array can be passed. The keys of the array are expanded to the variables in the view

public static function welcome(){
    self::render(array(
        "name" => "Ralf",
        "gender" => "Frau"
    ));
}
//welcome.php
echo "Herzlich Willkommen $gender $name";
//wird ausgegeben als
Herzlich Willkommen, Frau Ralf 

renderPartial

The renderPartial function is used basically in the same way as the render function, with the difference that the view is included in the exact point where the function was called. For example, again the side menu:

//index.php
if(Manual::isActive()){
    //the side menu is rendere right here
    Manual::sideMenu();
}

//Manual.php Controller
public static function sideMenu(){
    self::renderPartial();
}

Like in the render function, data can be transferred via an associative array.

If you want to load a partial view via AJAX, the second argument (\$ajax) should be set to true, to output the plain html and cancel the script execution. (-> ajax - PHP)