hasanrazahasrat / laravel-shortcodes
Laravel 5, 6, 7, 8 和 9 的类似 WordPress 的短代码
Requires
- php: ^7.2|^8.0
- illuminate/contracts: 5.6.x|5.7.x|5.8.x|^6.0|^7.0|^8.0|^9.0
- illuminate/support: 5.6.x|5.7.x|5.8.x|^6.0|^7.0|^8.0|^9.0
- illuminate/view: 5.6.x|5.7.x|5.8.x|^6.0|^7.0|^8.0|^9.0
Requires (Dev)
- orchestra/testbench: ^3.1|^7.0
- phpunit/phpunit: ^7.5|^8.0|^9.5
- scrutinizer/ocular: ^1.8.1
- squizlabs/php_codesniffer: ~2.3|^3.6
README
Laravel 5.x 的类似 WordPress 的短代码
[b class="bold"]Bold text[/b] [tabs] [tab]Tab 1[/tab] [tab]Tab 2[/tab] [/tabs] [user id="1" display="name"]
如果您正在寻找 Laravel 4.2,请参阅:https://github.com/patrickbrouwers/Laravel-Shortcodes
安装
通过 Composer
$ composer require "webwizo/laravel-shortcodes:1.0.*"
更新 composer 后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组
用法
Webwizo\Shortcodes\ShortcodesServiceProvider::class,
您可以使用外观以缩短代码。将以下内容添加到您的别名
'Shortcode' => Webwizo\Shortcodes\Facades\Shortcode::class,
该类绑定到 ioC 作为 shortcode
$shortcode = app('shortcode');
用法
withShortcodes()
以启用视图编译功能
return view('view')->withShortcodes();
这将仅为此视图启用短代码渲染。
通过类启用
Shortcode::enable();
通过类禁用
Shortcode::disable();
禁用某些视图的短代码编译
将配置设置为 true,您可以按视图禁用编译。
return view('view')->withoutShortcodes();
默认编译
要使用默认编译
Shortcode::compile($contents);
从渲染的视图中删除短代码。
return view('view')->withStripShortcodes();
通过类删除短代码
Shortcode::strip($contents);
注册新短代码
创建一个新的 ServiceProvider,您可以在其中注册所有短代码。
php artisan make:provider ShortcodesServiceProvider
定义短代码后,将 ServiceProvider 添加到 config/app.php 中的 providers 数组
用法
App\Providers\ShortcodesServiceProvider::class,
回调
您可以使用回调在 ShortcodesServiceProvider 中注册短代码
php artisan make:provider ShortcodesServiceProvider
ShortcodesServiceProvider.php 类文件
<?php namespace App\Providers; use App\Shortcodes\BoldShortcode; use Illuminate\Support\ServiceProvider; use Shortcode; class ShortcodesServiceProvider extends ServiceProvider { /** * Bootstrap the application services. * * @return void */ public function boot() { // } /** * Register the application services. * * @return void */ public function register() { Shortcode::register('b', BoldShortcode::class); Shortcode::register('i', 'App\Shortcodes\ItalicShortcode@custom'); } }
为 BoldShortcode 定义的默认类
您可以在每个类 app/Shortcodes/BoldShortcode.php 中存储每个短代码
namespace App\Shortcodes; class BoldShortcode { public function register($shortcode, $content, $compiler, $name, $viewData) { return sprintf('<strong class="%s">%s</strong>', $shortcode->class, $content); } }
具有自定义方法的类
您可以在每个类 app/Shortcodes/ItalicShortcode.php 中存储每个短代码
namespace App\Shortcodes; class ItalicShortcode { public function custom($shortcode, $content, $compiler, $name, $viewData) { return sprintf('<i class="%s">%s</i>', $shortcode->class, $content); } }
注册助手函数
如果您只想在短代码中提供属性时显示 HTML 属性,您可以使用 $shortcode->get($attributeKey, $fallbackValue = null)
class BoldShortcode { public function register($shortcode, $content, $compiler, $name, $viewData) { return '<strong '. $shortcode->get('class', 'default') .'>' . $content . '</strong>'; } }
变更日志
有关最近更改的更多信息,请参阅 CHANGELOG
贡献
有关详细信息,请参阅 CONTRIBUTING 和 CONDUCT
安全
如果您发现任何与安全相关的问题,请通过电子邮件 webwizo@gmail.com 而不是使用问题跟踪器来报告。
鸣谢
支持我
许可
MIT 许可证 (MIT)。有关更多信息,请参阅 许可文件