Using if statement in set statement?

Hi all,

{% if dynamic_page_crm_object %}
<details>
 <summary>
 <h5>Research Publications</h5>
 <div class="chevron-icon"></div>
 </summary>
 <div class="content-wrapper">
 {% set publication_types = [ {'type': 'Book', 'heading': 'Books', 'info': "{{ ac_profiles.name_of_author_s_ }} ({{ ac_profiles.year_published }}). <em>{{ ac_profiles.publication_title }}</em>. {{ ac_profiles.books_book_chapters_music__publisher_name_and_place }}<br>"}, {'type': 'Book chapter', 'heading': 'Book Chapters', 'info': "{{ ac_profiles.name_of_author_s_ }} ({{ ac_profiles.year_published }}). <em>{{ ac_profiles.publication_title }}</em> in {{ ac_profiles.book_chapter__book_name }} (pp.{{ ac_profiles.book_chapters_journals__page_numbers }}). {{ ac_profiles.books_book_chapters_music__publisher_name_and_place }}<br>"}, {'type': 'Creative Production or Composition', 'heading': 'Creative Productions or Compositions', 'info': "{{ ac_profiles.name_of_author_s_ }} ({{ ac_profiles.year_published }}). <em>{{ ac_profiles.publication_title }}</em>. {{ ac_profiles.books_book_chapters_music__publisher_name_and_place }} {{ ac_profiles.conference_creative_performance__location }}<br>"}, {'type': 'Journal article', 'heading': 'Journal Articles', 'info': "{{ ac_profiles.name_of_author_s_ }} ({{ ac_profiles.year_published }}). <em>{{ ac_profiles.publication_title }}</em>. {{ ac_profiles.books_book_chapters_music__publisher_name_and_place }} {{ ac_profiles.journal__name }} {{ ac_profiles.journal__volume_and_issue }}<br>"} ] %}
 {% for publication_type in publication_types %}
 {% set associated_objects = crm_associations(dynamic_page_crm_object.id, "USER_DEFINED", 209, "research_or_scholarship_publication_=Research Publication&type_of_publication=" ~ publication_type.type ~ "&visible_publication=true&orderBy=-year_published", "publication_title, year_published, books_book_chapters_music__publisher_name_and_place, name_of_author_s_, book_chapter__book_name, book_chapters_journals__page_numbers, conference_creative_performance__name_of, conference_creative_performance__location, publication_external_link, journal__name, journal__volume_and_issue") %}
 <h6>{{ publication_type.heading }}</h6>
 {% for ac_profiles in associated_objects.results %}
 {{ publication_type.info }}
 {% endfor %}
 <br>
 {% endfor %}
 </div>
</details>
{% endif %}

I’m using the above code to produce listings of academic publications on the site I’m creating. Here’s an example: https://8830131.hs-sites.com/people/publications/jeffrey-aernie?hsLang=en.
I’d like to only display publication headings if the academic has produced any of that type and also if only some of the fields are filled in (e.g. if page number may or may not be applicable) then if field is blank I want to not display this part of the string and move on.

Because the code is so tightly compacted, is there a way to achieve this with the existing way the code is? Can someone please recommend a way forward?
Thanks in advance :slightly_smiling_face:

Hi, @Luke-Simp :waving_hand: Thank you for reaching out! And thank you for including your code example and goals, it helps the community a lot :raising_hands: Let’s invite some of our community members to the conversation — @Anton @Indra @Stephanie-OG @ChristinaKay, do you have any suggestions for @Luke-Simp? Or thoughts on how you might tackle this differently?

Thank you very much for taking a look! — Jaycee

