Feature request - shortcodes

General idea:

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

Example shortcode:

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.


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}}",

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?

I don’t really, as I’m just spitballing.

For example, my documentation site is full of cross references to other content. Some of this is structured (collection link), but other references are links to other content within text strings (markdown or textarea blocks). If I had a shortcode available to use within markdown or textarea blocks to get the slug of a collection item, then I don’t have to worry about the accuracy of those links if/when the link changes.

I could see other use cases as well, such as my example of the Nikon camera price, where a price data point may change frequently and having the shortcode allows for that dynamic update in a way that’s not as dependent on front end logic.