I am using Moment.js with Twig for calculating Time ago,
Code (Twig) –
I have date in post_date_gmt variable and i am using it like,
<div class="time">
<time datetime="{{ post_date_gmt| date('Y-m-d H:i:s')}}">moment.unix({{ post_date_gmt }}).local().fromNow()</time>
</div>
This give me output :
<div class="time">
moment.unix(1331845445).local().fromNow()
</div>
When i try to run above string in console it worked fine – ‘a month ago‘.
I don’t understand why twig is not giving me the correct output?
Am i doing something wrong?. Thanks for your time.
moment is a javascript function and you’re trying to call it from a block of HTML instead of a block of javascript. There are several ways you could do it, but an easy hack would be:
I don’t recommend using document.write like that though. It would be better to do this as a separate block of javascript sort of like this (using jquery as an example):