November Happy Hour will be moved to Thursday December 5th.

Dynamic text strings in TinyMCE

Vote:
 

Hi there,

I am wondering if there is a way to pass a calculated value into the TinyMCE editor. For example, when a user logs in, a message can be displayed "You have donated ${DonatedAmt} this year".

Still using CMS 11 and have read posts about dynamic content being deprecated with the recommendation to use a blocks. However, seems like there are issues with nested blocks and rendering inline so was curious if there had been any more recent solutions proposed for something like this?

#331967
Oct 25, 2024 19:51
Vote:
 

If your editors accept writing actual things like: "You have donated ${DonatedAmt} this year", you could let them do that.

Add a display template like this, filename: XhtmlString.cshtml:

@using EPiServer.Core
@model XhtmlString

@{
    if (Model == null) { return; };
}

@{Html.RenderXhtmlString(Model.ReplacePlaceholders());}


Then, something like this:

public static class XhtmlStringExtensions
{
    public static XhtmlString ReplacePlaceholders(this XhtmlString xhtmlString)
    {
        var contextModeResolver = ServiceLocator.Current.GetInstance<IContextModeResolver>();
        if (contextModeResolver.CurrentMode == ContextMode.Edit)
        {
            return xhtmlString;
        }

        var processedText = xhtmlString.ToInternalString().Replace("${DonatedAmt}", "1234");
        return new XhtmlString(processedText);
    }
}



#331969
Edited, Oct 25, 2024 20:39
Vote:
 

Hi,

If you're just interested in replacing placeholder tokens in XhtmlString properties then what Tomas suggested is a great way to do it without too much worry about unintended consequences. You can even use a UIHint and display template to apply that same approach to just specific properties.

If you're looking for something more far-reaching which would allow you to use the token in any property (or even in the markup in your views) then it would be worth taking a look at this article from David Knipe:
https://www.david-tec.com/2017/11/tokenised-content-in-episerver/

The approach in that article involves creating a filter which will replace the token in the generated HTML before it's returned in the HTTP response to the client. You can apply it globally or to individual controllers but it will replace any instance of the token regardless of where it is within the response. 

#332083
Oct 28, 2024 13:38
Tomas Hensrud Gulla - Oct 30, 2024 9:00
I don't remember seeing David's blog post. Good option too! 😁
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.