Copied to clipboard

Flag this post as spam?

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


  • Jason N 55 posts 79 karma points
    Jun 18, 2012 @ 19:19
    Jason N
    0

    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.

     

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 18, 2012 @ 19:27
    Tom Fulton
    0

    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:

    <xsl:for-each select="...">
      <div>
        <xsl:attribute name="class">
          <xsl:choose>
            <xsl:when test="position() mod 2 = 0">even</xsl:when>
            <xsl:otherwise>odd</xsl:otherwise>
         </xsl:attribute>
      </div>
    </xsl:for-each> 

    -Tom

  • Jason N 55 posts 79 karma points
    Jun 18, 2012 @ 19:54
    Jason N
    0

    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.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Jun 18, 2012 @ 22:03
    Jan Skovgaard
    0

    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

  • Tom Fulton 2030 posts 4998 karma points c-trib
    Jun 18, 2012 @ 22:10
    Tom Fulton
    0

    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

  • Jason N 55 posts 79 karma points
    Jun 18, 2012 @ 22:23
    Jason N
    0

    @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.

  • Fuji Kusaka 2203 posts 4220 karma points
    Jun 19, 2012 @ 04:46
    Fuji Kusaka
    0

    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.

     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>
           }

     

    //Fuji

Please Sign in or register to post replies

Write your reply to:

Draft