Strategy for conditional rendering

Hi! I have coded quite a bit of javascript but I'm fairly new to CMS in general.

If a variable fruit has the value pear or apple it will affect the grammar.

"Do you want an apple?"
"Do you want a pear?"

This could be part a longer text that contains just a few places where the content should depend on a certain value.

What is the recommended strategy for handling this kind of logic? Do people embed code in the templates from Prismic and parse in when for example a gatsby site is generated? Or do they add more fields in their CMS-content types? Or what?

Thx for input!

Hey @einarpersson,

This is a great question! Thanks for asking it.

We don't have a set recommendation for this; but, personally, I would probably handle this in the code. You could use a package like this one, or just write the logic yourself, if you know what the options will be. Would that work for you? What other constraints do you have?

Sam

Thanks @samlittlefair

When you say "handle this in code", would you still store all text in Prismic?

Would you ever consider adding code in prismic contents, to be executed / parsed in a build stage? Like template string literals in javascript. Or is code in Prismic a no-go / bad practice? Would you chop this 400 word text into several pieces and "glue it together" in code?

Grateful for additional feedback!

Hey @einarpersson,

No, I wouldn't include scripts in Prismic. I believe that's an anti-pattern, because it can introduce security issues.

Yes, I think you could chop the 400 word text into pieces and glue it together in the code.

I've never seen an example like this, but I suppose you could create a simple templating system in Prismic. For example, you can write Markdown in Prismic by configuring a Rich Text field that only allows pre elements. Then you can parse the Markdown in your code. You could do something similar with a text template, like this:

Do you want [article] [fruit]?

You could create a singleton document to store this template, and query it whever you need it. And then write logic in your code to parse the text and insert your variables.

Does that make sense?

Let me know if an example would help and I could put something together.

Sam