CMS Development

jestlinbaum
Participant

HubDB Dynamic page AND Multipage Dynamic on same template

I have successfully created a dynamic page template using the following code.

 

{% if dynamic_page_hubdb_row %}
Stuff for individual page
{% elif dynamic_page_hubdb_table_id %}
{% for row in hubdb_table_rows(dynamic_page_hubdb_table_id) %}
Stuff for listing Page
{% endfor %}
{% endif %}

I have also successfully made a multipage dynamic template using this:

 

{% if dynamic_page_route_level == 0 %}
	Top Level Template
{% elif dynamic_page_route_level == 1 %}
	Parent table template (/food /beverage)
{% elif dynamic_page_route_level == 2 %}
	Child table template (/food/banana etc., /beverage/soda etc.)
{% endif %}  

Is there a way to get the functionality of each into the same template? Meaning, if row 1 of my parent DB has a child table, treat the row like the "route_level" example.

 

But if row 2 does not have a child table, go right to the dynamically created page.

 

I was trying to figure out how to do it by checking if a row contains a child table, but I'm not having much luck.

 

Any help is greatly appreciated!

 

Thanks!

0 Upvotes
1 Reply 1
lscanlan
HubSpot Alumni
HubSpot Alumni

HubDB Dynamic page AND Multipage Dynamic on same template

Hi @jestlinbaum,

 

I've been playing around with multilevel dynamic tables. I've tried to answer your question below given what I think you're trying to do. But of course feel free to link me to your page(s) if I've missed the mark. This is fairly conceptual and maybe I need something a bit more tangible to look at.

 

When you're on a dynamic page whose table row has a child table, you'll have access to that child table through the hs_child_table_id attribute. You can access that with {{ dynamic_page_hubdb_row.hs_child_table_id }} . You could then use the hubdb_table_rows() function to generate a dict of the child table's rows with something like this:

 

{% if dynamic_page_hubdb_row.hs_child_table_id %}
  {% set child_table_rows = hubdb_table_rows(dynamic_page_hubdb_row.hs_child_table_id) %}
  {% for row in child_table_rows %}
    {# pretty print each row in the child table: #}
    <p>row|pprint: {{ row|pprint }}</p>
  {% endfor %} {# end for row in child_table_rows #}
{% endif %} {# end if dynamic_page_hubdb_row.hs_child_table_id #}


So for example if row 1 of the top-level table has a child table, and you wanted to immediately display that info without linking to those dynamic pages, but then if row 2 doesn't have a child table, and at the same level you wanted to display data from its row, you would do something like this:

 

{% if dynamic_page_hubdb_row %}

  {% if dynamic_page_hubdb_row.hs_child_table_id %}
    {% set child_table_rows = hubdb_table_rows(dynamic_page_hubdb_row.hs_child_table_id) %}
    {% for row in child_table_rows %}
      {# pretty print each row in the child table: #}
      <p>row|pprint: {{ row|pprint }}</p>
    {% endfor %} {# end for row in child_table_rows #}
  {% else %} {# for a row that doesn't have a hs_child_table_id #}
    {# pretty print this row #}
    <p>dynamic_page_hubdb_row|pprint: {{ dynamic_page_hubdb_row|pprint }}</p>
  {% endif %} {# end if dynamic_page_hubdb_row.hs_child_table_id #}

{% elif dynamic_page_hubdb_table_id %}

  {% for row in hubdb_table_rows(dynamic_page_hubdb_table_id) %}
    {# Stuff for listing Page #}
  {% endfor %}

{% endif %} {# end if dynamic_page_hubdb_row #}


You can see how if we've found a hs_child_table_id on our dynamic_page_hudb_row, then we have access to all of the data in that child table. You could create logic inside that loop to print whatever you want on the page you're on. Additionally, you could add checks to make sure you're on the correct dynamic_page_route_level . So for example something like:

 

{% if dynamic_page_hubdb_row.hs_child_table_id and dynamic_page_route_level == 1 %}

 

I think the logic can get fairly complex here, but it should be possible. I hope this helps. And of course let me know if you have questions or if you were able to solve things differently. Or if you're actually trying to do something else, I'm happy to take a look.

 

 - Leland

Leland Scanlan

HubSpot Developer Support
0 Upvotes