Copied to clipboard

Flag this post as spam?

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


  • Aximili 177 posts 278 karma points
    Mar 26, 2012 @ 07:47
    Aximili
    0

    Render macro in a UserControl (ascx) or Razor

    I have a DocumentType News, which has a property teaser, which may contain a macro.

    But when I print out teaser using usercontrol

    <%=newsItem.GetProperty("teaser").Value%>

    or razor

    @newsItem.teaser

    What I get is

    <?UMBRACO_MACRO macroAlias="NewsFullPdfLink" />

     

    I have also tried this, but it doesn't print anything

    <%=umbraco.library.RenderMacroContent(newsItem.GetPropertyValue("teaser", ""), Node.getCurrentNodeId())%>

    Could someone tell me how to print out the macro content please? Thank you in advance.

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Mar 26, 2012 @ 09:56
    Michael Latouche
    0

    Hi Hardi,

    The problem is that RenderMacroContent only applies to  "<?UMBRACO_MACRO .../>" strings, not to the full property text

    I don't know if there exists a better/direct solution, but one way of doing would be to first get your property value, then parse it and replace each <?UMBRACO_MACRO ...> item yiou find by the result of the RenderMacroContent method called on that item.

    Cheers,

    Michael.

  • Rodion Novoselov 694 posts 859 karma points
    Mar 26, 2012 @ 11:38
    Rodion Novoselov
    0

    Hi. Afaik, the umbraco.library.Item(...) method does the trick.

  • Aximili 177 posts 278 karma points
    Mar 27, 2012 @ 00:55
    Aximili
    0

    Hi Michael,

    Thank you. I tried creating this function but it replaces with empty string instead.

      var allNews = new Node(Static.NODE_ID_NEWS).Children;
    foreach (INode newsItem in allNews)
    {
      string teaserText = RenderTextWithMacro(newsItem.GetProperty("teaser").Value, Node.getCurrentNodeId());
    ...
      public static string RenderTextWithMacro(string text, int pageId)
      {
        if (text.Contains("UMBRACO_MACRO"))
        {
          var matches = Regex.Matches(text, @"()");
          foreach(Match m in matches)
          {
            string macro = m.Groups[1].Value; // macro is
    string result = umbraco.library.RenderMacroContent(macro, pageId); // result is empty string
    text = text.Replace(macro, result);
          }
        }
        return text;
      }

    Could you see anything wrong?

    Should the PageId parameter be the node ID of the current page or the node ID of the news item? I tried both, both returns empty string.

  • Aximili 177 posts 278 karma points
    Mar 27, 2012 @ 01:12
    Aximili
    0

    Thank you Rodion, I tried this

    var allNews =newNode(Static.NODE_ID_NEWS).Children;
     
    foreach(INode newsItem in allNews)
     
    {
    string teaserText = new umbraco.library(Node.getCurrentNodeId()).Item(newsItem.Id, "teaser");
    ...

    but it returns this (I replaced square brackets with round brackets here because it didn't show)

    ((((umbraco:Item nodeId="1533" field="teaser" ))))
  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Mar 27, 2012 @ 10:15
    Michael Latouche
    0

    Hi Hardi,

    I'm not quit sure I understand your regex match expression "()". Is this the actual regex you used, or maybe something went wrong in the post and the actual expression has gone? Have you tried to print out/check the value of "macro" prior to rendering it, just to see if it had the correct format?

    In non Regex mode, I would (loop and) look for the string "<?UMBRACO_MACRO", then get everything until the next "/>", and apply the RenderMacroContent method on that string.

    The pageID in the RenderMacroContent method is the ID of the item on which the macro must be applied, so in this case I guess the newsitem.

    Cheers,

    Michael.

  • Aximili 177 posts 278 karma points
    Mar 28, 2012 @ 00:39
    Aximili
    0

    Oh I just realised both the regex and the comment containing UMBRACO_MACRO has dissappeared!!

    Yes, the regex correctly matched this string <?UMBRACO_MACRO macroAlias="NewsFullPdfLink"/>

    But applying RenderMacroContent on that string returns "". I've tried passing newsItem ID as well.

    Any other idea how to solve this? Thanks a lot Michael, I can't believe it's so hard to get this to work.

  • Aximili 177 posts 278 karma points
    Mar 28, 2012 @ 00:46
    Aximili
    0

    I see what went wrong! RenderMacroContent was actually printing the macro correctly!

    The problem lies in the .cshtml file, it got the wrong model! But I still don't know how to fix it. I'll post a new topic to keep the forum neat.

    http://our.umbraco.org/forum/developers/extending-umbraco/30282-Model-in-cshtml-incorrectly-referring-to-the-current-Page

    Could you help me again there please? You understand the problem the most :)

    Btw Michael, FYI, out of curiosity I tested everything again.
    It doesn't matter if I pass the whole "teaser" text to RenderMacroContent directly, and it doesn't matter if I pass the newsItem ID or the page ID (ie. Node.CurrentId).
    It does the same thing, Model is always the page :(

    Now I don't know which answer to accept because my question was wrong... but you've been really helpful I got this far because you ensured me there was nothing wrong with the code, thank you so much!

  • Michael Latouche 504 posts 819 karma points MVP 4x c-trib
    Mar 28, 2012 @ 01:35
    Michael Latouche
    1

    OK,

    I'll check later on the new topic then :-)

    Cheers,

    Michael.

  • Aximili 177 posts 278 karma points
    Mar 28, 2012 @ 01:40
    Aximili
    0

    Thanks a lot! :D

    I posted the link btw (edited my previous post)

Please Sign in or register to post replies

Write your reply to:

Draft