So, I'm using the standard template system that comes with craft, which has this code in it:
<main {% if craft.request.getSegment(1) != "" %}id="content"{% endif %}{% if craft.request.getSegment(1) == "" %}id="home"{% endif %} role="main">
{% block content %}
<p>If you see me, you haven’t set your <code>{% verbatim %}{% block content %}…{% endblock %}{% endverbatim %}</code> yet.</p>
<p>See Twig’s <a href="http://twig.sensiolabs.org/doc/templates.html#template-inheritance">Template Inheritance</a> documentation for an explanation.</p>
{% endblock %}
</main>
Clearly I've added a bit extra in there to set an ID on it.
Now as the page content is defined inside Block content, so it only knows what title to use inside block content, how to I get the Title out from the inner template back to the layout template to display here:
<title>{% if title is defined %}{{ title }} - {% endif %}{{ siteName }}</title>
Currently it just always displays the sitename which is no good. It should be getting the title from the title of whichever page is been pulled in as content which the content section works out from the CraftCMS routes/section information.
Since this is the default layout provided in Craft, there must be a simple way to do this!
I've tried making sure the title variable is defined inside the content template but I can see that any variable I set inside the content template is never accessible in the _layout template.
Heres the extra bits of relevant code:
Layout:
<title>{% if title is defined %}{{ title }} - {% endif %}{{ siteName }}</title>
Content:
{% for content in entries %}
{% set title = content.title %}
Thanks in advance!
Layout:
{# # Layout template # --------------- # # This template defines your site’s global layout. It can be # “extended” by other templates to fill in the gaps. # # See Twig’s Template Inheritance documentation for more details: # http://twig.sensiolabs.org/doc/templates.html#template-inheritance #} {% set title3 = "test" %} {% block _layout %} {% if title is defined %}{{ title }} - {% endif %}{{ siteName }} {% for templatehead in craft.entries.section('siteGeneral').limit(1) %} body{ background-color: {{templatehead.backgroundColour}} ; {% set backgroundtemplate = templatehead.backgroundFile.first() %} {% if backgroundtemplate %} background-image: url({{backgroundtemplate.getURL()}}); {% endif %} } body p{ color: {{templatehead.bodyTextColour}}; } #container, #header, #content, #footer, #home{ background-color: {{templatehead.mainBackgroundColour}} ; } h1, h2, h4, h5{ color:{{templatehead.mainTitleColour}}; } h1.subtitle{ border-bottom: 1px solid {{templatehead.borderLineColour}}; } body #container .innerblock p, body #container .innerblock h1, body #container .innerblock h2, body #container .innerblock h3, body #container .innerblock h4, body #container .innerblock h5{ color: {{templatehead.innerBlockTextColour}}; } a, .fakelink{ color: {{templatehead.innerBackgroundColour}}; } .innerblock a{ color: {{templatehead.innerBlockLinkColour}}; } .phone a, .phone:before, .email:before, .hrbottom ul li a:after{ color: {{templatehead.mainTitleColour}}; } ul li ul.subnavmenu .subnavitemwrap{ border-color: {{templatehead.mainTitleColour}}; } ul li ul.subnavmenu li.subnavitem a::after{ background-color: {{templatehead.mainTitleColour}}; } .phone, .email{ color: {{templatehead.bodyTextColour}}; } .innerblock, .mobilemenuwrap{ background-color: {{templatehead.innerBackgroundColour}}; {% set boxbackground = templatehead.innerBackgroundImage.first() %} {% if boxbackground %} background-image: url({{boxbackground.getURL()}}); {% endif %} background: {{templatehead.innerBackgroundColour}} url({{boxbackground.getURL()}}) no-repeat center top; color: {{templatehead.innerBlockTextColour}}; } .innerblock p, .innerblock h1, .innerblock h2, .innerblock h3, .innerblock h4, .innerblock h5{ color: {{templatehead.innerBlockTextColour}}; } .slidewrap{ border-top: 1px solid {{templatehead.borderLineColour}}; } #footer{ background-color: {{templatehead.innerBackgroundColour}}; } .leftfooter a{ color: {{templatehead.innerBackgroundColour}}; } {% endfor %} Title: {{title}}
Title3: {{title3}} Menu
- Home {% for tempNav in craft.entries.section('contentPages').order('displayOrder') %} {% if tempNav.isMainPage == "yes" %} {% set parents = tempNav.childPages %} {{tempNav.title}} {% if parents | length %} {% for subNav in parents %} {{subNav.title}} {% endfor %}
Email us: {{tempEntry.siteEMail}} Call us: {{tempEntry.phoneNumber}}
-
{% for tempNav in craft.entries.section('contentPages').order('displayOrder') %}
{% if tempNav.isMainPage == "yes" %}
{% set parents = tempNav.childPages %}
{{tempNav.title}}
{% if parents | length %}
{% for subNav in parents %}
{{subNav.title}}
{% endfor %}
{{tempEntry.footerAddressLine}}
{{tempEntry.footerLegalLine}}
{% endfor %} title: {{title}} {% endblock %}Content:
{# # Content Page Template # --------------- # #} {% extends "_layout" %} {% block content %} {% set entries = craft.entries({ slug: craft.request.getSegment(2) }).section(content) %} {% for content in entries %} {% set title = content.title %} Title: {{title}} {% for slide in content.mainImage %} {% endfor %} {{ content.textMainImageOverlay }} {{content.innerPageTitle}} {% if content.innerPageTitleAdditional is not empty %} - {{content.innerPageTitleAdditional}} {% endif %} {{content.innerboxText}} {% for image in content.innerboxImage %} {% endfor %} {% for block in content.mainContentArea %} {% for image in block.sectionImage %} {% endfor %} {{block.sectionTitle}} {{block.sectionText}} {% endfor %} {% set myClasses = ['left', 'center', 'right'] %} {% for block in content.pictureSection.type('heading') %} {% if block.type == "heading" %} {{block.header}} {{block.textSection}} {% endif %} {% endfor %} {% for block in content.pictureSection.type('images') %} {% if block.type == "images" %} {% for image in block.image %} {% endfor %} {{block.label}} {% endif %} {% endfor %} {% endfor %} {% endblock %}
entries
variable? you usually should do{% set title = entry.title %}