CMS Development

piersg
Key Advisor

Filter HubDB using path

SOLVE

I've had a look at dynamic team page example (which is for a module, I'm doing a page template, not sure if that makes a difference).

 

I need to change my DB filter from this:

 

{% if request.query_dict.category in ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"] %}
{% set queryparam = queryparam ~ "&category__contains="~request.query_dict.category|urlencode %}
{% endif %}

 

 

To this (based on the dynamic team example):

 

{% if request.path == "/en/products/marketplace/accounting" %}
  {% set queryparam = queryparam ~ "&category=1" %}
{% endif %}
//etc for all categories

 

However, that 404s. The filtering works with fine using query strings, but I've been told we need to use a URL path instead.

 

I know I could do the same thing splitting up the DB into one parent referencing category child tables, but the DB has 20 categories and several of the rows have mulitple categories, which would create duplicate pages under different categories. So that's not ideal.

 

Any help would be much appreciated

0 Upvotes
1 Accepted solution
piersg
Solution
Key Advisor

Filter HubDB using path

SOLVE

Ok so I'm came up with a solution for this.

 

My first thought was to use a parent db that had each category as a row and all had the main DB as a child table. This allowed the path structure I wanted, and I could then filter the main DB depending on the URL. Whatever the URL was, e.g. if it was /accounting at the end, use that by looking up the relevant id and setting that as the query parameter in the table call.

 

However, a requirement was to have individual integrations from the main DB on their own page, not under a category (as some have multiple categories, meaning they would have duplicates), e.g. products/marketplace/[integration] not products/marketplace/accounting/[integration] and the above idea doesn't allow for that.

 

The solution was then a little bit hacky, I duplicated the original DB and I added the categories as rows to that, again setting the child DB as the original main DB. This means the URLs for categories and integrations are all at the same level, as it were. Then I used some if logic to determine whether the URL was one of the categories (e.g. a filtered view) or one of the integrations. If it's one of the categories, it gets the relevant rows from the original DB using the method in the first idea and displays those in the listing view. If it's not, it displays the specific integration page.

 

So, it involves two DBs that have to be updated alongside one another so they match, which is not ideal, but it was that or create a page for each category (still use the one template and DB) which is more work, especially when we come to translate the site.

 

If you're curious: https://www-mews-com.sandbox.hs-sites.com/en/products/marketplace

View solution in original post

3 Replies 3
piersg
Solution
Key Advisor

Filter HubDB using path

SOLVE

Ok so I'm came up with a solution for this.

 

My first thought was to use a parent db that had each category as a row and all had the main DB as a child table. This allowed the path structure I wanted, and I could then filter the main DB depending on the URL. Whatever the URL was, e.g. if it was /accounting at the end, use that by looking up the relevant id and setting that as the query parameter in the table call.

 

However, a requirement was to have individual integrations from the main DB on their own page, not under a category (as some have multiple categories, meaning they would have duplicates), e.g. products/marketplace/[integration] not products/marketplace/accounting/[integration] and the above idea doesn't allow for that.

 

The solution was then a little bit hacky, I duplicated the original DB and I added the categories as rows to that, again setting the child DB as the original main DB. This means the URLs for categories and integrations are all at the same level, as it were. Then I used some if logic to determine whether the URL was one of the categories (e.g. a filtered view) or one of the integrations. If it's one of the categories, it gets the relevant rows from the original DB using the method in the first idea and displays those in the listing view. If it's not, it displays the specific integration page.

 

So, it involves two DBs that have to be updated alongside one another so they match, which is not ideal, but it was that or create a page for each category (still use the one template and DB) which is more work, especially when we come to translate the site.

 

If you're curious: https://www-mews-com.sandbox.hs-sites.com/en/products/marketplace

tjoyce
Recognized Expert | Elite Partner
Recognized Expert | Elite Partner

Filter HubDB using path

SOLVE

@piersg - Are you sure that is the relative path to the page you're on?

This seems like it should be working... can you do 

{{request.path | pprint}}

and see what you get?

 

For instance, when I run this code - I get the correlated category numbers based on the page URI

 

{% 
  set custom_dict = {
    "/v40fullfeature": "1",
    "/inner-module-test": "2"
  }
%}
{{custom_dict[request.path].index}}

 

 

On the 

/v40fullfeature link it prints out 1

and on the /inner-module-test it prints out 2

 

Only thing I can think of is that it either isn't respecting the language selector

or the path you are matching to isn't quite right

0 Upvotes
piersg
Key Advisor

Filter HubDB using path

SOLVE

Thanks @tjoyce.

 

On the path /en/products/marketplace?category=1 {{request.path | pprint}} prints String: /en/products/marketplace (of course).

 

The path /en/products/marketplace/accounting (for example) doesn't exist so I wouldn't be able to print anything. I thought the HubL code example from dynamic team page would essentially create that path/make that path valid and then show the same info as /en/products/marketplace?category=1.

0 Upvotes