View template engine of PHP extracted from Laravel

Related tags

Templating blade
Overview

Blade

【简体中文】

This is a view templating engine which is extracted from Laravel. It's independent without relying on Laravel's Container or any others.

Installation

With Composer, you just need to run

composer require xiaoler/blade

If you haven't use composer, you should add all the files in folder src to your project folder, and then require them in your code.

Usage

<?php

require '../src/Autoloader.php';

Xiaoler\Blade\Autoloader::register();

use Xiaoler\Blade\FileViewFinder;
use Xiaoler\Blade\Factory;
use Xiaoler\Blade\Compilers\BladeCompiler;
use Xiaoler\Blade\Engines\CompilerEngine;
use Xiaoler\Blade\Filesystem;
use Xiaoler\Blade\Engines\EngineResolver;

$path = ['/view_path'];         // your view file path, it's an array
$cachePath = '/cache_path';     // compiled file path

$file = new Filesystem;
$compiler = new BladeCompiler($file, $cachePath);

// you can add a custom directive if you want
$compiler->directive('datetime', function($timestamp) {
    return preg_replace('/(\(\d+\))/', '<?php echo date("Y-m-d H:i:s", $1); ?>', $timestamp);
});

$resolver = new EngineResolver;
$resolver->register('blade', function () use ($compiler) {
    return new CompilerEngine($compiler);
});

// get an instance of factory
$factory = new Factory($resolver, new FileViewFinder($file, $path));

// if your view file extension is not php or blade.php, use this to add it
$factory->addExtension('tpl', 'blade');

// render the template file and echo it
echo $factory->make('hello', ['a' => 1, 'b' => 2])->render();

You can enjoy almost all the features of blade with this extension. However, remember that some of exclusive features are removed.

You can't:

  • use @inject @can @cannot @lang in a template file
  • add any events or middleawares

Documentation: http://laravel.com/docs/5.4/blade

Thanks for Laravel and it authors. That is a great project.

