Answers.Body: <p>I suggested an edit to Jamies suggestion (hope that's okay - don't know all the rules yet).
  Here's the suggestion</p>
  
  <pre><code>{% set title = (entry.artistFirstName) ? [entry.artistFirstName, ' ', entry.title]|join : entry.title %}
  {{ title | capitalize }}
  </code></pre>
  
  <p>Here i'm joining the two variables if entry.artistFirstName exists, by first putting all the parts into an array, and then using the join filter to glue them together. I like this approach, because it's easy to add many parts without having to put ~ ' ' ~ for every space you need.</p>
  
  <p>In general, if you want to glue strings like this together, you use the ~ operator, like this:</p>
  
  <pre><code>{{ 'string1' ~ 'string2' ~ 'string3' }} -&gt; {{ string1string2string3 }}
  </code></pre>
  
  <p>An alternative approach to the one I suggest above is therefore like so:</p>
  
  <pre><code>{% set title = (entry.artistFirstName) ? entry.artistFirstName ~ ' ' ~ entry.title : entry.title %}
  </code></pre>
  
  <p>This is just as fine, but the first approach might be nicer if you have more parts.</p>
  
  <p>A third way is this, which might be just as nice/clean, but maybe slightly less readable?</p>
  
  <pre><code>{% set title = (entry.artistFirstName) ? '%s - %s'|format(entry.artistFirstName, entry.title) : entry.title %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-16T20:53:18.710Z
Answers.LastActivityDate: 2014-09-16T23:04:06.420Z
Answers.LastEditDate: 2014-09-16T23:04:06.420Z
Answers.UserDisplayName: Fred Carlsen
Answers.Body: <p>How about this?</p>
  
  <p>We assign the result of a conditional to a variable called <code>title</code>. The conditional checks if <code>entry.artistFirstName</code> is not empty. If true we assign it to the <code>title</code> variable, if false we fallback to assigning <code>entry.title</code> instead.</p>
  
  <pre><code>{% set title = (entry.artistFirstName) ? entry.artistFirstName ~ ' ' ~ entry.title : entry.title %}
  
  {{ title | capitalize }}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-16T20:20:09.307Z
Answers.Text: Thanks! But it seems to not work for me. If _entry.artistFirstName_ is defined it only outputs _entry.artistFirstName_ instead of _entry.artistFirstName + entry.title_.
Answers.UserDisplayName: Romain P.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-16T21:49:30.247Z
Answers.Text: Ok I edited my original reply. Hopefully you can work out the exact formatting you want from that.
Answers.UserDisplayName: Jamie Pittock
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-17T20:06:28.033Z
Answers.Text: Thank you very much, now it works! But I validate the answer from Fred Carlsen which is more complete, and which could be useful to others in a much larger number of cases.
Answers.UserDisplayName: Romain P.
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2014-09-16T20:11:45.240Z
Answers.LastActivityDate: 2014-09-16T21:47:37.927Z
Answers.LastEditDate: 2014-09-16T21:47:37.927Z
Answers.UserDisplayName: Jamie Pittock
Body: <p>I need to set the page title like this:</p>
  
  <pre><code>{% if entry.artistFirstName | length %}
      {{ entry.artistFirstName | capitalize }}
  {% endif %}
  
  {{ entry.title | capitalize }}
  </code></pre>
  
  <p>So if a first name is set I need to display it, but if not I only need to display the entry's title.</p>
  
  <p>Here is one of the solutions I've tried but unfortunately does not work:</p>
  
  <pre><code>{% set title = {% if entry.artistFirstName | length %}
      {{ entry.artistFirstName | capitalize }}
  {% endif %}
  
  {{ entry.title | capitalize }} %}
  </code></pre>
  
  <p>It does not seem possible to define something in something that is being defined. Any idea how to get around this?</p>
  
Tags: <conditional><titles><concatenation>
Title: How to add a conditional elements into the title of a page?
ThreadText: # How to add a conditional elements into the title of a page?
  
  (Asked by: username_0 on 2014-09-16)
  
  I need to set the page title like this:
  
  ```
  {% if entry.artistFirstName | length %}
      {{ entry.artistFirstName | capitalize }}
  {% endif %}
  
  {{ entry.title | capitalize }}
  
  ```
  
  So if a first name is set I need to display it, but if not I only need to display the entry's title.
  
  Here is one of the solutions I've tried but unfortunately does not work:
  
  ```
  {% set title = {% if entry.artistFirstName | length %}
      {{ entry.artistFirstName | capitalize }}
  {% endif %}
  
  {{ entry.title | capitalize }} %}
  
  ```
  
  It does not seem possible to define something in something that is being defined. Any idea how to get around this?
  
  **Tags:** conditional, titles, concatenation
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2014-09-16. Score: 3 | Accepted answer
  
  I suggested an edit to Jamies suggestion (hope that's okay - don't know all the rules yet).
  Here's the suggestion
  
  ```
  {% set title = (entry.artistFirstName) ? [entry.artistFirstName, ' ', entry.title]|join : entry.title %}
  {{ title | capitalize }}
  
  ```
  
  Here i'm joining the two variables if entry.artistFirstName exists, by first putting all the parts into an array, and then using the join filter to glue them together. I like this approach, because it's easy to add many parts without having to put ~ ' ' ~ for every space you need.
  
  In general, if you want to glue strings like this together, you use the ~ operator, like this:
  
  ```
  {{ 'string1' ~ 'string2' ~ 'string3' }} -> {{ string1string2string3 }}
  
  ```
  
  An alternative approach to the one I suggest above is therefore like so:
  
  ```
  {% set title = (entry.artistFirstName) ? entry.artistFirstName ~ ' ' ~ entry.title : entry.title %}
  
  ```
  
  This is just as fine, but the first approach might be nicer if you have more parts.
  
  A third way is this, which might be just as nice/clean, but maybe slightly less readable?
  
  ```
  {% set title = (entry.artistFirstName) ? '%s - %s'|format(entry.artistFirstName, entry.title) : entry.title %}
  
  ```
  
  
  ---
  
  ### Answer by username_2 on 2014-09-16. Score: 2
  
  How about this?
  
  We assign the result of a conditional to a variable called `title`. The conditional checks if `entry.artistFirstName` is not empty. If true we assign it to the `title` variable, if false we fallback to assigning `entry.title` instead.
  
  ```
  {% set title = (entry.artistFirstName) ? entry.artistFirstName ~ ' ' ~ entry.title : entry.title %}
  
  {{ title | capitalize }}
  
  ```
  
  > **Comments:**
  > **username_0**: Thanks! But it seems to not work for me. If _entry.artistFirstName_ is defined it only outputs _entry.artistFirstName_ instead of _entry.artistFirstName + entry.title_.  
  > **username_2**: Ok I edited my original reply. Hopefully you can work out the exact formatting you want from that.  
  > **username_0**: Thank you very much, now it works! But I validate the answer from username_1 username_1 which is more complete, and which could be useful to others in a much larger number of cases.  
  
  ---
  
  