Download the PHP package emsifa/block without Composer
On this page you can find all versions of the php package emsifa/block. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package block
Block - PHP Native Template System
Block is PHP native template system inspired by Laravel Blade. Block is not template language, so block doesn't need to be compiled and cached like blade, twig, smarty, etc.
Requirements
Block requires PHP 5.5 or greater
Installation
With Composer
If your project using composer, you can install Block via composer by typing this command:
Without Composer
Block is single file library, so you can easily install it without any autoloader by following steps below:
- Download this repo or just download raw
src/Block.php
- Place it somewhere in your project. For example in
yourproject/lib/Block.php
. - Then include/require it to your code
Getting Started
Initialize Block
By default $view_extension
is php
. We prefer to use custom extension.
Custom extension make you easier to identify view files in your editor without open that file.
In this examples we use block.php
, so our view filenames must be suffixed by .block.php
instead just .php
.
Your first template
Create file path/to/views/hello.block.php
.
Render it
Then somewhere in your code, you can render it with render
method like this:
Yes. you don't need to put file extension in Block.
Result
Now the result should looks like this:
Extending and Blocking
Practically, there is two main view types in most template engines or template systems. Master view and page view.
Master view is a view that contain base html tags like <doctype>
, <html>
, <head>
, <body>
, etc.
Page view is a view that extend
master view and contain some blocks that defined in master view.
Note: Master view is not for rendered by
render
method. Master view is just for extended by any page views.
If you familiar with laravel blade syntax, here are the differences.
Blade | Block |
---|---|
@extends('view-name') | `` |
@section('content') | `` |
@stop | `` |
@show | `` |
@parent | `` |
@yield('content') | `` |
Here is simple real world case about extending and blocking
Create Master View
In example above we use
<?=
instead `` instead.
For example, let's create a new page view that contain a widget slider.
First you need to create partial view for widget slider:
Then you can include it in home
page view using put
method.
Now if you echo $block->render('pages.home')
, the output should looks like this:
Notice: slider.css
and slider.js
are placed in that order.
Add Directory Namespace
You can put second argument in setDirectory
method to set namespaced directory.
For example, you have module admin that have its own views directory.
View Composer
We have told you that Block is inspired by Blade right. So Block also have view composer like blade.
Sometimes you may have a view partial that have its own data. For example, think about navbar. In navbar, you want to display logged user name. So basically you need to pass data user name in all views who rendering that navbar. Alternatively, you may set user data inside navbar view. But set data inside view file is a bad practice.
So, the solution is using view composer. With composer, you can add some data to view before rendering that view.
Here is an example for that case:
First you need to register view composer for navbar using composer
method.
Then in your navbar, you can do this
So now, whenever navbar is rendered, composer will set variable username
to it.
You can set first argument as array if you wanna set a composer to multiple views.
Component and Slot
This is new feature in Laravel 5.4 which inspired by Vue.js.
Sometimes you may have partial view containing dynamic HTML.
With put
method, you can add HTML string in view data (second argument put
).
But, putting HTML code inside string is a bad practice, most text editors cannot highlight it.
So, this features allows you to write HTML that will be transformed to variable in partial view.
Think about alert, you may have alert which contain dynamic HTML inside it like this:
With put
method, you need to pass slot
and title
variables like this:
It is ugly to put HTML inside string like that.
With component and slot, you can put alert
view like this:
Now code inside component
will transformed into slot
variable,
and code inside slot('title')
will transformed into title
variable.
Extending with Data
For example you have case that some page using sidebar, and some page are not using it.
You can pass data to extend
method, so you don't need to pass it inside controller.
For example:
Master view
Page view
Append Section
Instead of do this:
You can do this:
Prepend Section
Instead of do this:
You can do this:
Write Code Once
For example you want to make partial view for datepicker
,
and want put datepicker script just once even you put()
it multiple times, you can use method once()
.
Example
With this approach, no matter how much you put('partials.datepicker')
, datepicker script and css only rendered once!
Dot or Slash?
I love blade for template engine, but I can't always use blade in my project, especially in small projects. So I create this library to make it as similar as blade.
In blade you can use /
or .
to load view files. So block too.
But we prefer you to use .
instead /
to make you easier remembering
that you don't need to put view extension in block.