Comments
  • Fatal error with undefined method last when using components

    Fatal error with undefined method last when using components

    Inside \Concerns\ManagesComponents.php at line 109 the line has last($this->componentStack); which should be Arr::last($this->componentStack); and use the static class at the top use Xiaoler\Blade\Support\Arr;.

    opened by clytras 3
  • Undefined property: Xiaolin\Blade\Engines\CompilerEngine::$session

    Undefined property: Xiaolin\Blade\Engines\CompilerEngine::$session

    Problems working with sessions in codeigniter.

    A PHP Error was encountered

    Severity: Notice

    Message: Undefined property: Xiaoler\Blade\Engines\CompilerEngine::$session

    Filename: cache/877d4256ca2b11e0616b3866aa1e54f6

    opened by robertsanseries 3
  • Undefined offset: 3

    Undefined offset: 3

    protected function compileStatements($value) { $callback = function ($match) { if (strpos($match[1], '@') !== false) { $match[0] = isset($match[3]) ? $match[1].$match[3] : $match[1]; } elseif (isset($this->customDirectives[$match[1]])) { $match[0] = call_user_func($this->customDirectives[$match[1]], ($match[3] ?: '')); } elseif (method_exists($this, $method = 'compile'.ucfirst($match[1]))) { $match[0] = $this->$method(($match[3] ?: '')); }

        return isset($match[3]) ? $match[0] : $match[0].$match[2];
    };
    
    return preg_replace_callback('/\B@(@?\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $callback, $value);
    

    } xiaoler\blade\src\Compilers\BladeCompiler.php 248 line

    opened by Excas 3
  • Fatal error: require(): Failed opening required '../src/helpers.php'

    Fatal error: require(): Failed opening required '../src/helpers.php'

    I get a fatal error when I'm trying to use the example code. Am I doing something wrong?

    Warning: require(../src/helpers.php): failed to open stream: No such file or directory in /vendor/xiaoler/blade/src/Autoloader.php on line 12

    Warning: require(../src/helpers.php): failed to open stream: No such file or directory in /vendor/xiaoler/blade/src/Autoloader.php on line 12

    Fatal error: require(): Failed opening required '../src/helpers.php' (include_path='.:/opt/php56/lib/php') in /vendor/xiaoler/blade/src/Autoloader.php on line 12

    EDIT

    Just changed require '../src/helpers.php'; to require 'helpers.php'; inside the Autoloader.php and it's now working. Why do I get this error? I just C/P the sample code and created a simple view. Also why does the include use ../src/ for helpers.php, a file that it's inside the same directory with the Autoloader.php?

    opened by clytras 2
  • Argument 1 passed to Xiaoler\Blade\Compilers\Compiler::__construct()

    Argument 1 passed to Xiaoler\Blade\Compilers\Compiler::__construct()

    Hi guys,

    An uncaught Exception was encountered

    Type: TypeError

    Message: Argument 1 passed to Xiaoler\Blade\Compilers\Compiler::__construct() must be an instance of Xiaoler\Blade\Filesystem, string given, called in C:\xampp\htdocs\application\libraries\Blade.php on line 18

    Filename: C:\xampp\htdocs\vendor\xiaoler\blade\src\Compilers\Compiler.php

    Line Number: 33

    Backtrace:

    File: C:\xampp\htdocs\application\libraries\Blade.php Line: 18 Function: __construct

    File: C:\xampp\htdocs\index.php Line: 315 Function: require_once

    opened by marcuxyz 1
  • Error while registering autoloader

    Error while registering autoloader

    Hello, Im trying to do a basic render but after requiring the autoload.php and doing

    Xiaoler\Blade\Autoloader::register();

    I'm getting the following error:

    ( ! ) Warning: require(./helpers.php): failed to open stream: No such file or directory in /var/www/html/some-path/vendor/xiaoler/blade/src/Autoloader.php on line 12

    the line responsible inside Autoloader is: require './helpers.php';

    presumably because the ./

    opened by vanrez-nez 1
  • 你好, 请教一个关于laravel的 DB 基本用法迁移的问题

    你好, 请教一个关于laravel的 DB 基本用法迁移的问题

    如这里所说: http://stackoverflow.com/questions/27956821/can-i-use-dbtabletags-outside-of-laravel
    网上有很多教程移植 Eloquent , 并且可以使用成功, 如移植到 codeigniter 中, 可以直接使用 Active Record 用法(User::first());
    可是, 如何移植他的基本用法呢 ? 如: DB::table('user')->get();

    opened by fizzcome 1
  • fix issue call continue & break without condition

    fix issue call continue & break without condition

    fix error with complier “@continue” and “@break” without condition

    “ifArray continue”

    because parameter “$expression” passed as array to “compileContinue” & “compileBreak” when must to be empty if blade statement call without parameter

    opened by salkhwlani 0
  • xiaoler/blade depends on symfony/debug

    xiaoler/blade depends on symfony/debug

    The Engines/PhpEngine.php uses `Symfony\Component\Debug\Exception\FatalThrowableError´.

    In effect, this means that xiaoler/blade depends on symfony/debug.

    opened by frodeborli 0
  • 会提示一个错误这个依赖好像没有去除

    会提示一个错误这个依赖好像没有去除

    ERROR: Error: Class 'Symfony\Component\Debug\Exception\FatalThrowableError' not found in D:\phpStudy\WWW\teebphp\library\blade\src\Engines\PhpEngine.php:46 Stack trace: #0 D:\phpStudy\WWW\teebphp\library\blade\src\Engines\CompilerEngine.php(60): Xiaoler\Blade\Engines\PhpEngine->evaluatePath('D:\phpStudy\WWW...', Array) #1 D:\phpStudy\WWW\teebphp\library\blade\src\View.php(132): Xiaoler\Blade\Engines\CompilerEngine->get('D:\phpStudy\WWW...', Array) #2 D:\phpStudy\WWW\teebphp\library\blade\src\View.php(115): Xiaoler\Blade\View->getContents() #3 D:\phpStudy\WWW\teebphp\library\blade\src\View.php(82): Xiaoler\Blade\View->renderContents() #4 D:\phpStudy\WWW\teebphp\library\blade\src\Blade.class.php(51): Xiaoler\Blade\View->render() #5 [internal function]: Blade->make('admin/index', Array) #6 D:\phpStudy\WWW\teebphp\app\Component\Blade\BladeManager.php(41): call_user_func_array(Array, Array) #7 D:\phpStudy\WWW\teebphp\app\Http\AdminController.php(25): App\Component\Blade\BladeManager->__call('make', Array) #8 D:\phpStudy\WWW\teebphp\app\Http\AdminController.php(15): App\Http\AdminController->actionBlade() #9 [internal function]: App\Http\AdminController->actionIndex(Object(Teeb\Http\Request)) #10 D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Http\Controller.php(72): call_user_func_array(Array, Array) #11 D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Http\Controller.php(49): Teeb\Http\Controller->runAction('index') #12 D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Http\Core.php(73): Teeb\Http\Controller->run('index', Array) #13 D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Http\Core.php(56): Teeb\Http\Core->handleRequest('admin', Array) #14 D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Http\Core.php(45): Teeb\Http\Core->handle() #15 D:\phpStudy\WWW\teebphp\public\index.php(7): Teeb\Http\Core->run() #16 {main} [] in D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Log\Logger.php:72 in D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Log\Logger.php:62 in D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Log\Logger.php:37 in D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Log\Log.php:11 in D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Exception\Handler.php:27 in D:\phpStudy\WWW\teebphp\vendor\master_who\teebframework\src\Teeb\Bootstrap\HandleException.php:44

    opened by xianzhi0520 0
  • Using {{ }} to echo string not encode utf8

    Using {{ }} to echo string not encode utf8

    I have array: $arr = ['title' => 'Tiêu đề'] I use {{}} tag to echo string in array, but it not encode to utf8 {{$arr['title']}} //=> Ti&ecirc;u đề

    opened by phamvansy 0
Releases(5.4.3)
Owner
刘小乐
PHP,Golang,Linux
刘小乐
A ready-to-use Model View Controller template in PHP

PHP-MVC-Template A ready-to-use Model View Controller template in PHP Use this repo as a template! (Or clone it) Start to configure your MVC file Afte

Loule | Louis 20 Dec 26, 2022
PHP template engine for native PHP templates

FOIL PHP template engine, for PHP templates. Foil brings all the flexibility and power of modern template engines to native PHP templates. Write simpl

Foil PHP 167 Dec 3, 2022
Smarty is a template engine for PHP, facilitating the separation of presentation (HTML/CSS) from application logic.

Smarty 3 template engine smarty.net Documentation For documentation see www.smarty.net/docs/en/ Requirements Smarty can be run with PHP 5.2 to PHP 7.4

Smarty PHP Template Engine 2.1k Jan 1, 2023
☕ Latte: the intuitive and fast template engine for those who want the most secure PHP sites.

Latte: amazing template engine for PHP Introduction Latte is a template engine for PHP which eases your work and ensures the output is protected again

Nette Foundation 898 Dec 25, 2022
⚡️ Simple and fastly template engine for PHP

EasyTpl ⚡️ Simple and fastly template engine for PHP Features It's simple, lightweight and fastly. No learning costs, syntax like PHP template It is s

PHPPkg 19 Dec 9, 2022
Twig Template Engine to Phalcon PHP

Twig Template Engine to Phalcon PHP

Vinicius 4 Oct 7, 2022
Liquid template engine for PHP

Liquid is a PHP port of the Liquid template engine for Ruby, which was written by Tobias Lutke. Although there are many other templating engines for PHP, including Smarty (from which Liquid was partially inspired)

Harald Hanek 230 Aug 18, 2022
Pug (Jade) template engine for Symfony

Pug-Symfony Pug template engine for Symfony This is the documentation for the ongoing version 3.0. Click here to load the documentation for 2.8 Instal

Pug PHP 41 Dec 16, 2022
PHPlater, a simple template engine.

PHPlater A simple PHP template engine that lets PHP do all the logic and then append it to the HTML in the template file. It is set to solve the probl

John Larsen 2 Jun 3, 2022
A template abstraction prototype for PHP template engines

Schranz Templating A template abstraction prototype for PHP template engines. This project should help to find a way for a general Template Render Int

Schranz Templating 16 Dec 7, 2022
SwitchBlade: Custom Directives for the Laravel Blade templating engine

SwitchBlade: Custom Directives for the Laravel Blade templating engine

Awkward Ideas 10 Nov 29, 2022
Standalone Skeltch templating engine for PHP

SkeltchGo is a standalone version of Glowie Skeltch templating engine for PHP, intented to use from outside the framework.

glowie 1 Nov 5, 2021
Experimental ActiveRecord layer on top of Doctrine2 using the Twig templating engine

This is an experiment for building ActiveRecord functionality on top of Doctrine2 using the Twig templating engine. Whether it is called Propel2 or not is irrelevant.

Francois Zaninotto 85 Dec 5, 2022
A PHP project template with PHP 8.1, Laminas Framework and Doctrine

A PHP project template with PHP 8.1, Laminas Framework and Doctrine

Henrik Thesing 3 Mar 8, 2022
Laravel package template

REPLACE Simple and flexible package template. Usage Replace all occurances of REPLACE (case sensitive) with the name of the package namespace. E.g. th

ARCHTECH 56 Aug 15, 2022
Document templates Laravel package is intended for creating/managing user editable document template

Document Templates Introduction Document templates Laravel package is intended for creating/managing user editable document templates, with ability to

42coders 139 Dec 15, 2022
Twig, the flexible, fast, and secure template language for PHP

Twig, the flexible, fast, and secure template language for PHP Twig is a template language for PHP, released under the new BSD license (code and docum

Twig 7.7k Jan 1, 2023
Native PHP template system

Plates Plates is a native PHP template system that's fast, easy to use and easy to extend. It's inspired by the excellent Twig template engine and str

The League of Extraordinary Packages 1.3k Jan 7, 2023
A complete and fully-functional implementation of the Jade template language for PHP

Tale Jade for PHP Finally a fully-functional, complete and clean port of the Jade language to PHP — Abraham Lincoln The Tale Jade Template Engine brin

Talesoft 91 Dec 27, 2022