anax/view

Anax View module, collect and render views.


README

Latest Stable Version Join the chat at https://gitter.im/mosbth/anax

Build Status CircleCI

Build Status Scrutinizer Code Quality Code Coverage

Maintainability Codacy Badge

Anax View module to supply a structure of views to a Anax website.

About views, terminology and structure

The views, also known as template files, are pure PHP files and their purpose is to render content onto a html structure to create a web page.

It works like this:

  1. The content of the webpage is gathered as a collection of views.
  2. The view collection is created in the router callbacks and controllers.
  3. A view is a combination of variables (content), commonly known as $data, which is supplied to a template file, which is executed and renders the data onto a html structure.
  4. Each view is a small part of html and content and rendered onto the web page.

You can see a sample of views in the directory view/anax/v2.

Views are rendered in a layout

The views are rendered in an orderd fashion by a structured layout. This layout is in itself a view (template file and data), it is just a view which renders more views.

You can see a sample of an layout view in view/anax/v2/layout.

The layout views you above have organised each part of the web page as regions. Each view is rendered in a region.

This code shows how a general region is setup in the layout file, and how it renders the views belonging to this region.

<!-- flash -->
<?php if (regionHasContent("flash")) : ?>
<div class="outer-wrap outer-wrap-flash">
    <div class="inner-wrap inner-wrap-flash">
        <div class="row">
            <div class="region-flash">
                <?php renderRegion("flash") ?>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

You can optionally add a region, and a sort value, when you add the views to the collection. This is what decides where (region), and in what order the view is rendered.

A template file for the view

A template file is an ordinary PHP-file. Here is the template file anax/v2/article/default.

<?php

namespace Anax\View;

/**
 * Render content within an article.
 */

// Show incoming variables and view helper functions
//echo showEnvironment(get_defined_vars(), get_defined_functions());

// Prepare classes
$classes[] = "article";
if (isset($class)) {
    $classes[] = $class;
}


?><article <?= classList($classes) ?>>
<?= $content ?>
</article>

The namespace makes it possible to access a list of built in view helper functions, for example helper functions to escape output or create urls into the framework or assets.

namespace Anax\View;

The commented section can be uncommented for debugging what helper functions and what variables that are available.

// Show incoming variables and view helper functions
//echo showEnvironment(get_defined_vars(), get_defined_functions());

The general idea is then to further prepare content through incoming variables.

// Prepare classes
$classes[] = "article";
if (isset($class)) {
    $classes[] = $class;
}

Then finally render the output.

<article <?= classList($classes) ?>>
<?= $content ?>
</article>

That is the general procedures of a template file.

View helpers

The file View/ViewHelperFunctions holds the helper functions that eases work in the template files.

Here are some essential helpers.

Function Details
asset($url) Create a link to a static asset (css, img, php-file, pdf).
e($str) A shortcut for `htmlentities().
url($url) Create an url within the framework, for example "" (home route) or "doc/about".
redirect($url) Redirect to another url (within the framework), for example "game/init".

Review the helper files for details.

Dumb views or fat views

A view can be "dumb" and only recive input from the framework, and render it with the html code.

A view can also be "fat", or perhaps not so "smart", and make calls back into the framework.

The general recomendation is to have dumb views and not make calls back into the framwork.

Type of views

A view can generally render any type of result and is not limited to html only.

License

This software carries a MIT license. See LICENSE.txt for details.

 .  
..:  Copyright (c) 2013 - 2019 Mikael Roos, mos@dbwebb.se