# How to conditionally use template column

**URL:** <https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860>\
**Category:** Ask for Help\
**Created:** [August 9, 2023, 4:40pm UTC](https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860 "2023-08-09T16:40:20Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![csokolove](https://avatars.discourse-cdn.com/v4/letter/c/d6d6ee/32.png) [@csokolove](https://community.glideapps.com/u/csokolove)\
**Post date:** [August 9, 2023, 4:40pm UTC](https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860/1 "2023-08-09T16:40:20Z")

</div>

I have these columns for first name, preferred name, and last name that all flows into Full Name where it is formatted as:

`FirstName (PreferredName) LastName`

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/d/6/d6b2c3486d121ab9cec24ea82bca5032891d20bb.png)

However, sometimes someone may not have a preferred name. Is there a way to not include the parentheses if the preferred name is blank?

This is my template currently:  
 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/9/3/9379dc84c2a74299e3e5999b852baa57e7c14e83.png)

---

<div class="post-metadata">

**Author:** ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)\
**Post date:** [August 9, 2023, 4:45pm UTC](https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860/2 "2023-08-09T16:45:42Z")

</div>

Create a separate template column that adds the parentheses to the Preferred Name.  
Then create an if-then-else column as follows:

- if Preferred Name is not empty, then template column

And then use that if-then-else column in your final template

---

<div class="post-metadata">

**Author:** ![Dilon\_Perera](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/dilon_perera/32/85652_2.png) [@Dilon\_Perera](https://community.glideapps.com/u/Dilon_Perera)\
**Post date:** [August 9, 2023, 6:24pm UTC](https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860/3 "2023-08-09T18:24:52Z")

</div>

Just tried to achieve this with a one column using JS and here’s what I got! 😃

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/a/d/ade01a569c0b3fa8cd7bbebf35e88f1193868774.png)

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/a/7/a7d18efbabcb8cef151973a25d7eae666150acd9.png)

```auto
function getFullName(firstName, preferredName = "", lastName) {
  let fullName = "";

  if (preferredName === "") {
    fullName = lastName !== "" ? `${firstName} ${lastName}` : firstName;
  } else {
    fullName = lastName !== "" ? `${firstName} ${preferredName} ${lastName}` : `${firstName} ${preferredName}`;
  }

  return fullName;
}

const result = getFullName(p1, p2, p3);

const parts = result.split(' ').filter(part => part !== 'undefined');
const cleanedResult = parts.length === 1 ? parts[0] : parts.join(' ');

return cleanedResult;

```

Thank you

---

<div class="post-metadata">

**Author:** ![ThinhDinh](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/thinhdinh/32/49_2.png) [@ThinhDinh](https://community.glideapps.com/u/ThinhDinh)\
**Post date:** [August 10, 2023, 12:46am UTC](https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860/4 "2023-08-10T00:46:54Z")

</div>

Here’s a shorter version.

```auto
const getFullName = (firstName, preferredName = '', lastName) => 
  (firstName || '') + (preferredName ? ` (${preferredName})` : '') + (lastName ? ` ${lastName}` : '');

return getFullName(p1, p2, p3);

```

Using ternary and logical OR operators can simplify it.

 ![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/b/c/bcdc2f13f54d56ab1f6a2ddac8c7884b2212d845.png)

---

<div class="post-metadata">

**Author:** ![Dilon\_Perera](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/dilon_perera/32/85652_2.png) [@Dilon\_Perera](https://community.glideapps.com/u/Dilon_Perera)\
**Post date:** [August 10, 2023, 3:16am UTC](https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860/5 "2023-08-10T03:16:13Z")

</div>

Awesome Thinh! Thanks for sharing this! Still I’m learning JS in school and I hope one day I can also short my long codes 🙌😃

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [August 17, 2023, 3:16am UTC](https://community.glideapps.com/t/how-to-conditionally-use-template-column/64860/6 "2023-08-17T03:16:23Z")

</div>

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.
