Umbraco Render Macro Inside A String


/ Published in: C#
Save to your folder(s)



Copy this code and paste it in your HTML
  1. public string RenderMacro(string input, int nodeId) {
  2. if (string.IsNullOrEmpty(input))
  3. return string.Empty;
  4. var regex = new Regex("<\\?UMBRACO_MACRO[^>]*/>", RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.IgnorePatternWhitespace);
  5. return regex.Replace(input, p => {
  6. try {
  7. return umbraco.library.RenderMacroContent(p.Value, nodeId);
  8. } catch (Exception ex) {
  9. Trace.Warn(ex.Message);
  10. return "Problems Rendering Macro";
  11. }
  12. });
  13. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.