# @richtext

**URL:** <https://forums.realmacsoftware.com/t/richtext/47726>\
**Category:** Elements API\
**Created:** [May 24, 2025, 6:44am UTC](https://forums.realmacsoftware.com/t/richtext/47726 "2025-05-24T06:44:07Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![vibralogix](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/vibralogix/32/3065_2.png) [@vibralogix](https://forums.realmacsoftware.com/u/vibralogix)\
**Post date:** [May 24, 2025, 6:44am UTC](https://forums.realmacsoftware.com/t/richtext/47726/1 "2025-05-24T06:44:08Z")

</div>

I make use of @richtext in a component and noticed that since I updated [Elements](https://elementsapp.io/) it seems to have stopped working.

In the template I have

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

```

and in hooks I have

```const
  const {
    slrichtext,
  } = rw.props;

```

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

---

<div class="post-metadata">

**Author:** ![ben](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/ben/32/28488_2.png) [@ben](https://forums.realmacsoftware.com/u/ben)\
**Post date:** [May 26, 2025, 7:55am UTC](https://forums.realmacsoftware.com/t/richtext/47726/2 "2025-05-26T07:55:14Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![vibralogix](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/vibralogix/32/3065_2.png) [@vibralogix](https://forums.realmacsoftware.com/u/vibralogix)\
**Post date:** [May 26, 2025, 8:21am UTC](https://forums.realmacsoftware.com/t/richtext/47726/3 "2025-05-26T08:21:56Z")

</div>

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](https://elementsapp.io/) doesn’t. In [elements](https://elementsapp.io/) 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.

---

<div class="post-metadata">

**Author:** ![ben](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/ben/32/28488_2.png) [@ben](https://forums.realmacsoftware.com/u/ben)\
**Post date:** [May 26, 2025, 1:20pm UTC](https://forums.realmacsoftware.com/t/richtext/47726/4 "2025-05-26T13:20:44Z")

</div>

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:

```auto
@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](https://elementsapp.io/). Checkout one of my [latest CMS videos](https://www.youtube.com/watch?v=bA_lVo3uE58) for an example of this in action.

---

<div class="post-metadata">

**Author:** ![vibralogix](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/vibralogix/32/3065_2.png) [@vibralogix](https://forums.realmacsoftware.com/u/vibralogix)\
**Post date:** [May 26, 2025, 4:18pm UTC](https://forums.realmacsoftware.com/t/richtext/47726/5 "2025-05-26T16:18:29Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![vibralogix](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/vibralogix/32/3065_2.png) [@vibralogix](https://forums.realmacsoftware.com/u/vibralogix)\
**Post date:** [June 11, 2025, 9:30am UTC](https://forums.realmacsoftware.com/t/richtext/47726/6 "2025-06-11T09:30:43Z")

</div>

I finally have some time to look at this now. I am confused about $twig though. Does elements setup $twig internally or do I need to do something further? Do you have an example of including twig? I had a read through the Twig docs which make sense but not sure how to include it in the page.

---

<div class="post-metadata">

**Author:** ![ben](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/ben/32/28488_2.png) [@ben](https://forums.realmacsoftware.com/u/ben)\
**Post date:** [June 11, 2025, 11:52am UTC](https://forums.realmacsoftware.com/t/richtext/47726/7 "2025-06-11T11:52:08Z")

</div>

> [@vibralogix](#):
>
> Does elements setup $twig internally or do I need to do something further?

No, not out of the box. Your component needs to handle its own dependencies.

The way I am doing it is by adding dependencies to my pack’s `components/shared/assets` folder — then I include those files as needed in my component.

There’s nothing fancy going on.

You can get the shared assets path in your component’s hooks.js file like so:

```js
const { sharedAssetPath } = rw.component;

```

that’ll give you the published path to the `components/shared/assets` folder, so in your template you can do something like this:

```php
<?php
require_once __DIR__. '/{{sharedAssetPath}}/some_file.php';
?>

```

So `some_file.php` would be located in `you-pack.elementsdevpack/components/shared/assets/some_file.php`

Hope that helps 🙂

---

<div class="post-metadata">

**Author:** ![vibralogix](https://dub1.discourse-cdn.com/flex005/user_avatar/forums.realmacsoftware.com/vibralogix/32/3065_2.png) [@vibralogix](https://forums.realmacsoftware.com/u/vibralogix)\
**Post date:** [June 12, 2025, 10:36am UTC](https://forums.realmacsoftware.com/t/richtext/47726/8 "2025-06-12T10:36:58Z")

</div>

Hi Ben,

Thanks. That all seems to be working well. Using Twig may allow some more things to be done too.

I have another question regarding the pageStart portal but I will start a new thread for that.
