locaine/lcn-template-block-bundle

Populate blocks of template code from PHP/Twig and output them later in a Twig Template

1.0.1 2015-02-09 13:03 UTC

This package is not auto-updated.

Last update: 2024-04-27 14:22:59 UTC


README

Step 1: Download the Bundle

Open a command console, enter your project directory and execute the following command to download the latest stable version of this bundle:

$ composer require locaine/lcn-template-block-bundle "~1.0"

This command requires you to have Composer installed globally, as explained in the installation chapter of the Composer documentation.

Step 2: Enable the Bundle

Then, enable the bundle by adding the following line in the app/AppKernel.php file of your project:

<?php
// app/AppKernel.php

// ...
class AppKernel extends Kernel
{
    public function registerBundles()
    {
        $bundles = array(
            // ...

            new Lcn\TemplateBlockBundle\LcnTemplateBlockBundle(),
        );

        // ...
    }

    // ...
}

Usage

PHP

Example controller code:

//add code to a block
$this->container->get('lcn.template_block')->add('BLOCK_NAME', 'BLOCK_CONTENT');

////adding the same code to a block again is ignored by default. You can force adding the same code by supplying false as third argument
$this->container->get('lcn.template_block')->add('BLOCK_NAME', 'BLOCK_CONTENT', false);

//you can set the code in a block effectively overriding existing code in the block
$this->container->get('lcn.template_block')->set('BLOCK_NAME', 'BLOCK_CONTENT');

//clear a block
$this->container->get('lcn.template_block')->clear('BLOCK_NAME');

//get the code of a block
$this->container->get('lcn.template_block')->get('BLOCK_NAME', 'OPTIONAL_FALLBACK_CODE');

TWIG

Example Twig template code:

{# add code to a block #}
{{ lcn_add_to_template_block('BLOCK_NAME', 'BLOCK_CONTENT') }}

{# adding the same code to a block again is ignored by default. You can force adding the same code by supplying false as third argument #}
{{ lcn_add_to_template_block('BLOCK_NAME', 'BLOCK_CONTENT', false) }}

{# you can set the code in a block effectively overriding existing code in the block #}
{{ lcn_set_template_block('BLOCK_NAME', 'BLOCK_CONTENT') }}

{# clear a block #}
{{ lcn_clear_template_block('BLOCK_NAME') }}

{# get the code of a block #}
{{ lcn_template_block('BLOCK_NAME', 'OPTIONAL_FALLBACK_CODE') }}

{# the code is escaped by default, but cou can also get the raw code of a block #}
{{ lcn_template_block_raw('BLOCK_NAME', 'OPTIONAL_FALLBACK_CODE') }}