I am trying to do an if statement in razor for an umbraco website. I need to get the page and all of its children by the Name property to be able to add a stylesheet to that page and any children in the tree
I can get the parent no problem but am having difficulties with the descendants as i am not sure where it would go?
So far I have:
@{
var selectedNode = @Model.Name();
if(@selectedNode == "blue")
{
do this
}
}
Looks like you’re a bit new to razor. This answer has a list of some great resources available to help you start learning about Umbraco’s razor syntax.
In response to your question, I’m not sure if I understand exactly what you’re trying to accomplish. From what I can see, you want to add a style sheet to a page based on it’s name or the name of one of its ancestors. I’d suggest that it may not be the best option to key off of the name; a more stable solution would be to use the node’s id. But, here’s a code sample that should hopefully help:
The above snippet gets the node’s ancestors (including itself) and filters them by the name of the node (“Blue”). Then it checks if any nodes are in the collection. If there are, then either the current node is “Blue” or it has an ancestor named “Blue” and therefore the style is applied.