ps24love/shortcode

此包最新版本(dev-master)没有可用的许可信息。

dev-master 2014-06-02 00:16 UTC

This package is not auto-updated.

Last update: 2024-09-24 06:55:34 UTC


README

安装

打开你的 composer.json 文件,并添加新的必需包。

  "ps24love/shortcode": "dev-master"

接下来,打开终端并运行。

  composer update 

composer 更新后。在 app/config/app.php 中添加新的服务提供者。

  'ps24love\Shortcode\ShortcodeServiceProvider'

添加新的 Facade 别名。

'Shortcode'       => 'ps24love\Shortcode\Facades\Shortcode',

完成。

注册 Shortcode

使用闭包

Shortcode::register('a', function($attr, $content = null, $name = null)
{
	$text = Shortcode::compile($content);
	return '<a'.HTML::attributes($attr).'>'. $text .'</a>';
});

使用类名。

class DivShortcode
{
  public function register($attr, $content = null, $name = null)
  {
  	$text = Shortcode::compile($content);
  	return '<div'.HTML::attributes($attr).'>'. $text .'</div>';
  }
}

Shortcode::register('div', 'DivShortcode');

使用指定方法的类名。

class HTMLShortcode
{
  public function img($attr, $content = null, $name = null)
  {
    $src = array_get($attr, 'src');
  	$text = Shortcode::compile($content);
  	return '<img src="'.$src.'" '.HTML::attributes($attr).'/>';
  }
}


Shortcode::register('img', 'HTMLShortcode@img');

使用回调数组。

class SpanShortcode
{
  
  public function div($attr, $content = null, $name = null)
  {
  	$text = Shortcode::compile($content);
  	return '<span'.HTML::attributes($attr).'>'. $text .'</span>';
  }
}

Shortcode::register('span', array('SpanShortcode', 'span'));

使用函数名。

function smallTag($attr, $content = null, $name = null)
{
	$text = Shortcode::compile($content);
	return '<small'.HTML::attributes($attr).'>'. $text .'</small>';
}

Shortcode::register('small', 'smallTag');

编译

$text = '[a href="#"]Click here[/a]';
echo Shortcode::compile($text);

$text = '
[a href="#"]
 [img src="http://placehold.it/140x140"]
 [small]This is small text[/small]
[/a]
';
echo Shortcode::compile($text);

许可

此包是开源软件,受MIT 许可协议许可。