aios / laravel-shortcodes

此包已被 废弃 并不再维护。作者建议使用 webwizo/laravel-shortcodes 包。

Laravel 5, 6, 7 和 8 的类似 WordPress 的短代码

v2.0.0 2020-09-10 09:32 UTC

This package is auto-updated.

Last update: 2020-09-18 01:49:13 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,

您可以使用外观(facade)来缩短代码。将此添加到您的别名中

'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)。请参阅许可文件以获取更多信息。