leanhhoaivui/shortcode-bundle

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

Installs: 24

Dependents: 0

Suggesters: 0

Security: 0

Stars: 0

Watchers: 0

Forks: 0

Type:symfony-bundle

dev-master 2018-08-08 01:38 UTC

This package is auto-updated.

Last update: 2024-09-22 17:46:33 UTC


README

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 JTShortcodeBundle in your composer.json:

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

Register the bundle in AppKernel

<?php
// app/AppKernel.php

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

##How to use:

###1. Create a new shortcode handler

<?php

#MyProject\Bundle\TestBundle\Shortcode\DemoShortcode.php

namespace MyProject\Bundle\TestBundle\Shortcode;

use JT\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="jt.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: jt.shortcode, alias: demo }

###3. Use it in your Twig templates

<div>{{ page.content|shortcodes }}</div>