PHPackages                             swiftly/template - 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. swiftly/template

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

swiftly/template
================

Provides helpful wrappers for working with PHP templates.

026PHP

Since Aug 21Pushed 4mo agoCompare

[ Source](https://github.com/SwiftlyPHP/template)[ Packagist](https://packagist.org/packages/swiftly/template)[ RSS](/packages/swiftly-template/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (2)Used By (0)

Swiftly - Template
==================

[](#swiftly---template)

[![PHP Version](https://camo.githubusercontent.com/3571a86987b3a8b0b0d13c86369a99d5b7b5a1945a7e624bba32d4c3183eab0a/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f7068702d2533453d382e302d626c7565)](https://www.php.net/supported-versions)[![CircleCI](https://camo.githubusercontent.com/91646fe98766f035d0c281b68a0eec4e0eda49108e819dd51365162c65e59cf6/68747470733a2f2f636972636c6563692e636f6d2f67682f53776966746c795048502f74656d706c6174652f747265652f6d61696e2e7376673f7374796c653d736869656c64)](https://circleci.com/gh/SwiftlyPHP/template/tree/main)[![Coverage Status](https://camo.githubusercontent.com/8c212763e5d2d1c19292c3f602c5004c709d3d9a329c8e90bb1f8e7a89e4a4ae/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f53776966746c795048502f74656d706c6174652f62616467652e7376673f6272616e63683d6d61696e)](https://coveralls.io/github/SwiftlyPHP/template?branch=main)

Manage and render PHP templates.

Designed primarily to be used for [SwiftlyPHP](https://github.com/SwiftlyPHP)projects but can also be used on its own, the template component provides a straightforward utility for loading PHP files and capturing any output they generate.

The library also provides several helpers to make working in mixed HTML/PHP contexts easier, including the ability to display JSON, escape HTML character entities and render sub-components.

Installation
------------

[](#installation)

To install the library use [Composer](https://getcomposer.org/):

```
composer require swiftly/template
```

Usage
-----

[](#usage)

### Basic Templates

[](#basic-templates)

Using the template renderer in its most basic form only requires a few steps of setup.

To demonstrate, lets say we have a simple PHP template called `hi.php` that contains the following:

```

Hello world!
```

Normally, including a file like this would cause its content to be sent directly to the user.

To render the content and capture it in a variable we must instantiate a fresh instance of the [Engine](./src/Engine.php) class. The Engine class has only one method `render()`, which we pass the name of the file we wish to load.

For our Engine to first find the templates however, we'll need to construct a [FileFinder](./src/FileFinder.php) which is responsible for locating templates on the filesystem.

```

My name is  and I am  years old.
```

As we can see, this template expects 2 variables: `$name` and `$age` to exist and have values. To pass them to the template we provide an array of values to the `render()` method like so:

```
