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 7 posts 77 karma points
    1 week ago
    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 319 posts 1012 karma points c-trib
    7 days ago
    Frans de Jong
    2

    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 1269 posts 4652 karma points MVP c-trib
    7 days ago
    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 7 posts 77 karma points
    6 days ago
    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 319 posts 1012 karma points c-trib
    6 days ago
    Frans de Jong
    0

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

Please Sign in or register to post replies

Write your reply to:

Draft