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.

Issues
  • 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('/\[email protected](@?\w+)([ \t]*)(\( ( (?>[^()]+) | (?3) )* \))?/x', $callback, $value);
    

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

    opened by Excas 3
  • 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
  • 	component & slot 支持

    component & slot 支持

    opened by terranc 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
  • fix issue with loop variable in nested loop

    fix issue with loop variable in nested loop

    • fix issue with loop variable incorrect when use nested loop
    opened by salkhwlani 2
  • 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
  • Does not work

    Does not work "directive"?

    @datetime(strtotime()) not work?

    opened by netdragoon 2
  • component & slot 支持

    component & slot 支持

    opened by terranc 2
  • 关键字扩展(extending-blade)考虑加进来吗

    关键字扩展(extending-blade)考虑加进来吗

    觉得这些扩展方法放到模板, 以 plugin 的方式存在, 比直接调用框架/系统更加合理

    https://laravel.com/docs/5.4/blade#extending-blade

    opened by fengqi 2
  • 你好, 请教一个关于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
  • 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)
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 6 Nov 19, 2021
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 163 Nov 20, 2021
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 1.9k Nov 23, 2021
☕ 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 731 Nov 27, 2021
⚡️ 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 9 Nov 26, 2021
SwitchBlade: Custom Directives for the Laravel Blade templating engine

SwitchBlade: Custom Directives for the Laravel Blade templating engine

Awkward Ideas 9 Sep 2, 2021
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
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 47 Nov 24, 2021
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 99 Nov 14, 2021
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.3k Nov 24, 2021
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 Nov 19, 2021
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 93 May 4, 2021
Provides a GitHub repository template for a PHP package, using GitHub actions.

php-package-template Installation ?? This is a great place for showing how to install the package, see below: Run $ composer require ergebnis/php-pack

null 260 Nov 24, 2021
The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS! Some information before we start This repo is only code related, to a

Ilian 5 Nov 3, 2021
The free-to-use template for your Imagehost-website made with PHP, HTML and CSS!

The free-to-use template for your Imagehost-website made with PHP, HTML and CSS! Some information before we start This repo is only code related, to a

Ilian 5 Nov 3, 2021
A lightweight template parser used by PyroCMS.

Lex Lex is a lightweight template parser. Lex is released under the MIT License and is Copyrighted 2011 - 2014 PyroCMS Team. Change Log 2.3.2 Convert

PyroCMS 103 Nov 15, 2021
The Templating component provides all the tools needed to build any kind of template system.

Templating Component The Templating component provides all the tools needed to build any kind of template system. It provides an infrastructure to loa

Symfony 982 Nov 23, 2021
A SilverStripe Module with template methods to quickly make use of FocusPoint, LazySizes, and Object-fit

LazyFocusFit A SilverStripe module with template methods to quickly make use of FocusPoint, LazySizes and object-fit. Requirements PHP FocusPoint JS/C

Evans Hunt 8 Oct 12, 2021
Astroid Framework - Powerful Joomla Template Framework

Powerful framework for designers and developers to create responsive, fast & robust Joomla based websites and templates.

TemPlaza 9 Nov 23, 2021