Adding time values to a blog post template

Occasional Contributor

I'm working on an approach to adapt blog post mechanics to support a listing of future events (conferences etc) - I have most of the logic identified for input of the extra variables related to each event, including adding city and URL etc as "hidden" text fields in the head markup of the but I seems to be struggling with date inputs. I wish there was a way to invoke a "date picker" (like the options with custom module field inputs) and/or a good way to work with time/date conversions.

The specific things I'm sure some expert here can help me solve in a few seconds are:

- setting up a timedate variable in the blog post context, ideally a date equivalent of {% text "start_date_text" label='Event Start', value="yyyy-MM-dd", export_to_template_context=True %} that would enable me to have a "widget_data" accessible variable for start/end event dates.

- supported formats of the format string in 'strtotime("yyyy-MM-dd'T'HH:mm:ssZ")' so that a user-friendly format of text entry eg "2019-3-20" could be converted to a date or timestamp variable.

- a good way to set these date/timestamp variables so that event posts can be sorted using the "sort" filter on the appropriate data selection.

 

All help/pointers appreciated.

 

Reply
0 Upvotes
8 Replies 8
Community Manager

Hi @SteveHTM,

 

I wanted to provide a few resources as a starting point to assist: 

1. HubL supported variables; there is the {{ content.created }} which allows A datetime object for when the content was originally created, in UTC time. This variable can be formatted with the datetime filter.

2. Add time stamp to blog post template

 

I also want to tag in some subject matter experts to see if they have any input here: @Stephanie-OG@Kevin-C@DaniellePeters do you have any thoughts/suggestions/tips and tricks to help @SteveHTM?

 

Thanks,
Jenny


Did my post help answer your query? Help the Community by marking it as a solution
Reply
0 Upvotes
Occasional Contributor

Jenny - thank you for the pointers here. My challenge is not related to the date that a post is published (or created or expires etyc), but with the start and end dates of a conference, webinar, seinar or other physical event that is being promoted. I've simply used the blog mechanics/framework to set up a series of "posts" about such future events and would like to have an easy way to

- enter such dates when creating the "post"

- display the dates in the "post" content page

- sort on these dates for a "post" listing page to get the events in time order

I've explored a number of options at this point, and each seems extra-ordinarily arcane, especially when I casnt seem to reconcile behavior in the design manager to some of the published documentation.

 

An example:

- Enter a date in a forced structured format as a text string variable - "2019-03-17" for example.  

- Try and convert that to a date variable using the strtotime filter using a format string that matches ""yyyy-MM-dd". The conversion fails for every variant I have explored.

- If It try and concatenate the date entered with a literal string value (for example"'T00:00:00+5:00") to match a known working strtotime format, I get an design manager parsing error flagged.

Its hard to understand and work around the errors that get thrown out in these explorations - and the suppor team defers to this forum to try and sort things out.

 

Steve

Reply
0 Upvotes
Regular Advisor

Hi Steve, 

 

I don't believe there's a way to add an extra variable to the post so that you can then filter posts by that variable (e.g. pull post.my_custom_date on the listings page) but this sounds like a perfect use case for HubDB. In fact, I recall that one of the original examples on their documentation included an event calendar. 

 

You can create a table on HubDB with any number of columns: event title, event date, event description... etc., have a main "listings" page and use dynamic pages to generate a new page for each event. 

 

Aside from the documentation above, the CMS for Developers certification has a section on HubDB that's pretty useful in clarifying the above. 

 

I hope that helps!

 


Stephanie O'Gay Garcia

HubSpot Design / Development

Website | Contact

 

If this helped, please mark it as the solution to your question, thanks!

Occasional Contributor

Again - my thanks for the comment here. I've read up about the use of HubDB for verey custom applications, but as it turns out HubDB is not available at the current level of HS subscription.

 

What continues to surprise me in my investigations is the the addition of custom data to a new "blog" type is pretty routine for some things: text using - {% text "event_city" ..... %}, images {% image "event logo" .....%}, headers, etc. but seemingly impossible for a number or a date? It just feels like I'm missing something conceptual here?

 

Steve

Reply
0 Upvotes
Regular Advisor

Apologies, I'm used to working with the CMS but a lot of users might not have HubDB Smiley Embarassed

 

Because of that I haven't really tried to do anything similar to your use case but I came across this post on the old developer forums that could be useful. I haven't tested it myself but it might help.

 


Stephanie O'Gay Garcia

HubSpot Design / Development

Website | Contact

Reply
0 Upvotes
Occasional Contributor

Stephanie - many thanks agin for ther reference. It acyually serves to illustrate the knowledge gap that I'm trying to fill, since it shows how to add variables of various types to an adapted post (as does this old tutorial ) but does not deal with the specific case of adding a date variable.

 

Hope again for wisdom from an expert that has wrangled this one to the ground!

 

Steve

Reply
0 Upvotes
Regular Advisor

I asked on the Developers Slack (also a great resource if you're not on it) and one developer said that they ended up using a text field and told the content editors they'd need to use the exact date format. You can detect if they're on the page editor and set it to pop up an alert if the date entered isn't in that format. 

 

Anyway, I'll leave this thread in case anyone else who has worked this use case comes along Smiley Happy 

 


Stephanie O'Gay Garcia

HubSpot Design / Development

Website | Contact

Highlighted
Regular Contributor | Platinum Partner

Hi there Steve,


I have done exactly what you are looking for and remember it was actually pretty complex to handle on the blog. Sadly I don't have access to that portal anymore Smiley Frustrated (a good reason to start to save locally custom devs like that).


The main problem I remember is that you are going to need to write your own pagination and that means use blog_recent_posts and sadly it has a 200post limit and there is no workaround for this Smiley Sad

So hopefully thats enough to make it work and its important to tell your client and recomend him remove old/past posts so it doesn't break in the future.

 

Said that, my setup was like:
- One CM with some fields (with the date field here) that you need to add to the template (it can have empty the HTML tab so thats fine).
From the listing tab in the blog template you can call your data with something like:

widgets.module_XXXXXXX.body.field_slug

Where module_XXXXXXX is the block ID for that template (You can find it in the developer info).

Generate the list should be something like:

{% set all_posts_unsorted = blog_recent_posts( content.parent_blog.id, 200 ) %}

From here you should be able to sort with sort filter (or maybe map) like:

{% set all_posts_sorted = all_posts_unsorted|dictsort(false, by='widgets.module_XXXXXXX.body.field_slug') %}

And now you will need to split the dict and generate the pagination with current_page_num.