Length or Size

c-siems's Avatar

c-siems

15 Nov, 2012 10:15 PM

Is there a method to determine length or size of the initial variable? Testing $articles.size and $articles.length don't appear to work. Neither does if ($articles) Maybe a better question is what type is $articles? I'm guessing it's not an array.

#set ($articles = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-folder/system-folder/system-page"))
$_SortTool.addSortCriterion("start-date", "", "number", "descending", "upper-first")
$_SortTool.sort($articles)
#set ($count = 1)

#if ( $articles.size > 0 )
<ul class="unstyled newsitems">
 #foreach ($article in $articles)
    #set ($articleDate = $_DateTool.getDate($article.getChild("start-date").value))
    #set ($articleTitle = $article.getChild("title"))
    #set ($articleLink = $article.getChild("link").value)
    #if ($count < 3)
        <li class="newsitem">$_DateTool.format('MMMM d, yyyy', $articleDate )<br />
        <a href="${articleLink}">$_SerializerTool.serialize($articleTitle, true)</a></li>
        #set ($count = $count + 1)
    #end
    #set ($articleDate = "")
    #set ($articleTitle = "")
    #set ($articleLink = "/")
 #end
</ul>
#else
<p>There are no news stories.</p>
#end
  1. 1 Posted by c-siems on 15 Nov, 2012 10:42 PM

    c-siems's Avatar

    I messed with and found something that works:

    #set ( $articlesize = $articles.size() )
    #if ($articlesize > 0)
    
  2. 2 Posted by Ryan Griffith on 16 Nov, 2012 12:49 PM

    Ryan Griffith's Avatar

    Hi,

    Glad to hear you figured things out. As you indicated, you'll want to use the size() method as there is no attribute for size or length (like there is in JavaScript).

    For more reference, here are some additional methods you can use on collections returned using the XPathTool.

    Have a great day!

  3. Ryan Griffith closed this discussion on 16 Nov, 2012 12:49 PM.

Comments are currently closed for this discussion. You can start a new one.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac