# Set additional text along with {{ .Title }} in {{ .Scratch }}

**URL:** <https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916>\
**Category:** support\
**Created:** [August 30, 2020, 5:08pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916 "2020-08-30T17:08:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [August 30, 2020, 5:08pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916/1 "2020-08-30T17:08:15Z")

</div>

So, I’m trying to set page title based on a condition. Since I need to include the same title in properties like `og:title`, etc. I was thinking, maybe, I could store the title in a variable (`{{ .Scratch}}`).

Here’s what I am doing:

```
{{ if .IsHome }}

  {{ .Scratch.Set "title" .Site.Params.Title }}

  {{ else if in (.Scratch.Get "currentPage") "tags" }}

  {{ .Scratch.Set "title" "Projects tagged {{ .Title }} | {{ .Site.Params.Title }}" }}

    {{ else }}

    {{ .Scratch.Set "title" "{{ .Title }} | {{ .Site.Params.Title }}" }}

{{ end }}

<title>{{ .Scratch.Get "title" }}</title>

```

It’s working correctly except for the fact that in the both the `else` conditions, it’s setting title as `Projects tagged {{ .Title }} | {{ .Site.Params.Title }}` as-is. The `{{ .Title }}` and `{{ .Site.Params.Title }}` are not getting substituted with the expected values.

I’ve to do something similar for description, so, it’ll be helpful to set it once and access it multiple times.

P.S.: `currentPage` has been setup like this: `{{ .Scratch.Set "currentPage" .Permalink}}`

---

<div class="post-metadata">

**Author:** ![sephore](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/sephore/32/11673_2.png) [@sephore](https://discourse.gohugo.io/u/sephore)\
**Post date:** [August 30, 2020, 5:30pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916/2 "2020-08-30T17:30:18Z")

</div>

Try to use something like [printf](https://gohugo.io/functions/printf/). Example:

```auto
printf "Projects tagged %s | %s" .Title .Site.Params.Title

```

---

<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:** [August 30, 2020, 5:31pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916/3 "2020-08-30T17:31:26Z")

</div>

Also, do you remember this from yesterday?

> [@Access variable from baseof in partial](https://discourse.gohugo.io/t/access-variable-from-baseof-in-partial/27879/15):
>
> Never nest brackets. No: {{ if not (in {{ .Scratch.Get "currentPage"}} $name) }} wrong number of args for in: want 2 got 3 This tells you exactly what you did wrong. Don’t give up because of an error. Learn from it.

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [August 30, 2020, 5:40pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916/4 "2020-08-30T17:40:17Z")

</div>

Yeah I do, that’s why I wanted to know the alternative route to do this. I just thought that showing what I tried (basically, what I could think of trying) would be better than asking people to help without me trying anything.

@sephore

So, I replaced

`{{ .Scratch.Set "title" "Projects tagged {{ .Title }} | {{ .Site.Params.Title }}" }}`

with

`{{ .Scratch.Set "title" (printf "Projects tagged %s | %s" .Title .Site.Params.Title) }}`

and it works great, however the line

`{{ .Scratch.Set "title" (printf .Title " %s | %s" .Site.Params.Title) }}`

when used as

`{{ .Scratch.Set "title" (printf .Title " | " .Site.Params.Title) }}`,

produces the title as `ProjectName%!(EXTRA string= | , string=SiteTitle)`

Something to note, ProjectName and SiteTitle come out fine, the problem is the `%!(EXTRA string= | , string=` that are popping up.

EDIT: Formatted in a more readable way.

---

<div class="post-metadata">

**Author:** ![sephore](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/sephore/32/11673_2.png) [@sephore](https://discourse.gohugo.io/u/sephore)\
**Post date:** [August 30, 2020, 5:42pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916/5 "2020-08-30T17:42:14Z")

</div>

Please, read the code and error message carefully. 😐

---

<div class="post-metadata">

**Author:** ![Hrishikesh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/hrishikesh/32/19176_2.png) [@Hrishikesh](https://discourse.gohugo.io/u/Hrishikesh)\
**Post date:** [August 30, 2020, 5:50pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916/6 "2020-08-30T17:50:28Z")

</div>

Oh, so, the trick was to set is like this:

`{{ .Scratch.Set "title" (printf "%s | %s" .Title .Site.Params.Title) }}` Basically, all variables after the text in `"..."`. So, I’m guessing `%s` is what defines where the strings will be placed.

Alright, got it. Thanks guys.

---

<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:** [September 1, 2020, 5:50pm UTC](https://discourse.gohugo.io/t/set-additional-text-along-with-title-in-scratch/27916/7 "2020-09-01T17:50:32Z")

</div>

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