set variable- gather upcoming events by timestamp

Sarah Johnson Li's Avatar

Sarah Johnson Li

20 Sep, 2012 06:38 PM

Hello,

I could easily do this in XSLT, but I'm trying to use Velocity for the count/looping benefits for the rest of the format...

I'm trying to create variable of all upcoming events by comparing the current time and the event date from the page, then push this variable into a macro to render the event information.

I was able to make a set of Featured events this way:

#set ( $features = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page/dynamic-metadata[value='Yes']") )

But I'm not sure how to make a well-formed $upcoming variable by comparing the current time and the page's event date (if it's even possible).

Thanks in advance!
Sarah

  1. 1 Posted by Ryan Griffith on 20 Sep, 2012 07:17 PM

    Ryan Griffith's Avatar

    Hi Sarah,

    You should be able to use the DateTool to get the current timestamp (of type long) and compare it to the either the Page's metadata or a Date chooser in a Data Definition.

    Something like the following should compare the current system date with a Date chooser field from the Data Definition:

    ## Return the system's current time as the number of milliseconds.
    #set ( $currentTime = $_DateTool.getSystemTime() )
    
    ## Get pages with a "thedate" that is greater than or equal to the current time.
    #set ( $upcoming = $_XPathTool.selectNodes($contentRoot, "//system-page[system-data-structure/thedate >= " + $currentTime + "]") )
    

    Please let me know if you have any questions.

    Thanks!

  2. 2 Posted by Sarah Johnson L... on 20 Sep, 2012 07:34 PM

    Sarah Johnson Li's Avatar

    Hey Ryan!

    I got the following error:

    Velocity content invalid: Lexical error, Encountered: "+" (43), after : "" at unset[line 34, column 148]

    This is line 34:

    #set ( $upcoming = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[system-data-structure/sidebar/event-info/event-date >= " + $currtime + "]") )
    

    This is $currtime:

    #set ( $currtime = $_XPathTool.selectSingleNode($contentRoot, "/system-index-block/@current-time").value )
    

    I'll play around some more, but let me know what you think. It could just be me staring at this for too long! (we are on v7.0.2, upgrading to 7.0.4 on Sunday)

    Thanks,
    Sarah

  3. 3 Posted by Ryan Griffith on 20 Sep, 2012 07:52 PM

    Ryan Griffith's Avatar

    Hm, guess it doesn't like the string concatenation. Let's try the following:

    #set ( $upcoming = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[system-data-structure/sidebar/event-info/event-date >= ${currtime}]") )
    
  4. 4 Posted by Sarah Johnson L... on 20 Sep, 2012 08:18 PM

    Sarah Johnson Li's Avatar

    That was valid! Thank you very much!

    Is this the proper way to call a macro?...

    #macro ( showEvent $pgs )
    ...
    #end
    ...
    #set ( $featured = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[dynamic-metadata[value='Yes']]") )
    #set ( $upcoming = $_XPathTool.selectNodes($contentRoot, "/system-index-block/system-page[system-data-structure/sidebar/event-info/event-date >= ${currtime}]") )
    ...
    #if(${featured.size()} > 0)
        <h3>Featured Events</h3>
        #showEvent($featured)
    #end
    
  5. 5 Posted by Ryan Griffith on 20 Sep, 2012 08:20 PM

    Ryan Griffith's Avatar

    Glad to hear that worked. The macro and the call look right to me.

  6. 6 Posted by Sarah Johnson L... on 21 Sep, 2012 07:01 PM

    Sarah Johnson Li's Avatar

    Hi Ryan,

    Thanks for confirming! The reason I checked about the macro and call was that the macro wasn't firing for me. I came in this morning and the same macro (code untouched) was rendering content no problem. I just encountered something similar today where I added text in Velocity to display and the page has not updated. Is this related to the caching in 7.x or the memory leak with Velocity?

    We're in 7.0.2, Windows 7 Firefox 15 and Chrome 21 both displayed similar issues, even after browser cache was cleared.

  7. 7 Posted by Ryan Griffith on 21 Sep, 2012 07:06 PM

    Ryan Griffith's Avatar

    Ah yes, there is a known defect with Cascade 7.0.2 in which macros were caching and not being released properly. This sounds like what might be causing the issue. Upgrading to Cascade 7.0.3+ should fix this behavior.

  8. 8 Posted by Sarah Johnson L... on 21 Sep, 2012 07:24 PM

    Sarah Johnson Li's Avatar

    Oh good! We are updating to 7.0.4 over the weekend so that's fine. Thanks so much Ryan!!!

  9. 9 Posted by Ryan Griffith on 21 Sep, 2012 07:38 PM

    Ryan Griffith's Avatar

    Not a problem, let us know if the upgrade doesn't take care of that.

    Have a good weekend!

  10. Ryan Griffith closed this discussion on 21 Sep, 2012 07:38 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