tahiryasin / yii-shortcode
Yii 组件,使您能够创建类似 WordPress 的简码。
dev-master
2017-03-10 18:54 UTC
Requires
- php: >=5.4.0
This package is not auto-updated.
Last update: 2024-09-26 19:45:16 UTC
README
Yii-shortcode 是一个为 Yii PHP 框架 提供创建类似 WordPress 简码功能的组件。
用法
设置
从 Yii 扩展 下载最新版本。
解压缩组件到 protected/components 目录下,并在您的应用程序配置中添加以下内容
return array( 'components' => array( 'shortcode' => array( 'class' => 'application.components.ShortCode', ), ... ... ), );
protected/config/main.php
注册简码
您可以在基本控制器或任何自定义控制器中注册任意数量的简码
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);
输出结果: 我的画廊:<div id="gallery">画廊 #123</div>
$content= 'Hi, check out this caption: [caption]My Caption[/caption]'; echo Yii::app()->shortcode->parse($content);
输出结果: 嗨,看看这个标题:<span class="caption">我的标题</span>
protected/views/site/index.php