Theodore Enderby
Theodore Enderby

Reputation: 642

Does Twig support this?

I'm playing around with a CMS idea for Symfony and I'm not sure if what I want to implement is possible. I'm looking for some guidance.

I want to create a twig function that acts more like a block.

I want to be able to write template like this:

{% content('main_content') %}
    <p>Some markup here</p>
{% end content('main_content') %} 

where content($id) is a function that gives me access to the markup inside and allows my twig extension to change that markup if needed.

The content($id) function's goal will be to change the mark up based on what $id is given.

Obviously my train of thought on this implementation is off because I cannot find any docs relevant to this.

I'm not sure how to word this question : Is it possible to write a twig function that acts like a block and gives me access to the inner data. If possible is there some examples or blogs you can link me to? I'm using Symfony so answers with Symfony implementations are a plus.

edit: an example of the content($id) function

class TwigExtension extends \Twig_Extension{

    public function content ($id, $content) {
        /* pseudo code now*/
        if($id = "some condition"){
           return $someNewContent;
        }
        return $content;
    }
}

Upvotes: 0

Views: 73

Answers (1)

Alexandru Furculita
Alexandru Furculita

Reputation: 1373

You need to create a new Twig tag. More documentation here: https://github.com/twigphp/Twig/blob/master/doc/advanced.rst#tags

Also the Twig_Node_Block will give a good idea of what you need to build: https://github.com/twigphp/Twig/blob/1.x/lib/Twig/Node/Block.php

Upvotes: 4

Related Questions