Take the 2-minute tour ×
Craft CMS Stack Exchange is a question and answer site for administrators, end users, developers and designers for Craft CMS. It's 100% free, no registration required.

Inside the default template that comes installed with Craft, the title of a page is set as:

<title>{% if title is defined %}{{ title }} - {% endif %}{{ siteName }}</title>

This works perfectly for the homepage and on pages where the {{ title }} variable is set. However for individual entries inside a channel, only the site name is used for the title.

I've tried to use the {{ entry.title }} variable instead, however in that case the title of the homepage is rendered as 'Homepage - {{ siteName }}'.

What's the best way to achieve the following page titles for the different sections?

Homepage: {{ siteName }}
Page: {{ title }} - {{ siteName }}
Entry: {{ entry.title }} - {{ siteName }}
share|improve this question

1 Answer 1

up vote 4 down vote accepted

The title var can be set in the child templates, that way you can fine-tune it for each section of your site.

Take a look at these default templates:

news/index.html:

{% set title = "News" %}

news/_entry.html:

{% set title = entry.title %}

_layout.html:

The main layout just checks if it has been defined or not:

{% if title is defined %}{{ title }} - {% endif %}{{ siteName }}

.

P.S. Embed templates might be confusing at first, but are really great, check out the docs.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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