Installation Add the bundle via Composer:
composer require drinky78/shortcode-bundle:dev-master
Register the bundle in AppKernel.php:
new MW\Bundle\ShortcodeBundle\MWShortcodeBundle(),
First Use Case Create a simple shortcode handler:
php app/console generate:bundle --namespace=MyProject/TestBundle --format=yml --dir=src
Then create a shortcode class:
// src/MyProject/TestBundle/Shortcode/DemoShortcode.php
namespace MyProject\TestBundle\Shortcode;
use MW\Bundle\ShortcodeBundle\Shortcode\BaseShortcode;
class DemoShortcode extends BaseShortcode {
public function parse($options) {
return 'Hello, Shortcode!';
}
}
Service Configuration
Define the shortcode as a service in Resources/config/services.yml:
services:
myproject.shortcode.demo:
class: MyProject\TestBundle\Shortcode\DemoShortcode
tags:
- { name: mw.shortcode, alias: demo }
Usage in Twig Use the shortcode in your Twig templates:
{{ shortcode('demo') }}
Shortcode Registration
mw.shortcode tag.Bootstrap class or event subscriber:
$container->loadFromExtension('mw_shortcode', [
'shortcodes' => [
'demo' => 'myproject.shortcode.demo',
],
]);
Handling Shortcode Options
Parse and validate options in the parse() method:
public function parse($options) {
$name = $options['name'] ?? 'Guest';
return "Hello, {$name}!";
}
Use in Twig:
{{ shortcode('demo', { 'name': 'John' }) }}
Integration with Content Management
$shortcodes = $em->getRepository('MyProjectTestBundle:Shortcode')->findAll();
foreach ($shortcodes as $shortcode) {
$container->loadFromExtension('mw_shortcode', [
'shortcodes' => [$shortcode->getAlias() => $shortcode->getServiceId()],
]);
}
Reusable Shortcode Components
Extend BaseShortcode to create reusable components:
class ButtonShortcode extends BaseShortcode {
public function parse($options) {
$url = $options['url'] ?? '#';
$text = $options['text'] ?? 'Click Me';
return "<a href=\"{$url}\">{$text}</a>";
}
}
Early Prototype Limitations
var="value" in shortcodes; only unquoted attributes are supported.[demo]...[/demo] are not supported; use only self-closing tags like [demo].Service Configuration Issues
mw.shortcode (case-sensitive).Debugging Tips
mw_shortcode.list command to verify registered shortcodes:
php app/console mw_shortcode:list
parse() method to trace execution:
public function parse($options) {
\Log::debug('Shortcode options:', $options);
return 'Content';
}
Performance Considerations
public function parse($options) {
$cacheKey = md5(serialize($options));
if ($cache = $this->get('cache')->get($cacheKey)) {
return $cache;
}
$content = 'Dynamic content';
$this->get('cache')->set($cacheKey, $content, 3600);
return $content;
}
Extension Points
$dispatcher->addListener('mw_shortcode.parse', function($event) {
$event->setContent('Modified: ' . $event->getContent());
});
Testing
public function testParseShortcode() {
$shortcode = new DemoShortcode();
$this->assertEquals('Hello, John!', $shortcode->parse(['name' => 'John']));
}
TwigTestCase.How can I help you explore Laravel packages today?