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.
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.
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)
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.
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!
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
or razor
What I get is
<?UMBRACO_MACRO macroAlias="NewsFullPdfLink" />
I have also tried this, but it doesn't print anything
Could someone tell me how to print out the macro content please? Thank you in advance.
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.
Hi. Afaik, the umbraco.library.Item(...) method does the trick.
Hi Michael,
Thank you. I tried creating this function but it replaces with empty string instead.
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.
Thank you Rodion, I tried this
but it returns this (I replaced square brackets with round brackets here because it didn't show)
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.
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.
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!
OK,
I'll check later on the new topic then :-)
Cheers,
Michael.
Thanks a lot! :D
I posted the link btw (edited my previous post)
is working on a reply...