How to use variables/bbcode in a post?

I’m trying to figure out what would be the best way to write something like this on a post:

[Click here to save your spot](https://mysite.com/$CURRENT_USERNAME)

Where $CURRENT_USERNAME would turn into a different value depending on which user was logged on.

I think I can get the current username using the plugin api inside a theme component, but I’m not sure about the best way to actually insert the link.

I guess one option would be to write a plugin that adds a new bbcode command, like discourse-details or discourse-spoiler-alert. Something like this:

[dynamic-url description="Click here to save your spot" pattern="https://mysite.com/$CURRENT_USERNAME"][/dynamic-url]

Or even more generally:

[dynamic-tag tagName="iframe" src="https://mysite.com/$CURRENT_USERNAME"][/dynamic-tag]

But I’m wondering if there is a simpler alternative.

Have you looked at the placeholder theme component?

It’s a less complicated version of what you are suggesting. Otherwise you are going to need to tap into our markdown engine to handle the replacements, but also make sure the engine has access to the information you need to replace on the client and server side. That part is tricky.

2 Likes

An alternative is also to write a theme component (less heavy than a plugin) and use wrap:

2 Likes