Skip to content
Grav 2.0 is officially stable. Read the announcement →

Community guidelines

Please keep discussions civil and on-topic. Repeated violations may lead to a temporary ban.

Themes & Styling

Grav/Twig get title of twig expression

first-time

Solved by Ron Wardenier View solution

Started by Dorian Bollinger 8 years ago · 2 replies · 675 views
8 years ago

I want to get the title of a linked page, which is linked by a twig expression.

<a href="{{ header.home.linkOne }}">#TitleHere</a>

I tried to get the title with page.find but this doesn't work at all.
I don't want to create a backend field to type in the title manually.

Please help me to solve this problem. ☺️

Kindly regards,
Dorian

last edited 05/19/18 by Dorian Bollinger
8 years ago Solution

First make sure you have this included in the frontmatter of your page:

YAML
home:
    linkOne: /One

Second, try this in a Twig template:

TWIG
{% set link = page.find(header.home.linkOne).url %}
{% set title = page.find(header.home.linkOne).header.title %}
<a href="{{ link }}">{{ title }}</a>

The value of linkOne needs to be the full path from the root, e.g. /home/sub-page1.

Suggested topics

Topic Participants Replies Views Activity
Themes & Styling · by Pedro M, 2 months ago
4 193 2 months ago
Themes & Styling · by Ian, 2 months ago
3 90 2 months ago
Themes & Styling · by Norbert, 2 years ago
11 449 3 months ago
Themes & Styling · by Lukáš Findeis, 3 months ago
0 44 3 months ago
Themes & Styling · by Sebadamus, 4 months ago
5 123 3 months ago