@richtext

I make use of @richtext in a component and noticed that since I updated Elements it seems to have stopped working.

In the template I have

@richtext("slrichtext", default: "Enter text here")

and in hooks I have

  const {
    slrichtext,
  } = rw.props;

It now seems that slrichtext is returning null. Have there been any changes?

Hi @vibralogix

You can’t get the contents of a @richtext() directive from within the hook.js file.

If you could share your use-case I might be able to offer an alternative approach :slight_smile:

That’s odd as it has been working fine until a recent update. Just luck I guess.

The way I was using it was to allow rich text to be added to the page which could contain ‘variables’ which get replaced with actual data.

So for example I would get the rich text contents and replace !!!username!!! with the code used to insert the username. It has been working really well until I updated.

The main reason for needing to do this is to allow data to be inserted directly within text. This is something Rapidweaver Classic supports well (using ignore formatting) but Elements doesn’t. In elements I can only have the username inserted in a block by itself on the page but not within text.

Having access to the contents of the richtext again would be really useful.

I am doing exactly this in the CMS components I’m building — I’m using Twig in my templates.

I wrap the template contents in PHP, and then pass that to Twig to render.

Basic example:

@portal(pageStart, includeOnce: true)
<?php
// include twig / other required functions

if (!function_exists('renderTemplate')) {
    function renderTemplate($template, $variables = [])
    {
        global $twig;
        return $twig->createTemplate($template)->render($variables);
    }
}
?>
@endportal

<?php
  $data = [] // setup your data...
  ob_start();
?>

    @richtext("myContent")

<?php
  $template = ob_get_clean();
    echo renderTemplate($template, ['data' => $data]);
?>

This setup allows the user to use the Twig syntax directly in Elements. Checkout one of my latest CMS videos for an example of this in action.

Thanks Ben. That sounds like a good way to handle situations like this. I have never used Twig before so will take a closer look.

1 Like