edit

Skytells Framework Views


Overview

Views contain the HTML served by your application and separate your controller / application logic from your presentation logic.

A view is simply a web page, or a page fragment, like a header, footer, sidebar, etc. In fact, views can flexibly be embedded within other views (within other views, etc., etc.) if you need this type of hierarchy.

Views are never called directly, they must be loaded by a controller.

Remember that in an MVC framework, the Controller acts as the traffic cop, so it is responsible for fetching a particular view. If you have not read the Controllers page you should do so before continuing.

Remember

You can work with one of our built-in template engines which provide more functionality inside your views.

Namespace

The View Resolver Engine works under the following namespace :

1
Skytells\UI;

Path

Views are stored in the Application/Resources/Views directory.

A simple view might look something like this:

1
2
3
4
5
6
<!-- View stored in Application/Resources/Views/index.php -->
<html>
    <body>
        <h1>Hello World!</h1>
    </body>
</html>

Creating a View

Using your text editor, create a file called HelloWorld.php, and put this in it:

1
2
3
4
5
6
7
8
<html>
  <head>
    <title>Hello World</title>
  </head>
  <body>
    <h1>Welcome to my View!</h1>
  </body>
</html>

Then save the file in your Application/Resources/Views/ directory.

Now your view has been created with PHP Code access standards inside the view itself,
So you'll be able to write a PHP code inside your view normally.

Loading a View

To load a particular view file you will use the following method:

1
View::render('name');

Where name is the name of your view file.

Note

The .php file extension does not need to be specified unless you use something other than .php.

Now, open the controller file you made earlier called MyController.php or any other controller, and replace the echo statement with the view loading method:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View; // <--- Required namespace.

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
    }

  public function index() {
    View::render('name'); <----- // Replace name with the View name.
  }
}

Namespace Required

Please make sure to use the Viewer namespace on the top of your controller

1
use Skytells\UI\View;

If you visit your site using the URL you did earlier you should see your new view.
The URL was similar to this:

1
example.com/MyController/index

Loading Multiple Views

Skytells will intelligently handle multiple calls to View::Render() from within a controller.
If more than one call happens they will be appended together.
For example, you may wish to have a header view, a menu view, a content view, and a footer view.
That might look something like this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
    }

  public function index() {
    View::render('header'); <----- // Replace name with the View header name.
    View::render('name'); <----- // Replace name with the View name.
    View::render('footer'); <----- // Replace name with the View footer name.
  }
}

In the example above, we are using “dynamically added data”, which you will see below.

Storing Views within Sub-directories

Your view files can also be stored within sub-directories if you prefer that type of organization.
When doing so you will need to include the directory name loading the view.

Example:

1
View::render('directory_name/file_name');

Passing Data to Views

Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading method.
Here is an example using an array:

1
2
3
4
5
6
7
8
<?php
$data = array(
        'title' => 'My Title',
        'heading' => 'My Heading',
        'message' => 'My Message'
);

View::render('name', $data);

And here’s an example using an object:

1
2
3
<?php
$data = new Someclass();
View::render('name', $data);

Here's another example of assigning a multiple dynamic data using assign() method.

1
2
3
4
<?php
$data = new Someclass();
View::assign('data', $data); // <-- Using this.
View::render('name');

Now, We've assigned $data array to a new array inside the view named data

Note

You can give a new name to the assigned data by changing the first paramter of assign() method.

Let’s try it with your controller file. Open it add this code:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
<?php
use Skytells\Core;
use Skytells\Runtime\Console;
use Skytells\UI\View;

Class MyController extends \Controller implements IController  {

  function __construct($Ref = "") {
      parent::__construct();
    }

  public function index() {
    $data = Array("title" => "Hello World!", "name" => "Skytells");
    View::render('name', $data);
  }
}

Now open your view file and change the text to variables that correspond to the array keys in your data:

1
2
3
4
5
6
7
8
<html>
  <head>
    <title><?php echo $data['title'];?></title>
  </head>
  <body>
    <h1>Welcome <?php echo $data['name'];?></h1>
  </body>
</html>

Then load the page at the URL you’ve been using and you should see the variables replaced.

PHP Functions

Of course you can use all php functions inside your view like loops, if ..etc.

Languages Phrases

If you turned on the default language engines, Your Phrases will be assigned automatically And of course you can use them by calling l() function.

Warning

This function only works with built-in micro language engine and it doesn't work with the dynamic translator.

First, Create & configure your language files in Application/Misc/Phrases dir. Example :

1
2
3
4
5
6
7
8
<html>
  <head>
    <title><? echo l('PAGE_TITLE'); ?></title>
  </head>
  <body>
    <h1>Welcome <?php echo $data['name'];?></h1>
  </body>
</html>

Then load the page at the URL you’ve been using and you should see the variables replaced.

Template Engines

Skytells Framework comes with two built-in Template Engines.

  • MicroUI
    • MicroUI is a slim but powerful template engines designed for users who needs fast coding.
  • Oxygen
    • Oxygen Template engine is an implementation of Blade which used by Laravel, Oxygen is the world's most powerful template engine.