Download the PHP package tyam/bamboo without Composer
On this page you can find all versions of the php package tyam/bamboo. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package bamboo
Short Description PHP template engine, which has variables-pulling facility.
License MIT
Homepage https://github.com/tyam/bamboo
Informations about the package bamboo
bamboo
Bamboo is a PHP template engine, which has the following characteristics:
- native PHP as template language; no compile. familiar and well-defined syntax
- using global functions as helpers/escapers; no redundant
$e->h()
/E::h()
. justeh()
- facility to PULL template variables from the program side
Installation
Basic Usage
PHP side
template side: /your/template/dirs/mytpl.php
- A filename extension for template file is 'php'
- Path separator for the base directory is fixed as slash. bamboo engine converts to host specific one.
Including another template
subject template: /your/template/dirs/mytpl.php
another template: /your/template/dirs/common/another.php
$renderer
is a special variable from bamboo, which provides you the rendering instructions.- Template files can be grouped by subdirectories. Then a template name becomes 'subdir/template' (slash delimited).
Template inheritance
child template: /your/template/dirs/content.php
parent template: /your/template/dirs/common/layout.php
- In child template, by
wrap()
you can specify parent template. The rest part becomes a content for the parent. - In parent template, by
content()
you can outpupt the content generated by a child. - Template inheritance must be single. You cannot specify
wrap()
twice. - Multi-level inheritance is supported. Note that
content()
outputs a result of exact child template. You must callcontent()
every parent template.
Sections (Capturing block for other template)
child template: /your/template/dirs/content.php
parent template: /your/template/dirs/common/layout.php
- You can capture a block of content, called 'section', between
section()
andendsection()
. - Sections are named. Then, you can specify a name of a section to
yield()
to output a block. - Passing a section name to
endsection()
is redundant and optional, but useful as sanity check purpose. Bamboo throws an exception when the passed name was insane. - Note that evaluation order is matter. Bamboo always evaluates child first. So you can
yield()
only sections captured by descendant templates.
Variables passing
- You can pass template variables when you call another template.
- Note that you must pass template variables for each template. Template variables are not shared among templates.
Variables pulling
On production sites, a page is filled by dynamic contents. User name, logged in or not, notifications, recommendations, advertisements, etc. They all are side contents, not a main content.
We must fetch them all from the Model layer, then bind to template as variables. That code makes our PHP side (controller, responder or anything) ugly.
To improve this situation, bamboo has the variable-pulling facility. With it, templates automatically pull template variables from PHP side. Then, you can separate the code of side contents from that of main content.
Variable-pulling is actualized by passing a variable provider to bamboo engine.
PHP side
- Before rendering a template, bamboo calls
provideVariables()
with its name. Then, you return variables for the template. - In above example, template varialbes
user
,notifications
andrecommendations
can be used in 'my/template', its parents and all included templates. - If variable names collided, explicit ones, which is passed via
$engine->render()
,$renderer->wrap()
or$renderer->include()
, hides implicit ones, which is pulled viaprovideVariables()
.
Getting section values
As a effect of template rendering, other than returned output, there exists section values.
Usually section values are consumed in templates, and PHP side has no interest in them.
But, when PHP side is requiring something other than body output, sections are good place to track.
You can pass an ArrayAccess
object to $engine->render()
. Then bamboo uses this array to hold all section values, and you can investigate it after rendering.
The good example is e-mail tamplating.
template: e-mail template
PHP side
In the example above, we use sections to track some e-mail header values.
Other topics
- I recommend you define helper/escaper functions in the global namespace. If that is a bother, you can find some functions in 'function.php' in this package. Call
Engine::loadFunctions()
if you like it.
Lisence
Bamboo is MIT licensed. See LICENSE.