brouwers/shortcodes

Laravel 4.2 的 WordPress 样式短代码

1.0.2 2014-12-06 18:45 UTC

This package is not auto-updated.

Last update: 2024-09-24 02:43:42 UTC


README

WordPress 样式短代码,适用于 Laravel 4.2。

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

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

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

如果您正在寻找 BBcodes,请参阅:https://github.com/patrickbrouwers/Laravel-BBcodes

Build Status Latest Stable Version Total Downloads License Monthly Downloads Daily Downloads

安装

在您的 composer.json 中要求此包并更新 composer。

"brouwers/shortcodes": "1.*"

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

'Brouwers\Shortcodes\ShortcodesServiceProvider',

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

'Shortcode' => 'Brouwers\Shortcodes\Facades\Shortcode',

类绑定到 ioC 的 shortcode

$shortcode = App::make('shortcode');

使用方法

视图编译

默认情况下,短代码编译在配置中设置为 false。

withShortcodes()

要启用视图编译功能

return View::make('view')->withShortcodes();

这将只为该视图启用短代码渲染。

配置

通过配置 shortcodes::enabled 启用短代码,将为所有视图启用短代码渲染。

通过类启用

Shortcode::enable();

通过类禁用

Shortcode::disable();

禁用某些视图的短代码编译

将配置设置为 true,您可以禁用每个视图的编译。

return View::make('view')->withoutShortcodes();

默认编译

要使用默认编译

Shortcode::compile($contents);

注册新的短代码

在文件或 ServiceProvider 中注册短代码。(例如 app/start/shortcodes.phpApp/Services/ShortcodeServiceProvider.php

回调

短代码可以像 Laravel 宏一样使用回调注册

Shortcode::register('b', function($shortcode, $content, $compiler, $name)
{
  return '<strong class="'. $shortcode->class .'">' . $content . '</strong>';
});
  

默认类

class BoldShortcode {

  public function register($shortcode, $content, $compiler, $name)
  {
    return '<strong class="'. $shortcode->class .'">' . $content . '</strong>';
  }
}

Shortcode::register('b', 'BoldShortcode');

具有自定义方法的类

class BoldShortcode {

  public function custom($shortcode, $content, $compiler, $name)
  {
    return '<strong class="'. $shortcode->class .'">' . $content . '</strong>';
  }
}

Shortcode::register('b', 'BoldShortcode@custom');

注册助手

如果您只想在短代码提供属性时显示 html 属性,您可以使用 $shortcode->get($attributeKey, $fallbackValue = null)

class BoldShortcode {

  public function register($shortcode, $content, $compiler, $name)
  {
    return '<strong '. $shortcode->get('class', 'default') .'>' . $content . '</strong>';
  }
}