artygrand / shortcode
无需正则表达式的简单短代码解析器
v1.0.0
2017-02-12 13:18 UTC
Requires
- php: >=5.3.0
This package is not auto-updated.
Last update: 2024-09-14 18:31:35 UTC
README
无需正则表达式的简单短代码解析器
- 无需正则表达式
- 无限层级嵌套短代码
- 具有别名
- 具有显示短代码而非编译它的能力
- 具有预定义参数
安装
使用composer安装
composer require artygrand/shortcode
需要PHP 5.3或更高版本。
使用方法
以下是一个基本使用示例
<?php require __DIR__.'/vendor/autoload.php'; // or // include 'path/to/Shortcode.php'; $sample = ' [shortcode] [short argument="value"] [shortcode active other="complex value"] [short]content[/short] [shortcode argument="value"]content[/shortcode] [span6] [span6] [span6 ]Lorem[/span6] [span4] [/span6] [span4] [span6 class="first" ]dolor[/span6] [col6 class="last" ]cudere[/col6] [/span4] [/span6] [span6] @[span6] [span6]Lorem[/span6] [/span6] [/span6] '; $sc = artygrand\Shortcode::getInstance(); $sc ->add('shortcode', 'func_shortcode') ->withAlias('short') ->add('span4', array( 'grid', array('cols' => 4) )) ->add('span6', array( 'grid', array('cols' => 6) )) ->withAlias('col6'); echo $sc->compile($sample); // functions function func_shortcode($args, $content = null){ if (is_null($content)){ return print_r($args, true); } $args['content'] = $content; return print_r($args, true); } function grid($args, $content = null){ $args = array_merge(array( 'cols' => 1, 'class' => '', ), $args); $class = $args['class'] ? ' class="' . $args['class'] . '"' : ''; if (is_null($content)){ return '<div style="width:' . ($args['cols']/0.12) .'%;"' . $class . '>NoContent</div>'; } return '<div style="width:' . ($args['cols']/0.12) .'%;"' . $class . '>' . $content . '</div>'; }
最终结果将像这样
Array
(
)
Array
(
[argument] => value
)
Array
(
[active] => 1
[other] => complex value
)
Array
(
[content] => content
)
Array
(
[argument] => value
[content] => content
)
<div style="width:50%;">
<div style="width:50%;">
<div style="width:50%;">Lorem</div>
<div style="width:33.333333333333%;">NoContent</div>
</div>
<div style="width:33.333333333333%;">
<div style="width:50%;" class="first">dolor</div>
<div style="width:50%;" class="last">cudere</div>
</div>
</div>
<div style="width:50%;">
[span6]
<div style="width:50%;">Lorem</div>
[/span6]
</div>
许可证
在MIT许可证下发布。