PHPackages                             buzzingpixel/php-template-engine - 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. buzzingpixel/php-template-engine

ActiveLibrary

buzzingpixel/php-template-engine
================================

1.0.0(3y ago)01.9k↑50%1Apache-2.0PHPPHP &gt;=8.2

Since Mar 28Pushed 2y agoCompare

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

READMEChangelog (1)Dependencies (9)Versions (2)Used By (1)

Just simple, basic, PHP templating.

Usage
-----

[](#usage)

### Installation

[](#installation)

```
composer require buzzingpixel/php-template-engine
```

### Render a template

[](#render-a-template)

Get an instance of the `TemplateEngine` from the `TemplateEngineFactory`.

```
use BuzzingPixel\Templating\TemplateEngineFactory;

$templateEngine = (new TemplateEngineFactory())->create();
```

The only required item to set is the template path.

```
$templateEngine->templatePath('/path/to/template.phtml');
```

(note that `pthml` is just a convention. The TemplateEngine doesn't care what the extension is)

Variables are optional. If you have any to set, you can add them in one go:

```
$templateEngine->vars([
    'foo' => 'bar',
    'baz' => 'foo',
]);
```

…or you can add them one at a time:

```
$templateEngine->addVar('foo', 'bar')->addVar('baz', 'foo');
```

And when you're ready, call `render()` on the `TemplateEngine` instance, and your template will be rendered and the content returned as a string. Here's a full example.

```
use BuzzingPixel\Templating\TemplateEngineFactory;

$renderedContent = (new TemplateEngineFactory())->create()
    ->templatePath(__DIR__ . '/my/template.phtml')
    ->addVar('foo', 'bar')
    ->render();
```

### Templating

[](#templating)

To get auto-completing in your IDE for your PHP templates, you can assert that `$this` is an instance of `\BuzzingPixel\Templating\TemplateEngine`. You can also assert any variables you're expecting in your template:

```

```

#### Template Inheritance, Extending, and Sections

[](#template-inheritance-extending-and-sections)

Extending another template is fairly simple. Just run `$this->extends('/path/to/some/template.phtml')` somewhere in your template (I like to do it at the top). All rendered content in the template is assigned to a section named `layoutContent`.

As noted, by default, when extending, all content not placed in a section gets assigned to the `layoutContent` section. This is nice in that you don't have to do a lot of work if you just have one thing you're rendering and pushing up the stack. So a simple extension might look something like this:

`/my-template.phtml`:

```
