Wallogit.com
2017 © Pedro Peláez
Yii-shortcode is a component for the Yii PHP framework that provides ability to create WordPress like shortcodes., (*1)
Download the latest release from Yii extensions., (*2)
Unzip the component under protected/components and add the following to your application config:, (*3)
return array(
'components' => array(
'shortcode' => array(
'class' => 'application.components.ShortCode',
),
...
...
),
);
protected/config/main.php, (*4)
You can register as many shortcodes as you want in base controller or in any custom controller:, (*5)
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, (*6)
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, (*7)
$content = 'My Gallery: [gallery id="123"]'; echo Yii::app()->shortcode->parse($content);
The output would be:
My Gallery: <div id="gallery">Gallery #123</div>, (*8)
$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>, (*9)
protected/views/site/index.php, (*10)