synergitech/twigbridge
Composer 安装命令:
composer require synergitech/twigbridge
包简介
Adds the power of Twig to Laravel
README 文档
README
this is a fork that has been updated with various fixes, it will be kept up to date with any changes from upstream
Allows you to use Twig seamlessly in Laravel (>= 6).
If you need to use an older version of Laravel, have a look at the original rcrowe/TwigBridge
Installation
composer require synergitech/twigbridge
Laravel should automatically detect the service provider and facade but you should add the config file with artisan:
Laravel automatically registers the Service Provider. Use Artisan to publish the twig config file:
php artisan vendor:publish --provider="TwigBridge\ServiceProvider"
Installation on Lumen
For Lumen, you need to load the same Service Provider but you have to disable the Auth, Translator, and Url extensions in your local configuration.
Copy the config/twigbridge.php file to your local config folder and register the configuration and Service Provider in bootstrap/app.php:
$app->configure('twigbridge'); $app->register('TwigBridge\ServiceProvider');
Usage
At this point you can now begin using twig like you would any other view
//app/Http/routes.php //twig template resources/views/hello.twig Route::get('/', function () { return View::make('hello'); });
You can create the twig files in resources/views with the .twig file extension.
You call the Twig template like you would any other view:
// Without the file extension View::make('i_am_twig', [...])
TwigBridge also supports views in other packages:
View::make('pagination::simple')
The above rules continue when extending another Twig template:
{% extends "parent" %}
{% extends "pagination::parent" %}
You can call functions with parameters:
{{ link_to_route('tasks.edit', 'Edit', task.id, {'class': 'btn btn-primary'}) }}
And output variables, escaped by default. Use the raw filter to skip escaping.
{{ some_var }}
{{ html_var | raw }}
{{ long_var | str_limit(50) }}
Extensions
Sometimes you want to extend / add new functions for use in Twig templates. Add to the enabled array in config/twigbridge.php a list of extensions for Twig to load.
TwigBridge supports both a string or a closure as a callback, so for example you might implement the Assetic Twig extension as follows:
'enabled' => [ function ($app) { $factory = new Assetic\Factory\AssetFactory($app['path'] . '/../some/path/'); $factory->setDebug(false); // etc..... return new Assetic\Extension\Twig\AsseticExtension($factory); } ]
TwigBridge comes with the following extensions enabled by default:
- Twig\Extension\DebugExtension
- TwigBridge\Extension\Laravel\Auth
- TwigBridge\Extension\Laravel\Config
- TwigBridge\Extension\Laravel\Dump
- TwigBridge\Extension\Laravel\Form
- TwigBridge\Extension\Laravel\Gate
- TwigBridge\Extension\Laravel\Html
- TwigBridge\Extension\Laravel\Input
- TwigBridge\Extension\Laravel\Session
- TwigBridge\Extension\Laravel\String
- TwigBridge\Extension\Laravel\Translator
- TwigBridge\Extension\Laravel\Url
- TwigBridge\Extension\Loader\Facades
- TwigBridge\Extension\Loader\Filters
- TwigBridge\Extension\Loader\Functions
To enable '0.5.x' style Facades, enable the Legacy Facades extension:
- TwigBridge\Extension\Laravel\Legacy\Facades
FilterLoader and FunctionLoader
These loader extensions exposes Laravel helpers as both Twig functions and filters.
Check out the config/twigbridge.php file to see a list of defined function / filters. You can also add your own.
FacadeLoader
The FacadeLoader extension allows you to call any facade you have configured in config/twigbridge.php. This gives your Twig templates integration with any Laravel class as well as any other classes you alias.
To use the Laravel integration (or indeed any aliased class and method), just add your facades to the config and call them like URL.to(link) (instead of URL::to($link))
Functions/Filters/Variables
The following helpers/filters are added by the default Extensions. They are based on the helpers and/or facades, so should be self explaining.
Functions:
- asset, action, url, route, secure_url, secure_asset
- auth_check, auth_guest, auth_user
- can
- config_get, config_has
- dump
- form_* (All the Form::* methods, snake_cased)
- html_* (All the Html::* methods, snake_cased)
- input_get, input_old, input_has
- link_to, link_to_asset, link_to_route, link_to_action
- session_has, session_get, csrf_token, csrf_field, method_field
- str_* (All the Str::* methods, snake_cased)
- trans, trans_choice
- url_* (All the URL::* methods, snake_cased)
Filters:
- camel_case, snake_case, studly_case
- str_* (All the Str::* methods, snake_cased)
- trans, trans_choice
Global variables:
- app: the Illuminate\Foundation\Application object
- errors: The $errors MessageBag from the Validator (always available)
Artisan Commands
TwigBridge offers commands for CLI Interaction.
Empty the Twig cache:
php artisan twig:clean
Lint all Twig templates:
php artisan twig:lint
synergitech/twigbridge 适用场景与选型建议
synergitech/twigbridge 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 24.93k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2018 年 12 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「twig」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 synergitech/twigbridge 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 synergitech/twigbridge 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 synergitech/twigbridge 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
This Symfony bundle integrates PhpSpreadsheet into Symfony using Twig.
A Twig extension to insert css as inline styles with a tag
Caching and compression for Twig assets (JavaScript and CSS).
Twig extensions for Tracy Debugger
Provides code-quality helpers to your Twig templates.
统计信息
- 总下载量: 24.93k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 14
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2018-12-18