# Set page title programatically

**URL:** <https://discourse.gohugo.io/t/set-page-title-programatically/32740>\
**Category:** support\
**Created:** [May 6, 2021, 9:58pm UTC](https://discourse.gohugo.io/t/set-page-title-programatically/32740 "2021-05-06T21:58:51Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paulera](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/paulera/32/8672_2.png) [@paulera](https://discourse.gohugo.io/u/paulera)\
**Post date:** [May 6, 2021, 9:58pm UTC](https://discourse.gohugo.io/t/set-page-title-programatically/32740/1 "2021-05-06T21:58:51Z")

</div>

The content of some pages in my site is generated by reading markdown files stored in [gist.github.com](http://gist.github.com) when the site builds. I want to do the same with the page title.

Working page (hardcoded title):

```auto
---
date: 2021-03-12
title: WFH bingo
---

<!-- render gist as article -->
{{< gist-article "9665dc266ce6b76c2e6dc177b790f1bb" >}}

```

**Attempt 1** :

Using a shortcode in the front matter:

```auto
---
date: 2021-03-12
title: {{< gist-title "9665dc266ce6b76c2e6dc177b790f1bb" >}}

...

```

I got an error `failed to unmarshal YAML: yaml: invalid map key: map[interface {}]interface {}{"< gist-title \"9665dc266ce6b76c2e6dc177b790f1bb\" >":interface {}(nil)}`

**Attempt 2:**

Trying to change the value of `.Page.Title`:

```auto
{{ .Page.Title := "new title" }}

```

It raises an error `unexpected ":=" in operand`.

Thanks!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [May 6, 2021, 11:07pm UTC](https://discourse.gohugo.io/t/set-page-title-programatically/32740/2 "2021-05-06T23:07:33Z")

</div>

Attempt 1 failed because you cannot use template code or shortcodes in front matter. The only exception to this is within archetypes, where you can use template code but not shortcodes.

Attempt 2 failed because page properties are immutable.

If you really want to do this…

1) Set a page-scoped [Scratch](https://gohugo.io/functions/scratch) within your shortcode:

```auto
{{ .Page.Scratch.Set "page_title" "foo" }}

```

2) Retrieve the Scratch value when needed, making sure that you first render the `.Content` so that the shortcode is rendered before you try to access the Scratch.

layouts/\_default/baseof.html

```auto
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, minimum-scale=1.0, initial-scale=1.0">
  <title>
    {{ $noop := .Content }}
    {{ with .Scratch.Get "page_title" }}
      {{ . }}
    {{ else }}
      {{ .Title }}
    {{ end }}
  </title>
</head>

```

layouts/\_default/single.html

```auto
{{ $noop := .Content }}
{{ with .Scratch.Get "page_title" }}
  <h1>{{ . }}</h1>
{{ else }}
  <h1>{{ .Title }}</h1>
{{ end }}

```

---

<div class="post-metadata">

**Author:** ![paulera](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/paulera/32/8672_2.png) [@paulera](https://discourse.gohugo.io/u/paulera)\
**Post date:** [May 7, 2021, 11:09am UTC](https://discourse.gohugo.io/t/set-page-title-programatically/32740/3 "2021-05-07T11:09:23Z")

</div>

It worked, thanks @jmooring!

---

<div class="post-metadata">

**Author:** ![system](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/system/32/1_2.png) [@system](https://discourse.gohugo.io/u/system)\
**Post date:** [May 9, 2021, 11:09am UTC](https://discourse.gohugo.io/t/set-page-title-programatically/32740/4 "2021-05-09T11:09:34Z")

</div>

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.
