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

<p><a href="https://craftcms.stackexchange.com/questions/703/calling-variables-dynamically">This answer here</a> might help explain. Then try this...</p>

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

<p>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.</p>

<p>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)...</p>

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

<p>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.</p>

CC BY-SA 3.0
2015-08-13T23:11:18.113Z
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
CC BY-SA 3.0
2015-08-13T23:18:40.990Z
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
CC BY-SA 3.0
2015-08-13T23:26:28.187Z
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
CC BY-SA 3.0
2015-08-14T01:14:27.580Z
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 (http://craftcms.stackexchange.com/questions/1302/how-to-create-a-cms-editable-channel-section-index-page)
R0wan
CC BY-SA 3.0
2015-08-14T01:28:30.350Z
Feature request here: http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7236456-landing-pages-for-channels
R0wan
CC BY-SA 3.0
2015-08-13T23:06:16.260Z
2015-08-13T23:11:31.327Z
2017-04-13T12:51:00.303Z
Rick
<p>I'd like to understand how to combine these two elements into a twig set vars.</p>

<p>--</p>

<p>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.</p>

<p>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 }}.</p>

<p>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 }}. </p>

<p>--</p>

<p><strong>Craft global:</strong> <code>{{ news.pageTitle }}</code></p>

<p><strong>URL Segment (/news):</strong> <code>{% set getSegment = craft.request.getSegment(1) %}</code></p>

<p>This works: <code>{% set pageTitle = news.pageTitle %}</code></p>

<p>But this doesn't: <code>{% set pageTitle = getSegment.pageTitle %}</code></p>

CC BY-SA 3.0
2015-08-13T22:45:47.277Z
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
CC BY-SA 3.0
2015-08-13T23:11:45.307Z
Sure thing, have updated my question.
R0wan
<templating><variable>
Trying to set a dynamic twig variable using craft.request and a craft global
# Trying to set a dynamic twig variable using craft.request and a craft global

(Asked by: username_0 on 2015-08-13)

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 %}`

**Tags:** templating, variable
> **Comments:**
> **username_1**: 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.  
> **username_0**: Sure thing, have updated my question.  

---

## 1 answers

### Answer by username_2 on 2015-08-13. Score: 3 | Accepted answer

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](https://craftcms.stackexchange.com/questions/703/calling-variables-dynamically) 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.

> **Comments:**
> **username_0**: 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...  
> **username_0**: 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.  
> **username_2**: 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.  
> **username_0**: 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 (http://craftcms.stackexchange.com/questions/1302/how-to-create-a-cms-editable-channel-section-index-page)  
> **username_0**: Feature request here: http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7236456-landing-pages-for-channels  

---

