ps24love/shortcode

There is no license information available for the latest version (dev-master) of this package.

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

This package is not auto-updated.

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


README

Installation

Open your composer.json file, and add the new required package.

  "ps24love/shortcode": "dev-master"

Next, open a terminal and run.

  composer update 

After the composer updated. Add new service provider in app/config/app.php.

  'ps24love\Shortcode\ShortcodeServiceProvider'

Add new Facade alias.

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

Done.

Registering Shorcode

Using closure:

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

Using class name.

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');

Using class name with the specified method.

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');

Using callback array.

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'));

Using function name.

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

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

Compile

$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);

License

This package is open-sourced software licensed under the MIT license