5

I'm trying to generate titles for a section's entries based off of the given post date (if it exists). Here's the configuration I've got in the Title Format field for this section's entry types:

New Arrivals for {% if postDate %}{{ postDate|date('M j, Y') }}{% else %}{{ now|date('M j, Y') }}{% endif %}

I expect that, upon saving each new entry, Craft would build a title based off of the post date. Unfortunately, it always builds off of the current date - even with a more straightforward title format configuration:

New Arrivals for {{ postDate|date('M j, Y') }}

I understand that the user doesn't have to fill out the post date fields, but it appears that Craft creates a post date for every entry (and even does so before it writes the date created field to the database).

With that in mind, why wouldn't the above title format configurations work? Am I missing something? Any and all help is appreciated.

4

The entry will be passed to this template as a variable named object. When you want to reference its properties you have two options:

  • {postDate} (single braces)
  • {{object.postDate}} (regular Twig syntax)

The first one is just a shortcut for the second one. When Craft is getting ready to pass your Title Format template off to Twig, the first thing it will do is find any of those shortcut tags and swap ten with the full Twig syntax.

So in your case here's what you should do:

New Arrivals for {% if object.postDate %}{postDate|date('M j, Y')}{% else %}{{ now|date('M j, Y') }}{% endif %}
  • Thanks, @Brandon. I read through the docs but got confused about how the single bracket syntax worked with the normal (double bracket) Twig syntax. Thanks again for clearing that up! – Ray Sep 23 '14 at 14:33
  • 2
    @Bitmanic Thanks for the feedback! Just updated the docs - hopefully this will make more sense for the next poor soul. buildwithcraft.com/docs/… – Brandon Kelly Sep 23 '14 at 15:17
  • Holy smokes, that edit makes it clear as day. Thanks so much, and keep up the stellar work. – Ray Sep 23 '14 at 16:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.