It's been a while since I've had to write a Razor macro that uses a content picker as a property and I can't seem to get the syntax correct to get the url on the <a href>. Here is my code:
Thanks Dennis! This works great, except can I do this without a test for the value of contentPicker without risking exceptions? I really don't care if the user provides a url or not.
Okay, now I need to show you more of the code, because it says it's expecting a "}" after my first <a> line, but there is one right befor the "else". What's going on?
Well Dennis, I've done something like this, but if I add an <a> tag anywhere in any of the "featureBox" Content areas it creates another <a class="featureBox"> arround the link. Here is my code and the page: Page( https://edit-wwwprep.rose-hulman.edu/32742.aspx )
The big thing for me is correctly wrapping the featureBoxUrl, featureBoxImage, featureBoxHeadline and featureBoxContent all within the <a class="featureBox">, but for some reason, a user decides to put an in-line link within the "featureBoxContent" field on the page the razor doesn't wrap the outer <a> correctly, it in fact, ends it after the "featureBoxContent" field. Any suggestions about correcting this will be greatly appreaciated!!
Syntax for getting the URL Property of a Node
It's been a while since I've had to write a Razor macro that uses a content picker as a property and I can't seem to get the syntax correct to get the url on the <a href>. Here is my code:
@{ var page = Model.AncestorOrSelf("PresidentsHomePage"); if(page.HasValue("featureBox1Content") && page.HasValue("featureBox2Content") && page.HasValue("featureBox3Content")){ <div id="featureBoxWrap"> <a class="featureBox" href="@page.featureBox1Url"> @if(page.HasValue("featureBox1Headline")){ <div class="featureHeadline"> @page.featureBox1Headline <hr> </div> } @if(page.HasValue("featureBox1Image")){ var imageBox1 = @Model.Media("featureBox1Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox1Content")){ <div class="featureContent"> @page.featureBox1Content </div> } </a>Hi Steve,
Try to see this documentation on how to get the url from a content picker. https://our.umbraco.org/Documentation/Using-Umbraco/Backoffice-Overview/Property-Editors/Built-in-Property-Editors/Content-Picker
An example.
@{if (Model.HasValue("contentPicker")){
var node = Library.NodeById(Model.contentPicker);
<a href="@node.Url">@node.Name</a>
}
}
Hope this helps,
/Dennis
Thanks Dennis! This works great, except can I do this without a test for the value of contentPicker without risking exceptions? I really don't care if the user provides a url or not.
Hi Steve,
You could give it a try just to use this. And then try not to add a link to one of the elements and see if you get any risking exceptions
Else then you could make an if else statement.
/Dennis
Looks like I will have to try the if/else because the macro fails to load without the if statement.
Okay, now I need to show you more of the code, because it says it's expecting a "}" after my first <a> line, but there is one right befor the "else". What's going on?
@inherits umbraco.MacroEngines.DynamicNodeContext @{ var page = Model.AncestorOrSelf("PresidentsHomePage"); if(page.HasValue("featureBox1Content") && page.HasValue("featureBox2Content") && page.HasValue("featureBox3Content")){ <div id="featureBoxWrap"> @if(page.HasValue("featureBox1Url")){ var node = Library.NodeById(Model.featureBox1Url) <a class="featureBox" href="@node.Url"> } else { <a class="featureBox" href=""> @if(page.HasValue("featureBox1Headline")){ <div class="featureHeadline"> @page.featureBox1Headline <hr> </div> } @if(page.HasValue("featureBox1Image")){ var imageBox1 = @Model.Media("featureBox1Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox1Content")){ <div class="featureContent"> @page.featureBox1Content </div> } </a> } }Hi Steve,
What if you do something like this would this work for you.
Hope this helps,
/Dennis
Well Dennis, I've done something like this, but if I add an <a> tag anywhere in any of the "featureBox" Content areas it creates another <a class="featureBox"> arround the link. Here is my code and the page: Page( https://edit-wwwprep.rose-hulman.edu/32742.aspx )
@inherits umbraco.MacroEngines.DynamicNodeContext @{ var page = Model.AncestorOrSelf("PresidentsHomePage"); if(page.HasValue("featureBox1Content") && page.HasValue("featureBox2Content") && page.HasValue("featureBox3Content")){ <div id="featureBoxWrap"> @if(page.HasValue("featureBox1Url")){ var node = Library.NodeById(@Model.featureBox1Url); <a class="featureBox" href="@node.Url"> @if(page.HasValue("featureBox1Headline")){ <div class="featureHeadline"> @page.featureBox1Headline <hr> </div> } @if(page.HasValue("featureBox1Image")){ var imageBox1 = @Model.Media("featureBox1Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox1Content")){ <div class="featureContent"> @page.featureBox1Content </div> } </a> } else { <div class="featureBox" > @if(page.HasValue("featureBox1Headline")){ <div class="featureHeadline"> @page.featureBox1Headline <hr> </div> } @if(page.HasValue("featureBox1Image")){ var imageBox1 = @Model.Media("featureBox1Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox1Content")){ <div class="featureContent"> @page.featureBox1Content </div> } </div> } @if(page.HasValue("featureBox2Url")){ var node = Library.NodeById(@Model.featureBox2Url); <a class="featureBox" href="@node.Url"> @if(page.HasValue("featureBox2Headline")){ <div class="featureHeadline"> @page.featureBox2Headline <hr> </div> } @if(page.HasValue("featureBox2Image")){ var imageBox2 = @Model.Media("featureBox2Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox2Content")){ <div class="featureContent"> @page.featureBox2Content </div> } </a> } else { <div class="featureBox"> @if(page.HasValue("featureBox2Headline")){ <div class="featureHeadline"> @page.featureBox2Headline <hr> </div> } @if(page.HasValue("featureBox2Image")){ var imageBox2 = @Model.Media("featureBox2Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox2Content")){ <div class="featureContent"> @page.featureBox2Content </div> } </div> } @if(page.HasValue("featureBox3Url")){ var node = Library.NodeById(@Model.featureBox3Url); <a class="featureBox" href="@node.Url"> @if(page.HasValue("featureBox3Headline")){ <div class="featureHeadline"> @page.featureBox3Headline <hr> </div> } @if(page.HasValue("featureBox3Image")){ var imageBox3 = @Model.Media("featureBox3Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox3Content")){ <div class="featureContent"> @page.featureBox3Content </div> } </a> } else { <div class="featureBox"> @if(page.HasValue("featureBox3Headline")){ <div class="featureHeadline"> @page.featureBox3Headline <hr> </div> } @if(page.HasValue("featureBox3Image")){ var imageBox3 = @Model.Media("featureBox3Image"); <div class="featureImage"> <img src="/[email protected]&width=263" /> </div> } @if(page.HasValue("featureBox3Content")){ <div class="featureContent"> @page.featureBox3Content </div> } </div> } </div> } }The big thing for me is correctly wrapping the featureBoxUrl, featureBoxImage, featureBoxHeadline and featureBoxContent all within the <a class="featureBox">, but for some reason, a user decides to put an in-line link within the "featureBoxContent" field on the page the razor doesn't wrap the outer <a> correctly, it in fact, ends it after the "featureBoxContent" field. Any suggestions about correcting this will be greatly appreaciated!!
is working on a reply...
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.