8000 GitHub - tahiryasin/yii-shortcode: Create WordPress like shortcodes in Yii Framework
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

tahiryasin/yii-shortcode

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

yii-shortcode

Yii-shortcode is a component for the Yii PHP framework that provides ability to create WordPress like shortcodes.

Usage

Setup

Download the latest release from Yii extensions.

Unzip the component under protected/components and add the following to your application config:

return array(
  'components' => array(
    'shortcode' => array(
        'class' => 'application.components.ShortCode',
     ),
  ...
  ...
  ),
);

protected/config/main.php

Registring Shortcodes

You can register as many shortcodes as you want in base controller or in any custom controller:

public function beforeAction($action)
  {
      Yii::app()->shortcode->add_shortcode('gallery', array($this, 'gallery_callback'));
	  Yii::app()->shortcode->add_shortcode('caption', array($this, 'caption_callback'));
	  return $action;
  }

protected/components/Controller.php

function gallery_callback($atts)
  {
      return "<div id='gallery'>Gallery #{$atts['id']}</div>";
  }  
function caption_callback($atts, $content)
  {
      return '<span class="caption">' . $content . '</span>';
  }  

protected/controllers/SiteController.php

Rendering ShortCode

$content = 'My Gallery: [gallery id="123"]';
echo Yii::app()->shortcode->parse($content);

The output would be: My Gallery: <div id="gallery">Gallery #123</div>

$content= 'Hi, check out this caption: [caption]My Caption[/caption]';
echo Yii::app()->shortcode->parse($content);

The output would be: Hi, check out this caption: <span class="caption">My Caption</span>

protected/views/site/index.php

About

Create WordPress like shortcodes in Yii Framework

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

0