CMS Development

Philip_Marsh
投稿者

Multilevel dynamic pages with HubDB - Recent products

解決

Hi,

 

I'm reading through this tutorial, and for the most part, it does what I need it to do

 

How to build multilevel dynamic pages with HubDB (hubspot.com)

 

An area, I'm not sure on is recent additions to the DB. What I'm looking to do is create a portion of the page that displays the last 5 additions to any of the HubDB's. 

 

Using that tutorial, you can just loop through all of the Childs DB's and have the page populate with all the products. It's then fairly straight forward to restrict it to only display 5/6 entries. What I'm not sure on is how to only display the last 5 products added to any of the DB's. If this was using a single DB, that's fairly straight forward but I'm not sure how you would accomplish this across multiple Child DB's.

 

Is that even possible?

 

Phil

0 いいね!
1件の承認済みベストアンサー
piersg
解決策
キーアドバイザー

Multilevel dynamic pages with HubDB - Recent products

解決

Yep, exactly 👍

{% do childCombineArr.append({'date': date, 'row': row}) %} is the line that adds the child db row info to the array. 'row' is just all the info in each row, and will be an object itself, but the info you want to sort (in my case date) by has to be added outside of the row object, as it were, to be used by the sort function on childCombineArr. So it returns 

[{date=1594717200000, row={id=[id], createdAt=1597401699900, path='[path]', etc... }]

 

For you because you want to sort by the latest 5 products added you could use createdAt instead of date and then use if loop.index is less than or equal to 5 e.g.

{% set childCombineArr = [] %}
{% for row in table %}
  {% if row.hs_child_table_id %}
    {% set child_table = hubdb_table_rows(row.hs_child_table_id) %}
    {% for row in child_table %}
      {% set timeAdded = row.createdAt %}
      {% do childCombineArr.append({'timeAdded': timeAdded, 'row': row}) %}
    {% endfor %}
  {% endif %}
{% endfor %}
{% for item in childCombineArr|sort(True, False, 'timeAdded') %}
  {% if loop.index <= 5 %}
    {% set row = item.row %}
    {{row}}
  {% endif %}
{% endfor %}

 

元の投稿で解決策を見る

7件の返信
natsumimori
コミュニティーマネージャー
コミュニティーマネージャー

Multilevel dynamic pages with HubDB - Recent products

解決

Thank you so much @tjoyce and @piersg !!

0 いいね!
piersg
キーアドバイザー

Multilevel dynamic pages with HubDB - Recent products

解決

Hi @Philip_Marsh (thanks @tjoyce). Something I did to sort multiple child DBs by date could be useful for you. Basically combine them into a single array then sort that resulting array.

 

{% set childCombineArr = [] %}
{% for row in table %}
  {% if row.hs_child_table_id %}
    {% set child_table = hubdb_table_rows(row.hs_child_table_id) %}
    {% for row in child_table %}
      {% set date = row.start_datetime %}
        {% do childCombineArr.append({'date': date, 'row': row}) %}
    {% endfor %}
  {% endif %}
{% endfor %}
{% for item in childCombineArr|sort(True, False, 'date') %}
  {% set row = item.row %} // allows you to use standard row.title as if it were for "row in table" instead of item.row.title
  {{row.title}}
  {{row.start_datetime|datetimeformat('%e %B %Y')}}
  //...etc
{% endfor %}

 

Philip_Marsh
投稿者

Multilevel dynamic pages with HubDB - Recent products

解決

Thank you, I'll give that a try. 

 

I haven't array's before now in Hubl. Am I right in breaking this down. 

{% set childCombineArr = [] %}

This creates an empty Array, we then initiate a loop to go through the child DB's and adds that information to an array. 

{% for item in childCombineArr|sort(True, False, 'date') %}

We then create a second loop which uses the data in the array to print to the screen, but after it has sorted it by date?

 

Thanks

0 いいね!
piersg
解決策
キーアドバイザー

Multilevel dynamic pages with HubDB - Recent products

解決

Yep, exactly 👍

{% do childCombineArr.append({'date': date, 'row': row}) %} is the line that adds the child db row info to the array. 'row' is just all the info in each row, and will be an object itself, but the info you want to sort (in my case date) by has to be added outside of the row object, as it were, to be used by the sort function on childCombineArr. So it returns 

[{date=1594717200000, row={id=[id], createdAt=1597401699900, path='[path]', etc... }]

 

For you because you want to sort by the latest 5 products added you could use createdAt instead of date and then use if loop.index is less than or equal to 5 e.g.

{% set childCombineArr = [] %}
{% for row in table %}
  {% if row.hs_child_table_id %}
    {% set child_table = hubdb_table_rows(row.hs_child_table_id) %}
    {% for row in child_table %}
      {% set timeAdded = row.createdAt %}
      {% do childCombineArr.append({'timeAdded': timeAdded, 'row': row}) %}
    {% endfor %}
  {% endif %}
{% endfor %}
{% for item in childCombineArr|sort(True, False, 'timeAdded') %}
  {% if loop.index <= 5 %}
    {% set row = item.row %}
    {{row}}
  {% endif %}
{% endfor %}

 

Philip_Marsh
投稿者

Multilevel dynamic pages with HubDB - Recent products

解決

Thank you. Perfect

0 いいね!
tjoyce
名誉エキスパート | Elite Partner
名誉エキスパート | Elite Partner

Multilevel dynamic pages with HubDB - Recent products

解決

@piersg - might have some insight on this topic

natsumimori
コミュニティーマネージャー
コミュニティーマネージャー

Multilevel dynamic pages with HubDB - Recent products

解決

@tjoyce and @thesnappingdog , are you guys familiar with this topic? Would you mind sharing your advice for @Philip_Marsh ?