Often when outputting a dynamic list of macros, I need to know what position in the list the current macro is in in order to apply the right css class. For example: class="odd" or class="even".
Does umbraco support this in any way? If not, I'll look to a jquery solution, but would be a nice addition.
Thanks for the reply. Yes, if I were in control of the iteration, I would do something like that. But with the macro container, the user picks the macros that go in them and umbraco outputs them when you place the macro container tag on the page. The only thing I have control of is the markup for the individual macros (as far as I know). In this case, my macro scirpts are written in razor. I try to avoid xslt at all costs. Something like this.Container.CurrentIndex would be nice.
Must admit that I'm no Razor guru but have you had a glance at the Razor cheat sheet for Umbraco 4? In the "isHelpers" section there is .isEven() and .isOdd() helpers to determine if the input is either Odd or Even - perhaps this could help you?
Ahh yes, I forgot, the macro container content gets generated automatically by Umbraco in the template. So I don't think there's any OOTB way to get an even/odd hook, short of using jQuery or CSS3 selectors.
You might also be able to use your own macro to render the macro container content instead of letting Umbraco do it for you, by using umbraco.library:RenderMacroContent. This won't work if any of your macros are .NET usercontrols though
@Jan - Thanks for pointing out the isHelpers. Unfortunately they don't seem to work inside of a macro container. I get an error that says "Value cannot be null. Parameter name: Node 1218 has been orphaned and doesn't belong to a DynamicNodeList". I think this only applies to a tree node's position under it's parent.
@Tom - Sounds like I need to head over to CodePlex and punch in a feature request. Thanks.
I have a drop down menu working that way where if Parent is First the <ul> will have a style and if not another one. In the case of IsEven or IsOdd am not sure but you can have a look at how i proceeded.
if(parent !=null){ <ulstyle="top:@parent.IsFirst("58px","95px")">// yours would be something like class="@parent.IsFirst("odd",'even")" @foreach(var item in parent.Children.Where("Visible")){ <li@Library.If(item.IsAncestorOrSelf(Model), selectedClass)><a href="@item.Url">@item.Name</a></li> } </ul> }
Getting current position in macro container
Often when outputting a dynamic list of macros, I need to know what position in the list the current macro is in in order to apply the right css class. For example: class="odd" or class="even".
Does umbraco support this in any way? If not, I'll look to a jquery solution, but would be a nice addition.
Hi Jason,
Normally from within a for-each or apply-templates you can use the position() function to get the current position in the loop.
I think something like this would work:
-Tom
Hi Tom
Thanks for the reply. Yes, if I were in control of the iteration, I would do something like that. But with the macro container, the user picks the macros that go in them and umbraco outputs them when you place the macro container tag on the page. The only thing I have control of is the markup for the individual macros (as far as I know). In this case, my macro scirpts are written in razor. I try to avoid xslt at all costs. Something like this.Container.CurrentIndex would be nice.
Hi Jason
Must admit that I'm no Razor guru but have you had a glance at the Razor cheat sheet for Umbraco 4? In the "isHelpers" section there is .isEven() and .isOdd() helpers to determine if the input is either Odd or Even - perhaps this could help you?
/Jan
Ahh yes, I forgot, the macro container content gets generated automatically by Umbraco in the template. So I don't think there's any OOTB way to get an even/odd hook, short of using jQuery or CSS3 selectors.
You might also be able to use your own macro to render the macro container content instead of letting Umbraco do it for you, by using umbraco.library:RenderMacroContent. This won't work if any of your macros are .NET usercontrols though
-Tom
@Jan - Thanks for pointing out the isHelpers. Unfortunately they don't seem to work inside of a macro container. I get an error that says "Value cannot be null. Parameter name: Node 1218 has been orphaned and doesn't belong to a DynamicNodeList". I think this only applies to a tree node's position under it's parent.
@Tom - Sounds like I need to head over to CodePlex and punch in a feature request. Thanks.
Hi Jason,
I have a drop down menu working that way where if Parent is First the <ul> will have a style and if not another one. In the case of IsEven or IsOdd am not sure but you can have a look at how i proceeded.
//Fuji
is working on a reply...