PHP Classes

File: documentation/en/Views.md

Recommend this page to a friend!
  Classes of Fernando Val   Springy   documentation/en/Views.md   Download  
File: documentation/en/Views.md
Role: Auxiliary data
Content type: text/markdown
Description: Auxiliary data
Class: Springy
Microframework for Web application development
Author: By
Last change: Merge remote-tracking branch 'origin/master' into development
Date: 1 year ago
Size: 1,939 bytes
 

Contents

Class file image Download

Views

About

Views are part of MVC architecture of the framework. They are code responsible for presenting data to end users, usually files containing HTML code and special codes parsed by a template engine.

In this framework you can use two different template engines: Twig or Smarty.

Creating Views

The files with templates for the views, must be placed inside the templates directory. Each template drive uses a different suffix in file name. The files for Twig driver must end with .twig.html and the files for Smarty driver must end with .tpl.html.

Sample of a template to Twig driver.

<!DOCTYPE html>
<html>
    <head>
        <title>My Webpage</title>
    </head>
    <body>
        <ul id="navigation">
        {% for item in navigation %}
            <li><a href="{{ item.href }}">{{ item.caption }}</a></li>
        {% endfor %}
        </ul>

        <h1>My Webpage</h1>
        {{ a_variable }}
    </body>
</html>

Using Templates

Once you have created your template file, you will need a controller to print its content. All you need is create a Springy\\Template object and use its methods.

Sample how to use a template view:

    $tpl = new Template('my-template');
    $tpl->assign('var', 'a value to the variable');
    $tpl->display();

If your controller extends Springy\\Controller class, you can use the protected method createTemplate() to create the template object, like this:

    $this->createTemplate('my-template');
    $this->template->assign('var', 'a value to the variable');
    $this->template->display();