BladeOne is a standalone version of Blade Template Engine that uses a single PHP file and can be ported and used in different projects. It allows you to use blade template outside Laravel., (*2)
, (*3)
Dynamic blade components are not supported (reason: performance purpose) and custom features aimed for blade, but everything else is supported., (*4)
Comparison with Twig
Twig is slower. đ, (*5)
First Time Time
First Time Memory
Overload First Time
Second Time
Second Time Memory
BladeOne
1962ms
2024kb
263
1917ms
2024kb
Twig
3734ms
2564kb
123
3604ms
2327kb
What it was tested?. It was tested two features (that are the most used): It was tested with an array with
1000 elements and tested many times., (*6)
NOTE about questions, reports, doubts or suggesting:
â If you want to open an inquiry, do you have a doubt, or you find a bug, then you could open an ISSUE.
Please, don't email me (or send me PM) directly for question or reports.
Also, if you want to reopen a report, then you are open to do that.
I will try to answer all and every one of the question (in my limited time)., (*8)
Some example
| ExampleTicketPHP | Example cupcakes | Example Search | Example Editable Grid |
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|
| | | | |
| Custom control #1 | Custom control #2 | | |
https://www.southprojects.com, (*9)
By standard, The original Blade library is part of Laravel (Illuminate components) and to use this template library, you require install Laravel and Illuminate-view components.
The syntax of Blade is pretty nice and bright. It's based in C# Razor (another template library for C#). It's starting to be considered a de-facto standard template system for many PHP (Smarty has been riding off the sunset since years ago) so, if we can use it without Laravel then
it's a big plus for many projects.
In fact, in theory, it is even possible to use with Laravel.
Exists different versions of Blade Template that runs without Laravel, but most requires 50 or more files, and those templates add a new level of complexity, so they are not removing Laravel but hiding:, (*13)
More files to manage.
Changes to the current project (if you want to integrate the template into an existent one)
Incompatibilities amongst other projects.
Slowness (if your server is not using op-cache)
Most of the code in the original Blade is used for future use, including the chance to use a different template engine.
Some Laravel legacy code.
This project uses a single file called BladeOne.php and a single class (called BladeOne).
If you want to use it then include it, creates the folders and that's it!. Nothing more (not even namespaces)*[]: It is also possible to use Blade even with Laravel or any other framework. After all, BladeOne is native, so it's possible to integrate into almost any project., (*14)
Why to use it instead of native PHP?
Separation of concerns
Letâs say that we have the next code, (*15)
//some PHP code
// some HTML code
// more PHP code
// more HTML code.
It leads to a mess of a code. For example, letâs say that we oversee changing the visual layout of the page. In this case, we should change all the code,
and we could even break part of the programming.
Instead, using a template system works in the next way:, (*16)
// some php code
ShowTemplate();
We are separating the visual layer from the code layer. As a plus, we could assign a non-php-programmer in charge to edit the template, and he/she doesnât need to touch or know our php code., (*17)
Security
Letâs say that we have the next exercise (itâs a dummy example), (*18)
$name=@$_GET['name'];
echo "my name is ".$name;
It could be separates as two files:
```php // index.php
$name=@$_GET['name'];
include "template.php";, (*19)
```php
// template.php
echo "my name is ".$name;
Even for this simple example, there is a risk of hacking. How? A user could send malicious code by using the GET variable, such as html or even javascript.
The second file should be written as follows:, (*20)
// template.php
echo "my name is ".html_entities($name);
html_entities should be used in every single part of the visual layer (html) where the user could inject malicious code, and itâs a real tedious work. BladeOne does it automatically., (*21)
// template.blade.php
My name is {{$name}}
Easy to use
BladeOne is focused on an easy syntax that it's fast to learn and to write, while it could keep the power of PHP., (*22)
Let's consider the next template:, (*23)
```php // template.php
, (*24)
With BladeOne, we could do the same with
```php // template.blade.php
<select>
@foreach($countries as $c)
<option value={{$c->value}} >{{echo html_entities($c->text)}}</option>
@nextforeach
</select>
And if we use thehtml extension we could even reduce to, (*25)
### Performance
This library works in two stages.
The first is when the template calls the first time. In this case, the template compiles and store in a folder.
The second time the template calls then, it uses the compiled file. The compiled file consist mainly in native PHP, so **the performance is equals than native code.** since the compiled version IS PHP.
### Scalable
You could add and use your own function by adding a new method (or extending) to the BladeOne class. NOTE: The function should start with the name "compile"
```php
protected function compileMyFunction($expression)
{
return $this->phpTag . "echo 'YAY MY FUNCTION IS WORKING'; ?>";
}
Where the function could be used in a template as follows, (*27)
@myFunction('param','param2'...)
Alternatively, BladeOne allows running arbitrary code from any class or method if its defined., (*28)
{{SomeClass::SomeMethod('param','param2'...)}}
Install (pick one of the next one)
1) Download the file manually then unzip (using WinRAR,7zip or any other program) https://github.com/EFTEC/BladeOne/archive/master.zip
2) git clone https://github.com/EFTEC/BladeOne (it doesn't include the examples)
3) Composer. See usage
4) wget https://github.com/EFTEC/BladeOne/archive/master.zip
unzip master.zip, (*29)
Usage
If you use composer, then you could add the library using the next command (command line), (*30)
composer require eftec/bladeone
If you don't use it, then you could download the library and include it manually., (*31)
Explicit definition
use eftec\bladeone\BladeOne;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
$blade = new BladeOne($views,$cache,BladeOne::MODE_DEBUG); // MODE_DEBUG allows to pinpoint troubles.
echo $blade->run("hello",array("variable1"=>"value1")); // it calls /views/hello.blade.php
Where $views is the folder where the views (templates not compiled) will be stored.
$cache is the folder where the compiled files will be stored., (*32)
In this example, the BladeOne opens the template hello. So in the views-folder it should exist a file called hello.blade.php, (*33)
views/hello.blade.php:, (*34)
<h1>Title</h1>
{{$variable1}}
Implicit definition
In this mode, it uses the folders __DIR__/views and __DIR__/compiles, also it uses the mode as MODE_AUTO., (*35)
use eftec\bladeone\BladeOne;
$blade = new BladeOne(); // MODE_DEBUG allows to pinpoint troubles.
echo $blade->run("hello",array("variable1"=>"value1")); // it calls /views/hello.blade.php
Injection
You can inject the Bladeone using an existing instance of it. If there is no instance, then it will create a new one using
the default folders., (*36)
$blade=BladeOne::$instance;
echo $blade->run("hello",array("variable1"=>"value1")); // it calls /views/hello.blade.php
Fluent
use eftec\bladeone\BladeOne;
$blade = new BladeOne(); // MODE_DEBUG allows to pinpoint troubles.
echo $blade->setView('hello') // it sets the view to render
->share(array("variable1"=>"value1")) // it sets the variables to sends to the view
->run(); // it calls /views/hello.blade.php
Filter (Pipes)
It is possible to modify the result by adding filters to the result., (*37)
Let's say we have the next value $name='Jack Sparrow', (*38)
$blade=new BladeOne();
$blade->pipeEnable=true; // pipes are disable by default so it must be enable.
echo $blade->run('template',['name'=>'Jack Sparrow']);
Our view could look like:, (*39)
{{$name}} or {!! $name !!} // Jack Sparrow
What if we want to show the name in uppercase?., (*40)
We could do in our code $name=strtoupper('Jack Sparrow'). With Pipes, we could do the same as follows:, (*41)
{{$name | strtoupper}} // JACK SPARROW
We could also add arguments and chain methods., (*42)
{{$name | strtoupper | substr:0,5}} // JACK
You can find more information on https://github.com/EFTEC/BladeOne/wiki/Template-Pipes-(Filter), (*43)
Security (optional)
require "vendor/autoload.php";
Use eftec\bladeone;
$views = __DIR__ . '/views';
$cache = __DIR__ . '/cache';
$blade=new bladeone\BladeOne($views,$cache,BladeOne::MODE_AUTO);
$blade->setAuth('johndoe','admin'); // where johndoe is an user and admin is the role. The role is optional
echo $blade->run("hello",array("variable1"=>"value1"));
If you log in using blade then you could use the tags @auth/@endauth/@guest/@endguest, (*44)
@auth
// The user is authenticated...
@endauth
@guest
// The user is not authenticated...
@endguest
or, (*45)
@auth('admin')
// The user is authenticated...
@endauth
@guest('admin')
// The user is not authenticated...
@endguest
Custom controls.
There are multiples ways to create a new control (tag), (*46)
$blade = new BladeOne();
// with the method addAliasClasses
$blade->addAliasClasses('SomeClass', '\namespace1\namespace2\SomeClass');
// with the setter setAliasClasses
$blade->setAliasClasses(['SomeClass'=>'\namespace1\namespace2\SomeClass']);
// or directly in the field
$blade->aliasClasses=['SomeClass'=>'\namespace1\namespace2\SomeClass'];
The old method select only allows a limited number of arguments. And the order of the arguments is important., (*63)
The new method select allows adding different types of arguments, (*64)
Command Line (CLI)
, (*65)
BladeOne (since the version v4.2) allows to run some operations via command line (CLI), (*66)
How to run it?, (*67)
Go to your home path and call the PHP script as follows:
php vendor/bin/bladeonecli # windows/linux/macos
# or you could execute the script as:
./vendor/bin/bladeonecli.bat # windows
./vendor/bin/bladeonecli # linux/macos
Or change you folder according to your installation., (*68)
And you can set the syntax as follows:, (*69)
-templatepath (optional) the template-path (view paths).
Example: '/folder/views' or 'views' (relative)
-compilepath (optional) the compile-path.
Example: '/folder/compiles or 'compiles' (relative)
-clearcompile It deletes the content of the compile-path
-createfolder It creates the "compile" and "template" folders
You could download it or add it via Composer, (*77)
composer require eftec/bladeonehtml, (*78)
Collaboration
You are welcome to use it, share it, ask for changes and whatever you want to. Just keeps the copyright notice in the file., (*79)
Future
Blade locator/container
License
MIT License.
BladeOne (c) 2016-2024 Jorge Patricio Castro Castillo
Blade (c) 2012 Laravel Team (This code is based and inspired in the work of the team of Laravel, however BladeOne is
mostly an original work), (*80)