2017 © Pedro Peláez
 

library templating

image

asgard/templating

  • Saturday, August 19, 2017
  • by leyou
  • Repository
  • 1 Watchers
  • 0 Stars
  • 1,600 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Templating

Build Status, (*1)

Templating is a simple package which provides interfaces to build your own templating system. It also provides a PHP templating system and the Viewable class., (*2)

, (*3)

Installation

If you are working on an Asgard project you don't need to install this library as it is already part of the standard libraries., (*4)

composer require asgard/templating 0.*

, (*5)

Interfaces

TemplateEngineInterface

public function templateExists($template); #check that a template exists
public function getTemplateFile($template); #return the file corresponding to the template name
public function createTemplate(); #return a new instance of the template class (implementing TemplateInterface)

TemplateInterface

public function getEngine();
public function setEngine(TemplateEngineInterface $engine);
public function setTemplate($template);
public function getTemplate();
public function setParams(array $params=[]);
public function getParams();
public function render($template=null, array $params=[]);
public static function renderFile($file, array $params=[]);

, (*6)

PHP Template

The PHPTemplate class implements the TemplateInterface., (*7)

Create a new template:, (*8)

$template = new Asgard\Templating\PHPTemplate('template.php', ['title'=>'Hello!']);

Set a template, (*9)

$template->setTemplate('template2.php');

Get the template:, (*10)

$template->getTemplate(); #template2.php

Set parameters:, (*11)

$template->setParams(['title'=>'Hello!']);

Get parameters:, (*12)

$template->getParams(); #['title'=>'Hello!']

Render the template:, (*13)

$template->render();

Render a specific template with parameters:, (*14)

$template->render('template.php', ['title'=>'Hello!']);

Statically render a template:, (*15)

Asgard\Templating\PHPTemplate::renderFile('template.php', ['title'=>'Hello!']);

, (*16)

Viewable

The Viewable trait provides the methods so that a class can easily be rendered with templates., (*17)

Usage

class Abc {
    use \Asgard\Templating\Viewable;

    public function test($param1, $param2) {
        return 'test';
    }
}

$abc = new Abc;

$abc->setTemplateEngine($engine);
#the engine will be passed to any template used by the class
#$abc->getTemplateEngine(); to get the engine

$abc->run('test', ['param1', 'param2']);

Rendering

There are many ways a method can render the result., (*18)

public function test() {
    return 'test';
}
#run('test') returns 'test'

public function test() {
    echo 'test';
}
#run('test') returns 'test'

public function test() {
    return new MyTemplate('template.php', [/*..*/]);
}
#run('test') will call ->render() on the template and return the result

public function test() {
    $this->view = new MyTemplate('template.php', [/*..*/]);
}
#run('test') will call ->render() on $this->view and return the result

public function test() {
    $this->view = 'template';
}
#if the object as a TemplateEngine, it will create a template instance and pass 'template.php' to it.
#if not, Viewable will use its own default rendering technique.

Default rendering

When a template name is passed to $this->view, and the object does not have its own TemplateEngine, Viewable will try to solve the template file corresponding to the template name, include it and pass its own variables., (*19)

For example:, (*20)

#Viewable class test method
public function test() {
    $this->title = 'Hello!';
    $this->view = 'template'; #template matches /var/www/project/templates/template.php
}

#template.php
echo '<h1>'.$title.'</h1>';

Will return:, (*21)

<h1>Hello!</h1>

You can help the viewable object solves the template file with:, (*22)

$abc->addTemplatePathSolver(function($obj, $template) {
    $file = '/var/www/project/templates/'.$template.'.php';
    if(file_exists($file))
        return $file;
});

Static rendering

Abc::fragment('tes', [$param1, ...]);

Contributing

Please submit all issues and pull requests to the asgardphp/asgard repository., (*23)

License

The Asgard framework is open-sourced software licensed under the MIT license, (*24)

The Versions

19/08 2017

dev-master

9999999-dev

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

The Development Requires

by Michel Hognerud

13/05 2016

v0.3.1

0.3.1.0

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

by Michel Hognerud

12/05 2016

v0.3.0

0.3.0.0

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

by Michel Hognerud

12/06 2015

v0.2.0

0.2.0.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Michel Hognerud

09/09 2014

v0.1.0

0.1.0.0

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

by Michel Hognerud