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!
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.
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.
Any help or a pointer in the right direction would be greatly appreciated!
Cheers
Jack
Worked out a solution, I'll put it here in case anyone else needs a hand
Controller:
Controller passes IPublishedContent to the Model, which does replacements:
View then inherits from
Umbraco.Web.Mvc.UmbracoViewPage
instead ofUmbraco.Web.Mvc.UmbracoTemplatePage
. Now can use normal MVC Razor syntaxModel.Title
etc.is working on a reply...