1. Go to this page and download the library: Download younishd/endobox library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
younishd / endobox example snippets
use endobox\Endobox;
$endobox = Endobox::create('path/to/templates');
$endobox->addFolder('another/path/to/templates');
$welcome = $endobox('welcome');
echo $welcome->render([ 'name' => "Alice" ]);
$members = $endobox('members'); // no file extension
// via assign(…)
$welcome->assign([ "username" => "eve" ]);
// via object property
$welcome->username = "eve";
// via render(…)
$welcome->render([ "username" => "eve" ]);
// implicitly
$welcome([ "username" => "eve" ]);
$welcome->username = "eve"; // not accessible to 'profile'
$profile->email = "[email protected]"; // not accessible to 'welcome'
<p>Follow me on <?= $a("GitHub", "https://github.com/younishd")
echo`.
#### Markdown: `.md`
Markdown templates are processed by a Markdown parser ([Parsedown](https://github.com/erusev/parsedown)) which produces the corresponding HTML code. This can be used for static content.
###### `members.md`
#### PHP+Markdown: `.md.php`
As the name suggests, this template type combines both PHP and Markdown: The template gets evaluated as PHP first, then parsed as Markdown. Pretty neat.
###### `members.md.php`
#### HTML: `.html`
HTML templates are always printed as is. No further processing takes place.
###### `members.html`
### Data
Data is accessible inside a template as simple __variables__ (e.g., `$foo`) where the variable name corresponds to the assigned array key or property.
#### Assign data
There are several ways to assign data to a template box:
#### Shared data
Usually, template boxes are isolated from each other. Data that's been assigned to one box, will not be visible from another.
If they should share their data however, you can __link__ them together:
Now, these template boxes are linked and they share the same data.
###### `welcome.php`
###### `profile.php`
Notice how `welcome.php` prints out `$email` which was initially assigned to `$profile` and `profile.php` echoes `$username` even though it was assigned to `$welcome`.
> :information_source: __Protip:__ You can create template boxes using an existing `Box` object (instead of using the `BoxFactory` object) with `$box->create('template')` which has the advantage of __linking the two boxes__ together by default.
#### Default values
Sometimes it can be useful to supply a __default value__ to be printed in case a variable has not been assigned. You can easily achieve that using PHP 7's [__null coalescing operator__](https://en.wikipedia.org/wiki/Null_coalescing_operator#PHP): `??`
#### Escaping
Escaping is a form of data filtering which sanitizes unsafe, user supplied input prior to outputting it as HTML.
endobox provides two shortcuts to the `htmlspecialchars()` function: `$escape()` and its shorthand version `$e()`
##### Escaping HTML attributes
> :warning: __Warning:__ It's VERY important to always double quote HTML attributes that contain escaped variables, otherwise your template will still be open to injection attacks (e.g., [XSS](https://www.owasp.org/index.php/Cross-site_Scripting_(XSS))).
### Chaining & Nesting
Since you're rarely dealing with just a single template you might be looking for a method that combines multiple templates in a meaningful way.
#### Chaining
By __chaining__ we mean concatenating templates without rendering them.
Chaining two templates is as simple as:
Now, calling `->render()` on either `$header` or `$article` will render both templates and return the concatenated result.
> :information_source: __Protip:__ The benefit of not having to render the templates to strings right away is _flexibility_: You can define the layout made out of your templates before knowing the concrete values of their variables.
The general syntax for chaining a bunch of templates is simply:
Neat.
The more explicit (and strictly equivalent) form would be using `append()` or `prepend()` as follows:
Or…
> :information_source: __Protip:__ Note that the previously seen `Box::__invoke()` is simply an alias of `Box::append()`.
You have now seen how you can append (or prepend) `Box`es together.
Notice however that the variables `$first`, `$second`, `$third`, and `$fourth` were objects of type `Box` which means they needed to be brought to life at some point —
probably using the `BoxFactory` object created in the very beginning (which we kept calling `$endobox` in this document).
In other words the complete code would probably look something like this:
It turns out there is a way to avoid this kind of boilerplate code: You can directly pass the name of the template (a `string`) to the `append()` method instead of the `Box` object!
So, instead you could just write:
It looks trivial, but there is a lot going on here. The more verbose form would look as follows:
This is – in turn – equivalent to the following lines:
Notice that unlike before these (implicitly created) boxes are now all __linked__ together automatically, meaning they share the same data.
The rule of thumb is: _Boxes created from other boxes are linked by default._
#### Nesting
A fairly different approach (probably the _template designer_ rather than the _developer_ way) would be to define some sort of __layout template__ instead:
###### `layout.php`
Then somewhere in controller land:
This should be fine, but we can get rid of some boilerplate code here: `$header` and `$footer` really don't need to be variables.
That's where __nesting__ comes into play!
Use the `$box()` function to instantiate a template `Box` from _inside_ another template:
###### `layout.php`
Then simply…
This is already much cleaner, but it gets even better: By using `$box()` to nest a template `Box` inside another these two boxes will be __linked__ by default!
That allows us to condense this even further. Check it out:
###### `layout.php`
All three templates are now nested using `$box()` and therefore linked to their parent (i.e., `$layout`).
This reduces our controller code to one line:
Notice how we are assigning a title to the `layout` template even though the actual `$title` variable occurs in the nested `article` template.
> :information_source: __Protip:__ The `$box()` function is also available as a method of `Box` objects (i.e., outside templates): You can instantiate new boxes with `$box->create('template')` where `$box` is some `Box` object that has already been created.
### Functions
Functions are a cool and handy way of adding reusable functionality to your templates (e.g., filters, URL builders…).
#### Registering functions
You can register your own custom function (i.e., [closure](https://www.php.net/manual/en/functions.anonymous.php)) by simply assigning it to a template `Box` __just like data!__
Here is a simple function `$day()` which returns the day of the week:
Inside your template file you can then use it in the same fashion as any variable:
This would look like this (at least sometimes):
You can go even further and actually invoke the variable just like any function and actually __pass some arguments__ along the way.
Below is a simple closure `$a()` that wraps and escapes some text in a hyperlink tag:
Calling this function inside your template would look like this:
This would produce something like this:
#### Default functions
There are a couple of default helper functions that you can use out of the box (some of which you may have already seen):
|Function|Description|Example|
|--|--|--|
|`$box()` or `$b()`|Instantiate a `Box` from within another template. (See [Nesting](#chaining--nesting).)|`<article><?= $box('article')
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.