Feature request - shortcodes

General idea:

Shortcodes that return the value of a field in a response.

Example shortcode:
[collection.entry.field]

Example in use:
The Nikon camera is currently selling for $[products.nikon_camera.price]

Maybe this only works in specific field types: text, textarea, wysiwyg, markdown and returns the value of that field and ‘injects’ it into the response.

2 Likes

I understand the idea but not sure if it would make sense.

It would be required to trigger the collections.find.after, iterate on every resulting entry, check all fields for the shortcode, parse the short code, do another query to get the collection and then the field value, and finally replace the shortcode in the field. Depending on the complexity of your content structure that would impact the performance of the cockpit api.
Besides the performance aspects it should be simple to implement.

I agree here, if you want such functionality then it should be implemented in a dedicated addon

One idea that would potentially lessen the payload:

  • Make the shortcode less dynamic. So the shortcode pulls the data at time of save and saves it to the DB for the entry.
  • A CLI command or some other trigger, allows a user to create a cron job, etc. to update all shortcodes (such as if a price was updated like my example above) on a schedule, etc.

But when you save the data you are replacing the shortcode, which means you can’t do the second step…
it would maybe possible to use something like [code:value] so you could further updates, but then you’ll need to parse in the FE.

But if the shortcode remained, and the reference was in the same table wouldn’t that reduce payload?

Actually, what if it worked like this:

The shortcode is always in the response as it is, but that shortcode reference creates a new name:value pair in the response. The author, when parsing the response, can replace the shortcode dynamically.

For example:

"product_description":"Nikon camera sells for {{products.nikon_camera.price}}",
"products.nikon_camera.price":"$335.00"

If the reference data is in the same entry, yes, that would not require to perform an api request to the referenced collection. But in that case why to not do the replacement of the token in the FE?
It may depend on the way you structured your data, can you provide more details about your collection?