1. Go to this page and download the library: Download emsifa/block 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/ */
emsifa / block example snippets
use Emsifa\Block;
ws';
$view_extension = 'block.php';
$block = new Block($view_dir, $view_extension);
<h1><?= $title
echo $block->render('hello', [
'title' => 'Hello World'
'message' => 'Lorem ipsum dolor sit amet'
]);
<h1>Hello World</h1>
<p>
Lorem ipsum dolor sit amet
</p>
$block->setDirectory('path/to/admin/views', 'admin');
// then you can render it like this
$block->render('admin::pages.dashboard');
// and extend or put something in your view files like this
$this->extend('admin::master');
$this->put('admin::partials.some-chart');
$block->composer('partials.navbar', function($data) {
// $data is data you passed into `render` or `put` method
return [
'username' => Auth::user()->username
];
});
html
<!-- Stored in path/to/views/pages/lorem-ipsum.block.php -->
<?= $this->extend('master')
html
<!-- Stored in path/to/views/pages/sample-escaping.block.php -->
<div>
<h4><?= $e($title)
html
<!-- Stored in path/to/views/pages/home.block.php -->
<?= $this->extend('master')
html
<!-- Stored in path/to/views/partials/navbar.block.php -->
<nav>
<li>Some menu</li>
...
<li>
<?= $username
html
<?= $this->extend('master', ['sidebar' => false])
html
<?= $this->section('scripts')
html
<?= $this->append('scripts')
html
<?= $this->section('scripts')
html
<?= $this->prepend('scripts')
$this->extend('view-name')
$this->section('content')
$this->stop()
$this->show()
$this->parent()
echo $this->get('content')
` for `$this->section` and `$this->show`.
It is ok because those methods doesn't return a value.
#### Create Page View
In master view above, there are block `stylesheets`, `content`, and `scripts`.
So you need to define them in your page view.
> All blocks above are actually optional
#### Render It!
And the result should looks like this
## Another Useful Stuffs
#### HTML Escaping
Like another template engine, Block also have shortcut for escaping HTML.
In Block you can escaping HTML using `$this->escape($html)` or `$e($html)`.
Example:
Render
View
Then, title will be escaped like this:
#### $get($key, $default = NULL)
When rendering a view, we add variable `$get` that contain anonymous function.
This function allows you to get a value passed by `render` method.
If the key exists, it will return that value,
and if not it will return default value (NULL).
For example in master view above, if you didn't set `title` in array, it will show an error undefined variable title.
So to fix that, instead using `isset` like this
You can use `$get` like this:
Note: `$get` also support dot notation. It mean, you can access array using dot as separator in `$key`.
For example you render a view with array data like this:
You can use `$get` like this:
In example above `user.city.name` will return 'Unknown' because you didn't set `city` in array `user`.
#### Include Partial View
There is another view type called _partial view_.
_Partial view_ is a view file containing partial layout
that you can use in some _page_ or _master view_ like widget, sidebar, navbar, main-menu, etc.
_Partial view_ is like _master view_, it is not for rendered by `render` method.
But you can render it by put it inside _master_ or _page view_ via `put` method.
In Blade, you can use `@
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.