How to wrap component with text nodes based on trait?
Question
Hello @artf ,
I'm not sure if this goes here but I can't figure out from the documentation how to update the component model based on a trait.
The functionality I'm trying to achieve is add a text trait to a component called "condition" which would accept a Boolean expression. If this trait has a value then I would like to wrap the component with a text node like so:
Example 1
component without trait would render like so:
<p>Some content in here</p>
Example 2:
component with trait value would render like so:
{if TRAIT_VALUE }
<p>Some content in here</p>
{endif}
Also, I'd like to exclude the wrapper text nodes in example 2 from the view if possible.
I've been working on this for days but feel like everything I try is a hack. In your opinion, what is the best way to do this?
Thank you! Josh
Answers (2)
You just have to extend the toHTML function of the component model to achieve it (search in issues if you need examples)
Ok, I will take a look. Thank you!
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2504
How to extend the image asset type to add custom attributes?
Hello, I'm trying to make a simple modification to the default image asset and am running into a wall, and am sure I'm missing something si...
Issue #2279
[QUESTION]: How to update the model for a custom component when a trait is changed.
Hi! I have read and re-read the GrapesJS Docs and API Reference on how to create a custom component and I just can't seem to figure it out....
Issue #3170
Add class name from traits
Hello, what i am trying to achieve is to have the text component extended by assigning an extra trait named Alignment and deciding through...
Issue #3309
BUG: TextNodes interfere when changing content on element
Version: 0.16.44 Hi @artf I have a custom block with a custom button component, which its content is similar to this one. <a data-gjs-type=...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.