Display individual columns from one HubDB table row

DChadney
Member

I have created an employee listing page from a HubDB table and wish to display the details of each employee on a separate detail page. For the life of me I cannot find anything in the documentation that explains how to do this. Everything only shows how to list everything in a table. I only want to show one row of a table on a page.

 

Here's the code I am trying to get to work:

{% if dynamic_page_hubdb_table_id %}
{% elif dynamic_page_hubdb_row %}        
    <h1>{{ dynamic_page_hubdb_row.hs_name }}</h1>
    <p>{{ dynamic_page_hubdb_row.name }}</p>
    <p>{{ dynamic_page_hubdb_row.position }}</p>
     {% for row in hubdb_table_rows(4357029) %}
           <p>{{ dynamic_page_hubdb_row.name }}</p>
     {% endfor %}
{% endif %}

 

I have a table selected on the page settings, but nothing displays.

0 Upvotes
6 Replies 6
piersg
Key Advisor

Hi @DChadney, have you enabled the settings in the HubDB to do this? Go to Actions > Manage settings and make sure "Enable creation of dynamic pages using row data" is on. This will add two extra columns to you DB, Page title (as in meta title) and Page path (as in URL path). 

DChadney
Member

@piersg Yes this is already enabled and the two columns hs_name and hs_path are present. I'm just new to Hubl and not sure how to set up the code to show individual rows.

piersg
Key Advisor

Have you put values in the hs_name and hs_path columns (unique for each row)? Just checking 🙂

If so, try it the other way round, testing if the user is viewing a dynamically created sub-page (row) before the listing page (table), see if that works:

 

{% if dynamic_page_hubdb_row %}
  {# detail page #}
  {% set row = dynamic_page_hubdb_row %}
  <h1>{{ row.hs_name }}</h1>
  <p>{{ row.name }}</p>
  <p>{{ row.position }}</p>
{% elif dynamic_page_hubdb_table_id %}
  {# listings page #}
  {% set table_id = dynamic_page_hubdb_table_id %}
  {% set table = hubdb_table_rows(table_id) %}
  {% if table == [] %}
    <p>Sorry, something went wrong!</p>
  {% else %}
    {# all employees #}
    {% for row in table|sort(False, False, 'name') %}
      <p>{{row.name}}</p>
    {% endfor %}
  {% endif %}
[% endif %}

 

 

DChadney
Member

@piersg - Thanks for your reply. I tried your code snippet  but it is showing up as a syntax error for this line: 

{% if dynamic_page_hubdb_row %}

 

I've double checked and there are definately values for those columns - hs_name and hs_path

 

DChadney_0-1621971803012.png

 

0 Upvotes
piersg
Key Advisor

@DChadney Ah, you're using a module. You can't use a module for dynamically generated pages, it has to be a full HTML+HubL page template.

webdew
Top Contributor | Diamond Partner

Hi @DChadney ,

Use this code and should page published then hubdb content seem.

{% if dynamic_page_hubdb_row %}
<h4>About {{ dynamic_page_hubdb_row.name }}</h4>
<!-- detail page code -->
{% elif dynamic_page_hubdb_table_id %}

<!-- listing page code -->
{% for row in hubdb_table_rows(dynamic_page_hubdb_table_id) %}
<a href="{{ request.path }}/{{ row.hs_path }}">
<img src="{{ row.logo.url }}" width="500" {% if row.image.width row.image.height %}class="landscape" {% endif %} alt="{{ row.name }} ">
<p><a href="{{ request.path }}/{{ row.hs_path }}">
{{ row.domain_name }}</a></p> {% endfor %} {% endif %}

Hope this helps!

If we were able to answer your query, kindly help the community by marking it as a solution.

Thanks and Regards. 



0 Upvotes