tahiryasin / yii-shortcode
Yii component that enables you to create WordPress like shortcodes.
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2025-04-24 22:19:50 UTC
README
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