2017 © Pedro Peláez
 

library view

raw PHP template renderer that does not escape.

image

tuum/view

raw PHP template renderer that does not escape.

  • Sunday, March 6, 2016
  • by asaokamei
  • Repository
  • 1 Watchers
  • 0 Stars
  • 101 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 25 Versions
  • 0 % Grown

The README.md

View Template

Scrutinizer Code Quality, (*1)

A raw PHP based template with sections, block, and layout, but no escaping functions., (*2)

To escape values displayed in a template, use other packages, such as Tuum/Form., (*3)

Licence

MIT Licence, (*4)

PSR

PSR-1, PSR-2, and PSR-4., (*5)

Getting Started

Installation

composer require "tuum/view: ^1.0"

Sample Code

Constructing the renderer:, (*6)

$view = new Tuum\View\Renderer(
    new Tuum\View\Locator('/path/to/view/')
); // or alternatively,
$view = Tuum\View\Renderer::forge('/path/to/view');

To render a PHP template file,, (*7)

$view->render('my/file-name', [ 
  'some' => 'data' 
]);

The template file at /path/to/view/my/file-name.php may be:, (*8)

<html>
Some=<?= $some; ?>
</html>

The Tuum/View does not escape the value. Please use helpers to escape before displaying the values., (*9)

Using Layout

You can set a default layout in the renderer:, (*10)

$viewer = $viewer->setLayout('layout/layout1');
$viewer->render('file-name', [ 
  'some' => 'data' 
]);

or set layout inside individual template file (hence overwrites the default layout):, (*11)

<?php
$this->setLayout('layout/layout1');
?>
set layout inside this view.

layout file

In a layout file, emit content (from the initial template file) using $this->getContent() method., (*12)

This is layout#1.
= $this->section-getContent(); ?>

End of layout#1.

Using Section

in template file

In a template file, define a section using $this->section->start() and $this->section->saveAs('block'),, (*13)

 section->start(); ?>

this is a block.

section->saveAs('block'); ?>

this is a content.

in layout file

In a layout file, use the defined section as:, (*14)

Block:
= $this->section->get('block'); ?>

Content:
= $this->getContent(); ?>

Done:

You can check if a section is defined:, (*15)

<?php if ($this->section->exists('content')): ?>
    <?= $this->getContent(); ?>
<?php else: ?>
    Welcome Section Test
<?php endif; ?>

replaceBy method

Using replaceBySection in layout file may make it clearer..., (*16)

<?php $this->section->start(); ?>
    Welcome Section Test
<?php $this->section->replaceBy('content'); ?>

replacedBy method will take the output from a section (from start() till replacedBy() method) and checks if the named section exists or not. If the section exists, it outputs the existing section, or outputs the section in the layout., (*17)

disabling a section in layout

In the template file, it is possible to set a section not to be displayed at all by using markNotToRender method., (*18)

In template file:, (*19)

<?php $this->section->markNotToRender('bread'); ?>

In layout file:, (*20)

<?php $this->section->start(); ?>
<ol>
    <li>bread-top</li>
    <?= $this->getSection('bread'); ?>
</ol>
<?php $this->section->renderAs('bread'); ?>

Because the bread section is marked as NotToRender, the entire bread section will not be rendered., (*21)

This also works for replaceBySection as well. So, for the example for replaceBySection('content') will not be displayed at all if marked as NoDisplay., (*22)

Using Block

To include another template, use block method in a template file as:, (*23)

<?= $this->block('block-sub', ['some' =>'value']); ?>

The block-sub is another template file. The template's data is shared with the block template., (*24)

blockAsSection method

Some times, one might want to use a block as a section (well, I do). So, here's an easy way to do., (*25)

<?php $this->blockAsSection('block-file', 'section-name', ['another' => 'one']); ?>

The Versions

06/03 2016

1.x-dev

1.9999999.9999999.9999999-dev

raw PHP template renderer that does not escape.

  Sources   Download

MIT

by Asao

06/03 2016

dev-master

9999999-dev

raw PHP template renderer that does not escape.

  Sources   Download

MIT

by Asao

20/11 2015

1.0.0

1.0.0.0

raw PHP template renderer that does not escape.

  Sources   Download

MIT

by Asao

18/11 2015

1.0.0-RC2

1.0.0.0-RC2

raw PHP template renderer that does not escape.

  Sources   Download

MIT

by Asao

30/10 2015

1.0.0beta1

1.0.0.0-beta1

adapters for various view template components.

  Sources   Download

MIT

by Asao

30/10 2015

1.0.0-RC1

1.0.0.0-RC1

adapters for various view template components.

  Sources   Download

MIT

by Asao

18/06 2015

0.5.1

0.5.1.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

08/05 2015

dev-dev

dev-dev

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

08/05 2015

0.5.0

0.5.0.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

27/04 2015

0.4.0

0.4.0.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

22/04 2015

0.3.3

0.3.3.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

19/04 2015

0.3.2

0.3.2.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

15/04 2015

0.3.1

0.3.1.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

13/04 2015

0.3.0

0.3.0.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

10/04 2015

0.2.2

0.2.2.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

09/04 2015

0.2.1

0.2.1.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

07/04 2015

0.2.0

0.2.0.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

  • tuum/locator 0.*

 

by Asao

27/03 2015

0.1.6

0.1.6.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao

26/03 2015

0.1.5

0.1.5.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao

26/03 2015

0.1.4

0.1.4.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao

04/03 2015

0.1.3

0.1.3.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao

02/03 2015

0.1.2

0.1.2.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao

01/03 2015

0.1.1

0.1.1.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao

10/02 2015

0.1.0

0.1.0.0

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao

09/02 2015

dev-psr7

dev-psr7

adapters for various view template components.

  Sources   Download

MIT

The Requires

 

by Asao