2

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 %}
{% endif %} {% endif %} {% endfor %}
  • CONTACT
  • {% for tempEntry in craft.entries.section('siteGeneral').limit(1) %} Menu

    {% set logoimage = tempEntry.siteLogo.first() %} {% if logoimage %} {% endif %} Search

    Email us Call us: {{tempEntry.phoneNumber}}
    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 %}
    {% endif %} {% endif %} {% endfor %}
  • CONTACT
  • {% endfor %} {% block content %} {% endblock %} CONNECT WITH US --> {% for tempNav in craft.entries.section('contentPages').order('displayOrder') %} {% if tempNav.footerLink == "yes" %}
  • {{ tempNav.title }}
  • {% endif %} {% endfor %} {% for tempEntry in craft.entries.section('siteGeneral').limit(1) %}

    {{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 %}
    
    12
    • what is your entries variable? you usually should do {% set title = entry.title %} Oct 24, 2017 at 14:43
    • Its content, hence the {% set title = content.title %} line there. I can see content.title works as it has data in the inner template, its just its not getting past back out to the outer template. It seems to just be dumping everything out in a single pass as even is I manually set a value in the _layout template, it doesn't work unless I use it before I set it in the template. Most systems do multiple passes to get round this even if it does make a certain amount of sense.
      – Drenzul
      Oct 24, 2017 at 14:51
    • Do you extend your layout template in the template where you set the page title correct? Oct 24, 2017 at 15:09
    • The content template does have this at the top: {% extends "_layout" %}
      – Drenzul
      Oct 24, 2017 at 15:53
    • 1
      I think it would be much easier if you just show us your entire templates.. maybe you overwrite your title somewhere. Because when you loop through your content it's relatively easy to set title to null Oct 25, 2017 at 15:33

    1 Answer 1

    1

    This should do the trick

    {% extends "_layout" %}
    
    {# do you use a custom routing or why do you repull the entry #}
    {# you know craft presents you the entry if you are in a entrypage #}
    {# so just using title = entry.title will be enough because the global entry variable already exists #}
    {% set entry = craft.entries({ slug: craft.request.getSegment(2) }).section(content).first() %}
    {% set title = entry.title %}
    {% block content %}
    
            Title: {{ title }}
            <div class="slidewrap">
                <div id="slider">
                    <div class="slides slide" style="">
                        <div class="slideimgwrap">
                            {% for slide in entry.mainImage %}
                                <img src="{{ slide.url }}" alt="{{ entry.title }}" class="mySlides">
                            {% endfor %}
                        </div>
                        <div class="slideoverlay">
                            <div class="plainblock">
                                <div class="slideoverlayinner">
                                    <h2 class="slidetext">{{ entry.textMainImageOverlay }}</h2>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <h1 class="pagetitle">{{ entry.innerPageTitle }}
                {% if entry.innerPageTitleAdditional is not empty %}
                    <span class="hidemobile"> - </span><br class="mobileonly">
                    <span class="smallmobile">{{ entry.innerPageTitleAdditional }}</span>
                {% endif %}
            </h1>
    
    
            <div class="innerblock">
                <div class="plainblock">
                    <div class="mainboxlefttext">
                        {{ entry.innerboxText }}
                    </div>
                    <div class="mainboxrightimage hidemobile">
                        {% for image in entry.innerboxImage %}
                            <img src="{{ image.url }}"/>
                        {% endfor %}
    
                    </div>
                </div>
            </div>
    
            <div class="plainblock">
                {% for block in entry.mainContentArea %}
                    <div class="imageblock {% if block.sectionImageLocation == "left" %} left{% endif %}{% if block.sectionImageLocation == "right" %}right{% endif %}{% if block.sectionImageLocation == "noImage" %}noimage {% endif %}">
                        <div class="image">
                            {% for image in block.sectionImage %}
                                <img src="{{ image.url }}"/>
    
                            {% endfor %}
                        </div>
                        <div class="text">
                            <h2 class="title">{{ block.sectionTitle }}</h2>
                            {{ block.sectionText }}
    
                        </div>
                    </div>
                {% endfor %}
                {% set myClasses = ['left', 'center', 'right'] %}
                {% for block in entry.pictureSection.type('heading') %}
                    {% if block.type == "heading" %}
                        <div class="imageblock noImage noBottom">
                            <div class="text">
                                <h2 class="title">{{ block.header }}</h2>
                                {{ block.textSection }}
                            </div>
                        </div>
                    {% endif %}
                {% endfor %}
                {% for block in entry.pictureSection.type('images') %}
                    {% if block.type == "images" %}
                        <div class="{{ cycle(myClasses, loop.index0) }} imagebox">
                            {% for image in block.image %}
                                <img src="{{ image.url }}"/>
                            {% endfor %}
                            {{ block.label }}
                        </div>
                    {% endif %}
    
                {% endfor %}
    
            </div>
            <div class="plainblock footimage">
                <h1 class="pagetitle subtitle" style="height: 27px;"><img src="/images/divide_logo.jpg"
                                                                          style="width: auto; background-color: #fff; height: auto; padding: 0 10px;">
                </h1>
            </div>
    {% endblock %}
    
    3
    • 1
      {# do you use a custom routing or why do you repull the entry #} First craftcms project and I'm paranoid about it doing what I want it to ;) I see now, by adding the {% set title = entry.title %} before the {% block content %} tag, it allows it to be seen by the layout template as well. That was pretty much the only bit I was missing. Much appreciated!
      – Drenzul
      Oct 26, 2017 at 9:15
    • Yes and please don't use that {% set entry = craft.entries.... %} you always have the entry variable if you are in an entry and the category variable if you are in a category. No need to fetch them. As well as these craftcms.com/docs/templating/global-variables Oct 26, 2017 at 9:24
    • gotcha, too used to using Expression Engine :)
      – Drenzul
      Oct 26, 2017 at 11:59

    Your Answer

    By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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