Yii-shortcode is a component for the Yii PHP framework that provides ability to create WordPress like shortcodes.
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
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
$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