<p>If you are on an entry page have you tried:</p>

<p>{{ entry.eventBooking.title }}</p>

<p>Because at the moment it doesn't look like your telling craft to look at the eventBooking handle on the entry but the title on a eventBooking variable</p>

CC BY-SA 3.0
2014-08-07T16:02:25.823Z
I understand what you're saying and it makes sense, but no I still just get "-"
darylknight
CC BY-SA 3.0
2014-08-07T16:14:52.057Z
You may need to specify the .first() related entry. {{entry.eventBooking.first().title}}
Tyler Willingham
CC BY-SA 3.0
2014-08-07T15:55:11.917Z
2014-08-07T15:55:11.917Z
Alec Ritson
<p>I believe this is in regards to the <code>Title Format</code> feature, so just to be explicit in what I'm referring to, Craft Documentation calls this a <a href="http://buildwithcraft.com/docs/sections-and-entries#dynamic-entry-titles" rel="nofollow noreferrer">Dynamic Entry Title</a>.</p>
<p>The syntax you are looking for can take two forms:</p>
<p><strong>1) Long form object syntax</strong></p>
<p>Just as on the front end your <code>entry</code> is available via the <code>{{ entry }}</code> variable, within your Title pattern on the back end, the current entry is available via the <code>{{ object }}</code> variable.</p>
<p>So I believe the following should work, assuming you are using the actual field names:</p>
<pre><code>{{ object.eventBooking.one().title }} - {{ object.userBooking.one().username }}
</code></pre>
<p>You need to use the <code>one()</code> method here because Relations fields return an array by default. Since you need to output your content directly, you need to make sure you are accessing the <code>title</code> and <code>username</code> properties via the Model they are associated with in that array.  If your fields are required, you can safely assume that the first item will exist in the array.</p>
<p><strong>2) Short hand syntax</strong></p>
<p>If what you are doing is simple, Craft gives you the option to make the syntax a bit easier and omit the <code>object</code> variable.  So the above could also be written with single curly braces as such:</p>
<pre><code>{eventBooking.one().title} - {userBooking.one().username}
</code></pre>

CC BY-SA 3.0
2014-08-08T08:45:49.623Z
That shorthand syntax works perfectly, thank you!
darylknight
CC BY-SA 4.0
2023-12-15T07:05:53.523Z
This no longer seems to be working, as of Craft 4. Instead, `{fieldName.one().title}` is working for me.
tremby
CC BY-SA 4.0
2023-12-15T18:35:38.173Z
Good note. That changed a while ago. I updated the answer to reflect the .one() syntax.
Ben Parizek
CC BY-SA 4.0
2014-08-07T16:33:41.587Z
2023-12-15T18:35:09.077Z
2023-12-15T18:35:09.077Z
Ben Parizek
<p>If a booking channel contains two Entries fields (one related to a single User, one related to a single 'Event'), how can I get the booking entry title to be set automatically to the following syntax:</p>

<p>"Event Date - Username"</p>

<p>I've set up the entry to not show the title field, but my syntax is wrong. I'm using:</p>

<pre><code>{{eventBooking.title}} - {{userBooking.username}}
</code></pre>

<p>but all I'm getting is "-", so it's obviously not recognising the fields. Is this possible?</p>

CC BY-SA 3.0
2014-08-07T16:08:34.893Z
Are you actually trying to set an entry's title or are you trying to display a title on a page as a combination of those values?
Tyler Willingham
CC BY-SA 3.0
2014-08-08T08:47:09.753Z
I was trying to set the entry's title based on those values when the entry gets created. This has been answered by Ben now below.
darylknight
<entries><titles>
How can I set an entry title automatically, based on a related entry?
# How can I set an entry title automatically, based on a related entry?

(Asked by: username_0 on 2014-08-07)

If a booking channel contains two Entries fields (one related to a single User, one related to a single 'Event'), how can I get the booking entry title to be set automatically to the following syntax:
"Event Date - Username"
I've set up the entry to not show the title field, but my syntax is wrong. I'm using:
{{eventBooking.title}} - {{userBooking.username}}

but all I'm getting is "-", so it's obviously not recognising the fields. Is this possible?

**Tags:** entries, titles

> **Comments:**
> **username_1**: Are you actually trying to set an entry's title or are you trying to display a title on a page as a combination of those values?  
> **username_0**: I was trying to set the entry's title based on those values when the entry gets created. This has been answered by Ben now below.  

---

## 2 answers

### Answer by username_3 on 2014-08-07. Score: 6 | Accepted answer

I believe this is in regards to the Title Format feature, so just to be explicit in what I'm referring to, Craft Documentation calls this a Dynamic Entry Title.
The syntax you are looking for can take two forms:
1) Long form object syntax
Just as on the front end your entry is available via the {{ entry }} variable, within your Title pattern on the back end, the current entry is available via the {{ object }} variable.
So I believe the following should work, assuming you are using the actual field names:
{{ object.eventBooking.one().title }} - {{ object.userBooking.one().username }}

You need to use the one() method here because Relations fields return an array by default. Since you need to output your content directly, you need to make sure you are accessing the title and username properties via the Model they are associated with in that array.  If your fields are required, you can safely assume that the first item will exist in the array.
2) Short hand syntax
If what you are doing is simple, Craft gives you the option to make the syntax a bit easier and omit the object variable.  So the above could also be written with single curly braces as such:
{eventBooking.one().title} - {userBooking.one().username}

> **Comments:**
> **username_0**: That shorthand syntax works perfectly, thank you!  
> **username_4**: This no longer seems to be working, as of Craft 4. Instead, `{fieldName.one().title}` is working for me.  
> **username_3**: Good note. That changed a while ago. I updated the answer to reflect the .one() syntax.  

---

### Answer by username_2 on 2014-08-07. Score: 0

If you are on an entry page have you tried:

{{ entry.eventBooking.title }}

Because at the moment it doesn't look like your telling craft to look at the eventBooking handle on the entry but the title on a eventBooking variable

> **Comments:**
> **username_0**: I understand what you're saying and it makes sense, but no I still just get "-"  
> **username_1**: You may need to specify the .first() related entry. {{entry.eventBooking.first().title}}  

---

