Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Alexandre Locas 31 posts 134 karma points
    May 15, 2018 @ 19:23
    Alexandre Locas
    0

    Let user insert variables in rich content editors

    Hi, we have some values that we don't want to be hard-coded in Umbraco. What would be the best approach to let user insert pre-definied "variables" in editor and then render those variables ?

    For exemple i would like to let my users create content such as : "You have until $$LIMIT_DATE$$ to send us bla bla bla..."

    And then have some code to replace variables ($$xyz$$) with values coming from a DB.

    Thank you

  • Frans de Jong 524 posts 1767 karma points MVP c-trib
    May 16, 2018 @ 09:56
    Frans de Jong
    101

    Interesting case.

    I think I would use a helper in the view. Something like:

    @Helpers.ReplaceVariables(Model.Content.Richtexteditor)
    

    In that helper you can look for your special characters and replace it with a value. The only problem with this solution is that you need to access the database every pageload. To prevent that I would store the values in a settingsnode. Than it is cached and available as a Model so you can do something like:

    string.Replace("$$LIMIT_DATE$$", siteSettings.LimitDate);
    

    Frans

  • Dan Diplo 1543 posts 6098 karma points MVP 4x c-trib
    May 16, 2018 @ 11:48
    Dan Diplo
    1

    A very basic implementation would be something like:

    var richText = Model.Content.GetPropertyValue<IHtmlString>("content"); // or from model if you use model builder
    
    var replaced = new HtmlString(richText.ToString().Replace("$$LIMIT_DATE$$", replacementValue));
    
  • Alexandre Locas 31 posts 134 karma points
    May 17, 2018 @ 12:33
    Alexandre Locas
    0

    Thanks Frans and Dan, two great ideas.

    I was looking for something more generic : a solution that would apply to all views maybe.

  • Frans de Jong 524 posts 1767 karma points MVP c-trib
    May 17, 2018 @ 14:22
    Frans de Jong
    0

    Can you elaborate more on what you are trying to do? The solutions above are usable in every view.

  • Søren Kottal 571 posts 3749 karma points MVP 3x c-trib
    Feb 20, 2021 @ 10:15
    Søren Kottal
    0

    Hi Alexandre

    You could implement your own value converter for the rich text editor.

    The easiest way would be to inherit from RteMacroRenderingValueConverter, and just override ConvertIntermediateToObject with your logic.

    I guess it would look something like

        public override object ConvertIntermediateToObject(IPublishedElement owner, IPublishedPropertyType propertyType, PropertyCacheLevel referenceCacheLevel, object inter, bool preview)
        {
            var source = inter.ToString();
            source = source.Replace("$$LIMIT_DATE$$", "replacementValue");
    
            return base.ConvertIntermediateToObject(owner, propertyType, referenceCacheLevel, source, preview);
        }
    

    Then it will run the default ConvertIntermediateToObject method on your replaced content, making sure that links and macros work as expected.

    Remember to remove the original RteMacroRenderingValueConverter, so your own implementation gets used. It's described here: Registering PropertyValueConverters

  • Vinayak S Y 11 posts 80 karma points
    Feb 19, 2021 @ 13:54
    Vinayak S Y
    0

    Hi Everyone, Can you please help me how to fetch stored textstring value/textbox value into rich text editor?

    Here in the below image, I created 3 folders using document type, each folder contains 15 websites. I have created multiple websites and each website contains separate domains. Specialist Name is in textstring/textbox format example "xyz" i tried fetching that stored value in below rich text editor by using @Model.Value("specialityName") but in website its not displaying the value of textstring. Please suggest me any link or your ideas.

    enter image description here

  • Mehmet Avcı 41 posts 153 karma points
    Feb 19, 2021 @ 22:40
    Mehmet Avcı
    0

    Heya,

    Looks like easiest way would be helper. In addition, you might use macros if you can narrow down use-cases for your variables.

Please Sign in or register to post replies

Write your reply to:

Draft