Developed with love by KnpLabs Hire us for your project!
17

MWShortcodeBundle

by mweimerskirch

Bundle that provides a Twig filter to support WordPress-like shortcodes.

Important: It's an early prototype!

Currently supported are tags of this form:
* [demo] (simple tags)
* [demo var=xxx var2=yyy] (tags with unquoted attributes)

Not yet supported are:
* [demo]...[/demo] (tags with embedded content)
* [demo var="xxx"] (tags with quoted attributes)

Installation

Add MWShortcodeBundle in your composer.json:

{
    "require": {
        "mw/shortcode-bundle": "dev-master"
    }
}

Register the bundle in AppKernel

<?php
// app/AppKernel.php

public function registerBundles()
{
    $bundles = array(
        // ...
        new MW\Bundle\ShortcodeBundle\MWShortcodeBundle(),
    );
}

How to use:

1. Create a new shortcode handler

<?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. Define the shortcode as a service (the alias will be the name of your shortcode):

<service id="myproject.shortcode.demo" class="MyProject\Bundle\TestBundle\Shortcode\DemoShortcode">
    <tag name="mw.shortcode" alias="demo" />
</service>

YML example

parameters:
    myproject.shortcode.demo: MyProject\Bundle\TestBundle\Shortcode\DemoShortcode

services:
    myproject.shortcode.demo:
        class: %myproject.shortcode.demo%
        tags:
            - { name: mw.shortcode, alias: demo }

3. Use it in your Twig templates

<div>{{ page.content|shortcodes }}</div>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  • Merge pull request #5 from wiredmedia/master
    By mweimerskirch, 3 years ago
  • Check for existance of attributes.
    By , 3 years ago
  • Merge pull request #4 from gchokeen/master
    By mweimerskirch, 4 years ago
  • yml example added for services
    By , 4 years ago
  • Merge pull request #3 from gchokeen/master
    By mweimerskirch, 4 years ago
  • php 5.3.3 backward compatiblity added
    By gchokeen, 4 years ago
  • Merge pull request #2 from gchokeen/patch-1
    By mweimerskirch, 4 years ago
  • Update README.md
    By gchokeen, 4 years ago
  • Corrected target-dir in composer.json
    By mweimerskirch, 4 years ago
  • Added readme
    By mweimerskirch, 4 years ago
  • Initial prototype
    By mweimerskirch, 4 years ago