XF 2.1 Include Javascript depending on date

TiKu

Member
Hi,

I want my add-on to load a js file depending on the time of year. It shall be included only between November 27th and January 6th of the next year.
I did not succeed testing for this date range with pure template conditionals (xf:if), so now I'm trying to achieve the desired result with a callback function (xf:callback). It works, but I have to build the script file's path by myself and cannot use vb:js.
So is there either a way how to check for the date range with pure template conditionals, or how to reuse the logic that the templater does use to create the public path to the js file?

Best regards
TiKu
 
The current Unix timestamp is exposed to the templates via {$xf.time}. You can find the timestamps that match your window and add a condition based on the current time being between them.
 
Thank you. I got a little bit further. With the following code I can define variables that contain the start and end date adjusted to the current year:
Code:
    <xf:set var="$currentYear" value="{{date($xf.time, 'Y')}}" />
    <xf:set var="$showFromStr" value="{$currentYear}-11-27 00:00:00.0" />
    <xf:set var="$showToStr" value="{$currentYear}-01-07 00:00:00.0" />
But to compare it against $xf.time within an xf:if, I would need to parse this string into a timestamp. I tried to use strtotime, but xenForo complains that it is unknown:
Code:
    <xf:set var="$showFrom" value="{{strtotime({$showFromStr})}}" />
 
Okay, I've found out how to do it:
Code:
    <xf:set var="$showFrom" value="{{date_from_format('m-d H:i:s', '11-27 00:00:00')}}" />
    <xf:set var="$showTo" value="{{date_from_format('m-d H:i:s', '01-07 00:00:00')}}" />
    <xf:if is="$xf.time >= $showFrom->getTimestamp() || $xf.time < $showTo->getTimestamp()">
        ...
    </xf:if>
 
Top Bottom