Search code examples
phpdesign-patternstwigbolt-cms

Evaluate twig functions in record


I've created a bolt extension which provides a new twig function foo. The twig functions is added to the twig framework with the following code $this->addTwigFunction('foo', 'twigFoo');.

    public function twigFoo()
    {
        $markup = '
        <hr>
            Foo
        <hr>';

        return new \Twig_Markup($markup, 'UTF-8');
    }

My idea was that the users of the cms can use the twig function in the content types. But when the body of a record is displayed the twig function is visible as plain HTML for example: {{ foo }}

I think the problem is, that the twig template will be rendered before the record body will be assigned. So the body of my record will not be evaluated by twig. Has anyone a idea how to evaluate the twig function witch is use in a record? What's the best practice for this problems?


Solution

  • The field in the ContentType needs allowtwig: true to tell Bolt that you trust the field/editor to allow this, e.g.:

    body:
      type: html
      allowtwig: true