walter silva cruz/laravel-shortcodes

Laravel 5、6、7、8、9、10 和 11 的类似 WordPress 的短代码(webwizo/laravel-shortcodes 的分支)

v1.0.22 2024-08-21 14:25 UTC

README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads StyleCI

为 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

贡献

有关详细信息,请参阅 CONTRIBUTINGCONDUCT

安全

如果您发现任何安全问题,请通过电子邮件 webwizo@gmail.com 而不是使用问题跟踪器

鸣谢

支持我

Buy Me A Coffee

许可证

MIT 许可证(MIT)。有关更多信息,请参阅 许可证文件