Copied to clipboard

Flag this post as spam?

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


  • Jack Guthrie 3 posts 94 karma points
    Oct 24, 2017 @ 16:41
    Jack Guthrie
    0

    Merge string into property data value before content is rendered

    Hi guys,

    I'm brand new to Umbraco, but have been searching all over for a solution to this for days...

    I'm looking for a way to merge values into a property's data value text string before the text is rendered on the page.

    So, for example, a content editor would write some text and include a place-holder e.g. @@EventDate@@

    When the text is rendered, the controller would find all instances of @@EventDate@@ and dynamically replace them with some value (retrieved from a database/computed)

    I've tried various things similar to the code below (which obviously doesn't work because IPublishedContent is read-only).

    I don't want to do a find and replace on the database itself because the merged values need to be dynamic.

    public class HomeController : RenderMvcController
    {
        private UmbracoContext _context;
    
        public HomeController()
        {
            _context = UmbracoContext.Current;
        }
    
        public ActionResult Home(RenderModel model)
        {
            var umbracoHelper = new Umbraco.Web.UmbracoHelper(_context);
    
            IPublishedContent pubContent = umbracoHelper.TypedContent(model.Content.Id);
            var pubContentArray = pubContent.Properties.ToArray();          
    
            foreach (var property in pubContentArray)
            {
                property.DataValue = property.DataValue.ToString().Replace("@@EventDate@@", "Some New Value");
            }
    
            RenderModel newRenderModel = new RenderModel(pubContent);
    
            return base.Index(newRenderModel);
        }
    }
    

    Any help or a pointer in the right direction would be greatly appreciated!

    Cheers

    Jack

  • Jack Guthrie 3 posts 94 karma points
    Oct 25, 2017 @ 09:55
    Jack Guthrie
    101

    Worked out a solution, I'll put it here in case anyone else needs a hand

    Controller:

    namespace UmbracoTestTwo.Controllers
    {
        public class HomeController : RenderMvcController
        {
            // GET: Home
            private UmbracoContext _context;
    
            public HomeController()
            {
                _context = UmbracoContext.Current;
            }
    
            public ActionResult Home(RenderModel model)
            {
                var umbracoHelper = new Umbraco.Web.UmbracoHelper(_context);
    
                IPublishedContent pubContent = umbracoHelper.TypedContent(model.Content.Id);
    
                HomeViewModel viewModel = new HomeViewModel(pubContent, true);
    
                return base.Index(viewModel);
            }
        }
    }
    

    Controller passes IPublishedContent to the Model, which does replacements:

    namespace UmbracoTestTwo.ViewModels
    {
        public class HomeViewModel : RenderModel
        {
            public HomeViewModel(IPublishedContent content) : base(content)
            {
                Title = content.GetProperty("title").DataValue.ToString().Replace("@@test@@", "REPLACED TEST");
                Body = content.GetProperty("body").DataValue.ToString().Replace("@@test@@", "REPLACED TEST");
            }
    
            //Custom properties here...
            public string Title { get; set; }
            public string Body { get; set; }
    
        }
    }
    

    View then inherits from Umbraco.Web.Mvc.UmbracoViewPage instead of Umbraco.Web.Mvc.UmbracoTemplatePage. Now can use normal MVC Razor syntax Model.Title etc.

    @using UmbracoTestTwo.ViewModels
    @inherits Umbraco.Web.Mvc.UmbracoViewPage<UmbracoTestTwo.ViewModels.HomeViewModel>
    
    @{
        Layout = "Master.cshtml";
    }
    
    <h1>Static content Home</h1>    
    
    <h2>
        @Model.Title
    </h2>
    
    <p>
        @Model.Body
    </p>
    
  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies