Download the PHP package buzzingpixel/php-template-engine without Composer
On this page you can find all versions of the php package buzzingpixel/php-template-engine. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package php-template-engine
Just simple, basic, PHP templating.
Usage
Installation
Render a template
Get an instance of the TemplateEngine
from the TemplateEngineFactory
.
The only required item to set is the template path.
(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:
…or you can add them one at a time:
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.
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
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
:
/layout.phtml
:
There is no limit on how deep you can go on extending. An extended template can, in turn, extend another template.
But you may also want to set other sections, say, a sidebar, for your extended template. That might look something like this:
/my-template.phtml
:
/layout.phtml
:
Partials
Reusability of code is a big concern, which is why partials are of interest. Partials work a little something like this:
/my-partial.phtml
/my-template.phtml
:
Partials are a full instance of the TemplateEngine
so they have every feature any other template does. They do NOT inherit the variable context of the calling template.
Escaping
The TemplateEngine
has escaping helper methods which you should use whenever you have output from untrusted sources.