CMS Development

benji_thecroc
Contributor | Gold Partner
Contributor | Gold Partner

Creating dynamic list of blog posts based on custom property values

SOLVE

Hi,

 

So we have a custom property where a contact can set category preferences. In the HUBL I grab the contact's content preferences property and the set conditional variables like so -

 

{% set string_to_split = contact.content_preferences %}
{% set category_string1 = string_to_split|replace(" ", "") %}
{% set category_string2 = category_string1|replace(",", " + ") %}

{% set string1 = "Culture&Behaviour" %}
{% set string2 = "DigitalExperiences" %}
{% set string3 = "Data&Technology" %}
{% set string4 = "CustomerBehaviour" %}
{% set string5 = "IndustryInsight" %}

{% if string1 in category_string2 %}
  {% set blog1 = blog_recent_posts('7860078445', limit=5) %}
{% endif%}
{% if string2 in category_string2 %}
  {% set blog2 = blog_recent_posts('11345634639', limit=5) %}
{% endif%}
{% if string3 in category_string2 %}
  {% set blog3 = blog_recent_posts('11402560060', limit=5) %}
{% endif%}
{% if string4 in category_string2 %}
  {% set blog4 = blog_recent_posts('11402555417', limit=5) %}
{% endif%}
{% if string5 in category_string2 %}
  {% set blog5 = blog_recent_posts('11402801845', limit=5) %}
{% endif%}

{% set rec_posts = (blog1 + blog2 + blog3 + blog4 + blog5) | sort(true, false, 'publish_date') %}

 

This issue is, if a user hasn't selected one of more of the categories and one of the blog1, blog2 etc. variables aren't set then rec_posts no longer works.

 

I don't particualrly want to have to write out 25 conditions for all category choice scenarios. Is there an easy way around this?

0 Upvotes
1 Accepted solution
benji_thecroc
Solution
Contributor | Gold Partner
Contributor | Gold Partner

Creating dynamic list of blog posts based on custom property values

SOLVE

Here's the solution I came up with -

 

I just created the blog array variables as empty arrays before the if conditions, then if the category was present in the user preferences and the if conditions returned true it would overrule the original variable decleration -

 

{% set string_to_split = contact.content_preferences %}
{% set category_string1 = string_to_split|replace(" ", "") %}
{% set category_string2 = category_string1|replace(",", " + ") %}

{% set string1 = "Culture&Behaviour" %}
{% set string2 = "DigitalExperiences" %}
{% set string3 = "Data&Technology" %}
{% set string4 = "CustomerBehaviour" %}
{% set string5 = "IndustryInsight" %}

{% set blog1 = [] %}
{% set blog2 = [] %}
{% set blog3 = [] %}
{% set blog4 = [] %}
{% set blog5 = [] %}

{% set new_list = [] %}
{% if string1 in category_string2 %}
  {% set blog1 = blog_recent_posts('7860078445', limit=5) %}
{% endif%}
{% if string2 in category_string2 %}
  {% set blog2 = blog_recent_posts('11345634639', limit=5) %}
{% endif%}
{% if string3 in category_string2 %}
  {% set blog3 = blog_recent_posts('11402560060', limit=5) %}
{% endif%}
{% if string4 in category_string2 %}
  {% set blog4 = blog_recent_posts('11402555417', limit=5) %}
{% endif%}
{% if string5 in category_string2 %}
  {% set blog5 = blog_recent_posts('11402801845', limit=5) %}
{% endif%}

{% set rec_posts = (blog1 + blog2 + blog3 + blog4 + blog5) | sort(true, false, 'publish_date') %}

 

View solution in original post

1 Reply 1
benji_thecroc
Solution
Contributor | Gold Partner
Contributor | Gold Partner

Creating dynamic list of blog posts based on custom property values

SOLVE

Here's the solution I came up with -

 

I just created the blog array variables as empty arrays before the if conditions, then if the category was present in the user preferences and the if conditions returned true it would overrule the original variable decleration -

 

{% set string_to_split = contact.content_preferences %}
{% set category_string1 = string_to_split|replace(" ", "") %}
{% set category_string2 = category_string1|replace(",", " + ") %}

{% set string1 = "Culture&Behaviour" %}
{% set string2 = "DigitalExperiences" %}
{% set string3 = "Data&Technology" %}
{% set string4 = "CustomerBehaviour" %}
{% set string5 = "IndustryInsight" %}

{% set blog1 = [] %}
{% set blog2 = [] %}
{% set blog3 = [] %}
{% set blog4 = [] %}
{% set blog5 = [] %}

{% set new_list = [] %}
{% if string1 in category_string2 %}
  {% set blog1 = blog_recent_posts('7860078445', limit=5) %}
{% endif%}
{% if string2 in category_string2 %}
  {% set blog2 = blog_recent_posts('11345634639', limit=5) %}
{% endif%}
{% if string3 in category_string2 %}
  {% set blog3 = blog_recent_posts('11402560060', limit=5) %}
{% endif%}
{% if string4 in category_string2 %}
  {% set blog4 = blog_recent_posts('11402555417', limit=5) %}
{% endif%}
{% if string5 in category_string2 %}
  {% set blog5 = blog_recent_posts('11402801845', limit=5) %}
{% endif%}

{% set rec_posts = (blog1 + blog2 + blog3 + blog4 + blog5) | sort(true, false, 'publish_date') %}