PHPackages                             asgard/templating - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Templating &amp; Views](/categories/templating)
4. /
5. asgard/templating

ActiveLibrary[Templating &amp; Views](/categories/templating)

asgard/templating
=================

v0.3.1(10y ago)01.7k1MITPHPPHP &gt;=5.5.9

Since Sep 9Pushed 8y agoCompare

[ Source](https://github.com/asgardphp/templating)[ Packagist](https://packagist.org/packages/asgard/templating)[ RSS](/packages/asgard-templating/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (3)DependenciesVersions (5)Used By (1)

\#Templating

[![Build Status](https://camo.githubusercontent.com/06f95e5bf25f553c30133fa3d459d7eb0e161de437bc24b15d1134a425bd2515/68747470733a2f2f7472617669732d63692e6f72672f6173676172647068702f74656d706c6174696e672e7376673f6272616e63683d6d6173746572)](https://travis-ci.org/asgardphp/templating)

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.

- [Installation](#installation)
- [Interfaces](#interfaces)
- [PHP Template](#php)
- [Viewable](#viewable)

\##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.**

```
composer require asgard/templating 0.*

```

\##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=[]);

```

\##PHP Template

The PHPTemplate class implements the TemplateInterface.

Create a new template:

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

```

Set a template

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

```

Get the template:

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

```

Set parameters:

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

```

Get parameters:

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

```

Render the template:

```
$template->render();

```

Render a specific template with parameters:

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

```

Statically render a template:

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

```

\##Viewable

The Viewable trait provides the methods so that a class can easily be rendered with templates.

\###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.

```
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-&gt;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.

For example:

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

#template.php
echo ''.$title.'';

```

Will return:

```
Hello!

```

You can help the viewable object solves the template file with:

```
$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](http://github.com/asgardphp/asgard) repository.

### License

[](#license)

The Asgard framework is open-sourced software licensed under the [MIT license](http://opensource.org/licenses/MIT)

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity15

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity51

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 97.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~204 days

Total

4

Last Release

3657d ago

PHP version history (2 changes)v0.1.0PHP &gt;=5.4.0

v0.3.0PHP &gt;=5.5.9

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/352850?v=4)[leyou](/maintainers/leyou)[@leyou](https://github.com/leyou)

---

Top Contributors

[![h0gar](https://avatars.githubusercontent.com/u/439138?v=4)](https://github.com/h0gar "h0gar (42 commits)")[![PeeHaa](https://avatars.githubusercontent.com/u/1330296?v=4)](https://github.com/PeeHaa "PeeHaa (1 commits)")

### Embed Badge

![Health badge](/badges/asgard-templating/health.svg)

```
[![Health](https://phpackages.com/badges/asgard-templating/health.svg)](https://phpackages.com/packages/asgard-templating)
```

###  Alternatives

[mustache/mustache

A Mustache implementation in PHP.

3.3k44.6M291](/packages/mustache-mustache)[roots/acorn

Framework for Roots WordPress projects built with Laravel components.

9682.1M97](/packages/roots-acorn)[whitecube/nova-flexible-content

Flexible Content &amp; Repeater Fields for Laravel Nova.

8053.0M25](/packages/whitecube-nova-flexible-content)[mopa/bootstrap-bundle

Easy integration of twitters bootstrap into symfony2

7042.9M33](/packages/mopa-bootstrap-bundle)[limenius/react-bundle

Client and Server-side react rendering in a Symfony Bundle

3871.2M](/packages/limenius-react-bundle)[nicmart/string-template

StringTemplate is a very simple string template engine for php. I've written it to have a thing like sprintf, but with named and nested substutions.

2101.7M30](/packages/nicmart-string-template)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
