2017 © Pedro PelĂĄez
 

library bladeone

The standalone version Blade Template Engine from Laravel in a single php file

image

eftec/bladeone

The standalone version Blade Template Engine from Laravel in a single php file

  • Saturday, July 28, 2018
  • by JorgeCastro
  • Repository
  • 13 Watchers
  • 76 Stars
  • 1,530 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 24 Forks
  • 0 Open issues
  • 17 Versions
  • 33 % Grown

The README.md

Logo, (*1)

BladeOne Blade Template Engine

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)

Packagist Total Downloads Maintenance composer php php php CocoaPods, (*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)

Comparison with Twig, (*7)

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 | |------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------| | example php bladeone | example php bladeone cupcakes | example php bladeone search | example php bladeone search | | Custom control #1 | Custom control #2 | | | https://www.southprojects.com, (*9)

Manual

PHP 5.x support?

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., (*10)

Laravel blade tutorial

You can find some tutorials and example on the folder Examples., (*11)

You could also check the Wiki, (*12)

About this version

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)

```php // template.blade.php @select('id1') @items($countries,'value','text','','') @endselect(), (*26)





### 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)

Extensions Libraries (optional)

BladeOneCache Documentation, (*47)

https://github.com/eftec/BladeOneHtml, (*48)

Calling a static methods inside the template.

Since 3.34, BladeOne allows to call a static method inside a class., (*49)

Let's say we have a class with namespace \namespace1\namespace2, (*50)

namespace namespace1\namespace2 {
    class SomeClass {
        public static function Method($arg='') {
            return "hi world";
        }
    }
}

Method 1 PHP Style

We could add a "use" in the template. Example:, (*51)

Add the next line to the template, (*52)

@use(\namespace1\namespace2)

and the next lines to the template (different methods), (*53)

{{SomeClass::Method()}}
{!! SomeClass::Method() !!}
@SomeClass::Method()

All those methods are executed at runtime, (*54)

Method 2 Alias

Or we could define alias for each class., (*55)

php code:, (*56)

    $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'];

Template:, (*57)

{{SomeClass::Method()}}
{!! SomeClass::Method() !!}
@SomeClass::Method()

We won't need alias or use for global classes., (*58)

Named argument (since 3.38)

BladeOne allows named arguments. This feature must be implemented per function., (*59)

Let's say the next problem:, (*60)

It is the old library BladeOneHtml:, (*61)

@select('id1')
    @item('0','--Select a country--',"",class='form-control'")
    @items($countries,'id','name',"",$countrySelected)
@endselect

And it is the new library:, (*62)

@select(id="aaa" value=$selection values=$countries alias=$country)
    @item(value='aaa' text='-- select a country--')
    @items( id="chkx" value=$country->id text=$country->name)
@endselect

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)

docs/cli.png, (*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
  • -check It checks the library

Clear the compile-folder, (*70)

php vendor/lib/eftec/bladeone/lib/BladeOne.php -clearcompile

Check the folders, if the folder exists, if it has the right permissions, etc., (*71)

php vendor/lib/eftec/bladeone/lib/BladeOne.php -check

Example to clear the compile-folder using a custom compile path, (*72)

php vendor/lib/eftec/bladeone/lib/BladeOne.php -clearcompile -compilepath mycompile # relative path to the current location
php vendor/lib/eftec/bladeone/lib/BladeOne.php -clearcompile -compilepath /var/mycompile # absolute path (Linux/MacOS)
php vendor/lib/eftec/bladeone/lib/BladeOne.php -clearcompile -compilepath c:\var\mycompile # absolute path (Windows)

BladeOneHtml

It is a new extension to BladeOne. It allows creating HTML components easily and with near-to-native performance., (*73)

It uses a new feature of BladeOne: named arguments, (*74)

Example to create a select:, (*75)

@select(id="aaa" value=$selection values=$countries alias=$country)
    @item(value='aaa' text='-- select a country--')
    @items( id="chkx" value=$country->id text=$country->name)
@endselect

https://github.com/eftec/BladeOneHtml, (*76)

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)

The Versions

28/07 2018

dev-master

9999999-dev https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template php blade view

27/07 2018

3.1

3.1.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template php blade view

12/07 2018

3.0

3.0.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template php blade view

12/07 2018

2.4

2.4.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template php blade view

18/06 2018

2.3.5

2.3.5.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template php blade view

18/06 2018

2.3.4

2.3.4.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template php blade view

17/06 2018

2.3.3

2.3.3.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template php blade view

13/04 2018

2.3

2.3.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template blade view

21/07 2017

1.9

1.9.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template blade view

10/04 2017

1.8

1.8.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template blade view

14/08 2016

1.6

1.6.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

The Requires

  • php >=5.4

 

by Jorge Patricio Castro Castillo

template blade view

26/06 2016

1.4

1.4.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

by Jorge Patricio Castro Castillo

template blade view

13/06 2016

1.3

1.3.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

by Jorge Patricio Castro Castillo

template blade view

11/06 2016

1.2

1.2.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

by Jorge Patricio Castro Castillo

template blade view

09/06 2016

1.0

1.0.0.0 https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

by Jorge Patricio Castro Castillo

template blade view

09/06 2016

0.2Beta

0.2.0.0-beta https://github.com/EFTEC/BladeOne

The standalone version Blade Template Engine from Laravel in a single php file

  Sources   Download

MIT

by Jorge Patricio Castro Castillo

template blade view

09/06 2016

0.1beta

0.1.0.0-beta

Blade Template System in a single php file

  Sources   Download

MIT

by Jorge Patricio Castro Castillo