Sorry to jump in here without being summoned :slightly_smiling_face: Hi @Luke-Simp, separating where you grab and format the data, and where you display it will help. This will allow you to add if statements if a piece of data is present or not. I’d do something like this (I added an example if statement to the page numbers for ‘Book chapter’:

{% if dynamic_page_crm_object %}
{% set publication_types = [ 
 {
 'type': 'Book', 
 'heading': 'Books', 
 'publications': []
 },
 {
 'type': 'Book chapter',
 'heading': 'Book Chapters', 
 'publications': []
 },
 {
 'type': 'Creative Production or Composition',
 'heading': 'Creative Productions or Compositions',
 'publications': []
 },
 {
 'type': 'Journal article',
 'heading': 'Journal Articles',
 'publications': []
 }
 ] 
%}

{% for publication_type in publication_types %}
 {% set associated_objects = crm_associations(dynamic_page_crm_object.id, "USER_DEFINED", 209, "research_or_scholarship_publication_=Research Publication&type_of_publication=" ~ publication_type.type ~ "&visible_publication=true&orderBy=-year_published", "publication_title, year_published, books_book_chapters_music__publisher_name_and_place, name_of_author_s_, book_chapter__book_name, book_chapters_journals__page_numbers, conference_creative_performance__name_of, conference_creative_performance__location, publication_external_link, journal__name, journal__volume_and_issue") %}
 {% for ac_profiles in associated_objects.results %}
 {% set publication = {
 'name_of_author_s_': ''~ac_profiles.name_of_author_s_~'',
 'publication_title': ''~ac_profiles.publication_title~'',
 'year_published': ''~ac_profiles.year_published~'',
 'book_chapters_journals__page_numbers': ''~ac_profiles.book_chapters_journals__page_numbers~'',
 'books_book_chapters_music__publisher_name_and_place': ''~ac_profiles.books_book_chapters_music__publisher_name_and_place~'',
 'conference_creative_performance__name_of': ''~ac_profiles.conference_creative_performance__name_of~'',
 'conference_creative_performance__location': ''~ac_profiles.conference_creative_performance__location~'',
 'publication_external_link': ''~ac_profiles.publication_external_link~'',
 'journal__name': ''~ac_profiles.journal__name~'',
 'journal__volume_and_issue': ''~ac_profiles.journal__volume_and_issue~''
 } 
 %}
 {% do publication_type.publications.append(publication) %}
 {% endfor %}
{% endfor %}
<details>
 <summary>
 <h5>Research Publications</h5>
 <div class="chevron-icon"></div>
 </summary>
 <div class="content-wrapper">
 {% for publication_type in publication_types %}
 <h6>{{ publication_type.heading }}</h6>
 {% for publication in publication_type.publications %}
 {% if publication_type.type == 'Book' %}
 <p>{{ publication.name_of_author_s }} ({{ publication.publication_title }}). <em>{{ publication.publication_title }}</em>. {{ publication.books_book_chapters_music__publisher_name_and_place }}</p>
 }
 {% elif publication_type.type == 'Book chapter' %}
 <p>{{ publication.name_of_author_s_ }} ({{ publication.year_published }}). <em>{{ publication.publication_title }}</em> in {{ publication.book_chapter__book_name }} {% if publication.book_chapters_journals__page_numbers|length > 0 %}(pp.{{ publication.book_chapters_journals__page_numbers }}){% endif %}. {{ publication.books_book_chapters_music__publisher_name_and_place }}</p>
 {% elif publication_type.type == 'Creative Production or Composition' %}
 <p>{{ publication.name_of_author_s_ }} ({{ publication.year_published }}). <em>{{ publication.publication_title }}</em>. {{ publication.books_book_chapters_music__publisher_name_and_place }} {{ publication.conference_creative_performance__location }}</p>
 {% elif publication_type.type == 'Journal article' %}
 <p>{{ publication.name_of_author_s_ }} ({{ publication.year_published }}). <em>{{ publication.publication_title }}</em>. {{ publication.books_book_chapters_music__publisher_name_and_place }} {{ publication.journal__name }} {{ publication.journal__volume_and_issue }}</p>
 {% endif %}
 {% endfor %}
 <br>
 {% endfor %}
 </div>
</details>
{% endif %}

Edit: small change, formatted the publication object items like this 'name_of_author_s_': ''~ac_profiles.name_of_author_s_~'', so if it doesn’t exist it will be an empty string. This means you don’t have to write multiple object options based on the type of publication and you can use |length > 0 for your if statements

Thanks so much for the thoughtful response! Seems to do the trick. Really helpful for me as a novice.