i have some data in my entry like productnames "Nike", "Puma" etc. which are headings i diplay in my template.

{{ productnames1 }}

"some text the contenteditor maybe wants do add"

{{ productnames2 }}

"some text the contenteditor maybe wants do add"

How can i create fields like textfields which contains data from the productnames that the contenteditor knows which text of which productname he is editing.

my best approach is to let the contenteditor count the number of productnames and corresponding to the number create new textfields which will be outputed through a loop matching the number of products.

explanation is a little wild. hope you can get my point.


Instead of splitting the content up into multiple fields, you can put your editors in charge of outputting the product headlines by using reference tags. So your field content could look like this:


some text the contenteditor maybe wants do add


some text the contenteditor maybe wants do add

In this example, nike and puma would be the entries' slugs. There are other ways to refer to specific entries, check the examples in the documentation linked below.

If I remember correctly, Craft should parse those automatically on output, otherwise you can use the parseRefs filter in your template.

Another maybe slightly more solid approach would be to use Matrix fields. You can define two block types for product references and custom text respectively. Then loop through them and for every product reference block, output the headline of the selected product as a headline. This would allow your editors more control over the content structure, and you more control over the resulting HTML.


The Variables plugin in the Redactor plugin might be useful in this case.

One way to handle it would be to create your variables, like Brand and Product.

Setup a Redactor field and add variable to buttons and plugins to the json config. Then inside variables define the variables you want, in this case Brand and Product, for example.

  "buttons": ["html","formatting", "bold", "italic", "unorderedlist", "orderedlist", "link", "image", "video","variable"],
  "plugins": ["fullscreen", "video","variable"],
  "linkNewTab": true,
  "toolbarFixed": true,

Just click the {x} button in redactor to select the variable you want. These are more or less placeholders for where you want the replaced content to appear, hence variables.

This gets you something like:

Redactor Variables

The Redactor field outputs variables like:

<span data-redactor-type="variable">Brand</span>

so you just need to do a quick find/replace in Twig on the content:

{{ entry.body|replace({'<span data-redactor-type="variable">Brand</span>':entry.brand.one.title,'<span data-redactor-type="variable">Product</span>':entry.product.one.title}) }}

I used an Entries field as the example but you can use whatever field type you prefer.

The last thing to note is you'll need to either turn off HTML Purifer or add some code in a module/plugin to allow redactor's data-redactor-type data attribute on a span element or it will be cleansed off the next time you save.

The Redactor plugin actually comes with the exact code you need to enable the Variables plugin to work, it's just not enabled.

      use craft\redactor\events\ModifyPurifierConfigEvent;
      use craft\redactor\Field;
      use HTMLPurifier_AttrDef_Text;
      use yii\base\Event;
      Event::on(Field::class, Field::EVENT_MODIFY_PURIFIER_CONFIG, function(ModifyPurifierConfigEvent $e) {
           // Allow the use of the Redactor Variables plugin
           $e->config->getHTMLDefinition(true)->addAttribute('span', 'data-redactor-type', new HTMLPurifier_AttrDef_Text());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.