The standalone version Blade Template Engine without Laravel in a single php file and without dependencies
The standalone version Blade Template Engine without Laravel in a single php file and without dependencies
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.
Dynamic blade components are not supported (reason: performance purpose) and custom features aimed for blade, but everything else is supported.
(spoiler) Twig is slower.
| 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.
✔ 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).
| ExampleTicketPHP | Example cupcakes | Example Search | Example Editable Grid |
|---|---|---|---|
| Custom control #1 | Custom control #2 | ||
| https://www.southprojects.com |
This version does not support PHP 5.x anymore. However, you can use the old version that is 100% functional with PHP 5.6 and higher.
You can find some tutorials and example on the folder Examples.
You could also check the Wiki
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:
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.
Let’s say that we have the next code
//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:
// 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.
Let’s say that we have the next exercise (it’s a dummy example)
$name=@$_GET['name'];
echo "my name is ".$name;It could be separates as two files:
$name=@$_GET['name'];
include "template.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:
// 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.
// template.blade.php
My name is {{$name}}BladeOne is focused on an easy syntax that it's fast to learn and to write, while it could keep the power of PHP.
Let's consider the next template:
value); ?> > text); ?>
With BladeOne, we could do the same with
@foreach($countries as $c)
value}} >{{echo html_entities($c->text)}}
@nextforeach
And if we use thehtml extension we could even reduce to
@select('id1')
@items($countries,'value','text','','')
@endselect()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.
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"
protected function compileMyFunction($expression)
{
return $this->phpTag . "echo 'YAY MY FUNCTION IS WORKING'; ?>";
}Where the function could be used in a template as follows
@myFunction('param','param2'...)Alternatively, BladeOne allows running arbitrary code from any class or method if its defined.
{{SomeClass::SomeMethod('param','param2'...)}}If you use composer, then you could add the library using the next command (command line)
composer require eftec/bladeoneIf you don't use it, then you could download the library and include it manually.
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.phpWhere $views is the folder where the views (templates not compiled) will be stored.
$cache is the folder where the compiled files will be stored.
In this example, the BladeOne opens the template hello. So in the views-folder it should exist a file called hello.blade.php
views/hello.blade.php:
Title
{{$variable1}}No open issues yet, or sync has not completed.