# Using if statement in set statement?

**URL:** <https://community.hubspot.com/t/using-if-statement-in-set-statement/79893>\
**Category:** CMS Development\
**Tags:** design-manager, html, enterprise\
**Created:** [April 24, 2023, 5:39am UTC](https://community.hubspot.com/t/using-if-statement-in-set-statement/79893 "2023-04-24T05:39:35Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Luke-Simp](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/luke-simp/32/74544_2.png) [@Luke-Simp](https://community.hubspot.com/u/Luke-Simp)\
**Post date:** [April 24, 2023, 5:39am UTC](https://community.hubspot.com/t/using-if-statement-in-set-statement/79893/1 "2023-04-24T05:39:35Z")

</div>

Hi all,

```lia-code-sample
{% 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](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 🙂

---

<div class="post-metadata">

**Author:** ![Jaycee\_Lewis](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/jaycee_lewis/32/61352_2.png) [@Jaycee\_Lewis](https://community.hubspot.com/u/Jaycee_Lewis)\
**Post date:** [April 24, 2023, 6:48pm UTC](https://community.hubspot.com/t/using-if-statement-in-set-statement/79893/2 "2023-04-24T18:48:51Z")

</div>

Hi, @Luke-Simp 👋 Thank you for reaching out! And thank you for including your code example and goals, it helps the community a lot 🙌 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

---

<div class="post-metadata">

**Author:** ![piersg](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/piersg/32/99782_2.png) [@piersg](https://community.hubspot.com/u/piersg)\
**Post date:** [April 25, 2023, 11:55am UTC](https://community.hubspot.com/t/using-if-statement-in-set-statement/79893/3 "2023-04-25T11:55:39Z")

</div>

Sorry to jump in here without being summoned 🙂 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’:

```lia-code-sample
{% 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

---

<div class="post-metadata">

**Author:** ![Luke-Simp](https://d10pqhm3lwygmk.cloudfront.net/user_avatar/community.hubspot.com/luke-simp/32/74544_2.png) [@Luke-Simp](https://community.hubspot.com/u/Luke-Simp)\
**Post date:** [May 2, 2023, 10:28am UTC](https://community.hubspot.com/t/using-if-statement-in-set-statement/79893/4 "2023-05-02T10:28:22Z")

</div>

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