uzwebline/laravel_shortcodes

Laravel 5.x的WordPress-like短代码

1.0.0 2018-06-28 17:45 UTC

This package is not auto-updated.

Last update: 2024-09-29 06:07:03 UTC


README

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

基于webwizo/laravel-shortcodes的WordPress-like短代码,适用于Laravel 5.x,查看:https://github.com/webwizo/laravel-shortcodes

[b class="bold"]Bold text.[/b]

[tabs]
  [tab]Tab 1[/tab]
  [tab]Tab 2[/tab]
[/tabs]

[user id="1" display="name"]

安装

通过Composer

$ composer require "uzwebline/laravel_shortcodes:1.0.*"

更新composer后,将ServiceProvider添加到config/app.php中的providers数组中

用法

Uzwebline\Shortcodes\ShortcodesServiceProvider::class,

您可以使用外观以缩短代码。将以下内容添加到您的别名中

'Shortcode' => Uzwebline\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

安全

如果您发现任何与安全相关的问题,请通过电子邮件umidjonsmail@gmail.com联系,而不是使用问题跟踪器。

鸣谢

许可

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