• Stars
    star
    719
  • Rank 60,788 (Top 2 %)
  • Language
    PHP
  • License
    Other
  • Created almost 8 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

The standalone version Blade Template Engine without Laravel in a single php file and without dependencies

Logo

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.

Packagist Total Downloads Maintenance composer php php php CocoaPods

NOTE: So far it's apparently the only one project that it's updated with the latest version of Blade 8 (January 2022). Dynamic blade components are not supported (reason: performance purpose) and custom features aimed for blade, but everything else is supported [missing](#missing.

Comparison with Twig

(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.

Comparison with Twig

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

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

https://www.southprojects.com

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.

Laravel blade tutorial

You can find some tutorials and example on the folder Examples.

You could also check the Wiki

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:

  • 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.

Why to use it instead of native PHP?

Separation of concerns

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.

Security

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}}

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.

Let's consider the next template:

<select>
    <? foreach($countries as $c) { ?>
        <option value=<? echo html_entities($c->value); ?> > <? echo html_entities($c->text); ?></option>
    <? } ?>
</select>

With BladeOne, we could do the same with

<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

@select('id1')
    @items($countries,'value','text','','')
@endselect()

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"

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'...)}}

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
  3. Composer. See usage
  4. wget https://github.com/EFTEC/BladeOne/archive/master.zip unzip master.zip

Usage

If you use composer, then you could add the library using the next command (command line)

composer require eftec/bladeone

If you don't use it, then you could download the library and include it manually.

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.

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:

<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.

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

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.

Let's say we have the next value $name='Jack Sparrow'

$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:

 {{$name}}  or {!! $name !!} // Jack Sparrow

What if we want to show the name in uppercase?.

We could do in our code $name=strtoupper('Jack Sparrow'). With Pipes, we could do the same as follows:

 {{$name | strtoupper}} // JACK SPARROW 

We could also add arguments and chain methods.

 {{$name | strtoupper | substr:0,5}} // JACK

You can find more information on https://github.com/EFTEC/BladeOne/wiki/Template-Pipes-(Filter)

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

@auth
    // The user is authenticated...
@endauth

@guest
    // The user is not authenticated...
@endguest

or

@auth('admin')
    // The user is authenticated...
@endauth

@guest('admin')
    // The user is not authenticated...
@endguest

Extensions Libraries (optional)

BladeOneCache Documentation

https://github.com/eftec/BladeOneHtml

Calling a static methods inside the template.

Since 3.34, BladeOne allows to call a static method inside a class.

Let's say we have a class with namespace \namespace1\namespace2

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:

Add the next line to the template

@use(\namespace1\namespace2)

and the next lines to the template (different methods)

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

All those methods are executed at runtime

Method 2 Alias

Or we could define alias for each class.

php code:

    $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:

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

We won't need alias or use for global classes.

Named argument (since 3.38)

BladeOne allows named arguments. This feature must be implemented per function.

Let's say the next problem:

It is the old library BladeOneHtml:

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

And it is the new library:

@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.

The new method select allows adding different types of arguments

Command Line (CLI)

docs/cli.png

BladeOne (since the version v4.2) allows to run some operations via command line (CLI)

How to run it?

  • 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.

And you can set the syntax as follows:

  • -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

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

Check the folders, if the folder exists, if it has the right permissions, etc.

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

Example to clear the compile-folder using a custom compile path

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.

It uses a new feature of BladeOne: named arguments

Example to create a select:

@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

You could download it or add it via Composer

composer require eftec/bladeonehtml

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.

Future

  • Blade locator/container

License

MIT License. BladeOne (c) 2016-2023 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)

More Repositories

1

StateMachineOne

State Machine library for PHP
PHP
109
star
2

PdoOne

It is a simple library for PHP that simplify the use of the PDO extension.
PHP
107
star
3

php-benchmarks

It is a collection of php benchmarks
PHP
69
star
4

Cyberarms

IDDS is a free and open source intrusion detection and prevention system for Windows Server 2008 R2 and later.
C#
49
star
5

AutoLoadOne

AutoLoadOne is a program for PHP auto include classes by parsing a project and generates an auto load class
PHP
37
star
6

RouteOne

Route for PHP
PHP
29
star
7

AMPGeneratorOne

It generates Google AMP sites using PHP
PHP
15
star
8

DocumentStoreOne

A flat document store for PHP that allows multiples concurrencies
PHP
11
star
9

CacheOne

CacheOne is a cache of service for php
PHP
11
star
10

UsagiMQ

A minimalist Message Queue by using Redis and PHP.
PHP
11
star
11

ValidationOne

It's a php library for fetch and validate fields
PHP
10
star
12

MiniLang

A mini language script for PHP
PHP
10
star
13

gentelella-bladeone

A bootstrap 3 admin using bladeone template (php)
HTML
9
star
14

DaoOne

Database Access Object wrapper for PHP and MySqli in a single class
PHP
9
star
15

BladeOneHtml

BladeOne HTML Extension
PHP
8
star
16

DashOne

A minimalist dashboard generator for PHP
PHP
7
star
17

example.cupcakes

Example of a catalog of cupcakes made with php
HTML
5
star
18

SecurityOneMysql

Security Library for Mysql.
PHP
5
star
19

ApiAssembler

This library creates a boilerplate of an API Rest server based in the tables of a database.
PHP
5
star
20

example-php-editablegrid

It is an example of an editable grid, php and mysql
PHP
4
star
21

AuthOne

An Auth library for PHP
PHP
3
star
22

PdoOne_login

An example of a login page using php and mysql
PHP
3
star
23

Services_JSON

PHP implementaion of json_encode/decode
PHP
3
star
24

ChaosMachineOne

A controlled random generator data for PHP
PHP
3
star
25

mapache-commons

It's a set of useful functions for PHP. The name is a pun.
PHP
3
star
26

ObjDoctor

Object Doctor is a small tool for open and edit Obj Wavefront 3d format files
C#
3
star
27

SecurityOne

It's a barebone security class written on PHP
PHP
2
star
28

cloudking

A SOAP Server Engine for PHP 5.6 and higher.
PHP
2
star
29

FormOne

Creates HTML web Form on PHP
PHP
2
star
30

MessageContainer

It is a Message Container for PHP, similar in functionality MessageBag for Laravel
PHP
2
star
31

FileTextOne

pending
PHP
1
star
32

BladeOne-tutorial1

A PHP Tutorial of BladeOne, DaoOne and ValidationOne
HTML
1
star
33

RutChile

Librería para el RUT chileno en C#
C#
1
star
34

PdoOneORM

PHP
1
star
35

ArrayOne

ArrayOne
PHP
1
star
36

MvcOne

It's a MVC implementation on PHP based on Asp.net MVC
PHP
1
star
37

CliOne

A minimanist PHP library to manage the Command Line operations (CLI)
PHP
1
star
38

mailcapuchin

A massive email server made on PHP
PHP
1
star