drinky78 / shortcode-bundle
提供Twig过滤器以支持类似WordPress的短代码的软件包。
dev-master
2014-10-28 11:43 UTC
Requires
- php: >=5.3.3
- symfony/framework-bundle: >=2.2
This package is not auto-updated.
Last update: 2024-09-24 08:46:23 UTC
README
提供Twig过滤器以支持类似WordPress的短代码的软件包。
重要:这是一个早期原型!
目前支持以下形式的标签
[demo]
(简单标签)[demo var=xxx var2=yyy]
(带有未引用属性的标签)
目前不支持的是
[demo]...[/demo]
(带有嵌入内容的标签)[demo var="xxx"]
(带有引用属性的标签)
###安装
在您的composer.json中添加ShortcodeBundle
{
"require": {
"drinky78/shortcode-bundle": "dev-master"
}
}
在AppKernel中注册此软件包
<?php
// app/AppKernel.php
public function registerBundles()
{
$bundles = array(
// ...
new MW\Bundle\ShortcodeBundle\MWShortcodeBundle(),
);
}
##如何使用
###1. 创建一个新的短代码处理器
<?php #MyProject\Bundle\TestBundle\Shortcode\DemoShortcode.php namespace MyProject\Bundle\TestBundle\Shortcode; use MW\Bundle\ShortcodeBundle\Shortcode\BaseShortcode; class DemoShortcode extends BaseShortcode { public function parse($options) { // TODO: Render your content return 'Shortcode content'; } }
###2. 将短代码定义为服务(别名将是您的短代码名称)
<service id="myproject.shortcode.demo" class="MyProject\Bundle\TestBundle\Shortcode\DemoShortcode"> <tag name="mw.shortcode" alias="demo" /> </service>
YML示例
parameters:
myproject.shortcode.demo: MyProject\Bundle\TestBundle\Shortcode\DemoShortcode
services:
myproject.shortcode.demo:
class: %myproject.shortcode.demo%
tags:
- { name: mw.shortcode, alias: demo }
###3. 在您的Twig模板中使用它
<div>{{ page.content|shortcodes }}</div>