2

I'd like to understand how to combine these two elements into a twig set vars.

--

Context: I'm building a dynamic template that will list entries from which ever section the URL segment loads. For example /news will be news articles.

Each template will have a page title variable that I'm sending up to the _layout master. The page title's are input using craft.global's I have them setup like this {{ news.pageTitle }}.

In my dynamic template I'm trying to load everything from the URL slug. For example if it loaded the /notices URL the template would render the global: {{ notices.pageTitle }}.

--

Craft global: {{ news.pageTitle }}

URL Segment (/news): {% set getSegment = craft.request.getSegment(1) %}

This works: {% set pageTitle = news.pageTitle %}

But this doesn't: {% set pageTitle = getSegment.pageTitle %}

2
  • Not quite sure I'm following what you're trying to do, can you elaborate a bit more? The variable getSegment is going to be a string so it won't have a property called pageTitle on it.
    – Brad Bell
    Aug 13, 2015 at 22:45
  • Sure thing, have updated my question.
    – R0wan
    Aug 13, 2015 at 23:11

1 Answer 1

3

Are you trying to look up the global variable {{ news.pageTitle }} by having the news part be dynamically set from the first segment?

This answer here might help explain. Then try this...

{% set pageTitle = _context[craft.request.getSegment(1)].pageTitle|default("News") %}

I don't think that's the best way to set the displayed page title for your sections, but I believe that's what you're trying to do.

A slightly nicer way would be to also create a page for each section/channel root and have its URL be the same as the base path you use for the entries in those sections. Then you could dynamically look up the page attached to any entry by using this (sorry it's complicated)...

{% set page = craft.enties.url(entry.section.urlFormat|split("{")[0]) %}

That should find the page with the URL that matches the URL of the section entries (up until the first dynamic part). Then you can use the page title, banner, or any other fields you've put on the page.

5
  • Thanks rick, that works. Any better way to set the page title for a listing page that isn't a page inside Craft? I'm using a route to call this template that then lists section based content...
    – R0wan
    Aug 13, 2015 at 23:11
  • Thanks for the edit Rick. Looks like a nice solution. I agree having these as pages inside Craft is definitely a better way to go. Perhaps I'll make the listing pages "singles" and then make them load the listing template.
    – R0wan
    Aug 13, 2015 at 23:18
  • Some other CMSs actually use pages as the base point for channel type data, which is great because it automatically gives you an index page. But Craft does it a little different.
    – Rick
    Aug 13, 2015 at 23:26
  • That would be a great idea if even a "checkbox" saying this entry is a "listing page" would be cool. My question stems from this larger issue (craftcms.stackexchange.com/questions/1302/…)
    – R0wan
    Aug 14, 2015 at 1:14
  • Feature request here: feedback.buildwithcraft.com/forums/285221-feature-requests/…
    – R0wan
    Aug 14, 2015 at 1:28

Your Answer

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

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