Skip to content

danielsum/shortcode

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Shortcode

Build Status Latest Stable Version Total Downloads Latest Unstable Version License Bitdeli Badge

Server Requirements

  • PHP 5.4.0 or higher.

Installation

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

  "pingpong/shortcode": "1.0.*" 

Next, open a terminal and run.

  composer update 

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

  'Pingpong\Shortcode\ShortcodeServiceProvider'

Add new Facade alias.

'Shortcode'       => 'Pingpong\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);

Unregister The Specified Shortcode

Shortcode::unregister('img');

Destroy All Shortcodes

Shortcode::destroy();

License

This package is open-sourced software licensed under The BSD 3-Clause License

This package also adopted a system of wordpress shortcodes. Please see or read the license of wordpress also here.

About

Laravel Shortcode

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 100.0%