# Struggling to use .CurrentSection in shortcode

**URL:** https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383
**Category:** support
**Created:** [April 15, 2021, 1:35pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383 "2021-04-15T13:35:27Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![gadams999](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gadams999/32/8820_2.png) [@gadams999](https://discourse.gohugo.io/u/gadams999)
#### Post date: [April 15, 2021, 1:35pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/1 "2021-04-15T13:35:27Z")

</div>

Hi all,

I’m trying to modify the children shortcode from Learn/docdock to filter on sections under a particular level. For example, I have the following:

```
content
└── animals
├── _index.html
├── cat
│ ├── _index.html
│ ├── persian
│ │ └── _index.html
│ └── tabby
│ └── _index.htm
└── dog
    ├── _index.html
    ├── husky
    │ └── _index.html
    └── poodle
        └── _index.html

```

And I want to add a short code to `content/animals/_index.html` to filter and only process “dog” pages for instance. If I reference `.Section` for each page in a range I get `animals` which I expect.

When I try to use `.CurrentSection`, which I interpret as the current section for the page being processed, it returns a page object for that page. For the same page, `.Kind` returns “section”, so I’m stumped as to how to get the current section for “dog”, and then when I process those sub pages get “husky” and “poodle”.

I’m pretty sure there is something fundamental I’m missing, so any insights for how to get the current section would be appreciated!

---

<div class="post-metadata">

### Author: ![pointyfar](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/pointyfar/32/5797_2.png) [@pointyfar](https://discourse.gohugo.io/u/pointyfar)
#### Post date: [April 15, 2021, 2:19pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/2 "2021-04-15T14:19:46Z")

</div>

Hi there,

It would be easier to help you if we were able to replicate your issue. Do you have your code somewhere we can have a look at?

See: [Requesting Help](https://discourse.gohugo.io/t/requesting-help/9132)

---

<div class="post-metadata">

### Author: ![gadams999](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gadams999/32/8820_2.png) [@gadams999](https://discourse.gohugo.io/u/gadams999)
#### Post date: [April 15, 2021, 3:17pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/3 "2021-04-15T15:17:28Z")

</div>

Sure thing. Here’s the abbreviated shortcode I want to drop onto a page and have it filter (not implemented) on the child pages based on their current section:

```
{{% sectiontest depth="2" %}}

```

Here’s the `sectiontest.html` code:

```
{{ $depth := .Get "depth" | default 1 }}
<ul>
	<!-- For testing, not checking for IsHome -->
	{{ .Scratch.Set "pages" (.Page.Pages | union .Page.Sections) }}
	{{ $pages := (.Scratch.Get "pages") }}

	{{template "childs" dict "menu" $pages.ByWeight "count" 1 "depth" $depth "pages" .Site.Pages }}

</ul>

{{ define "childs" }}
	<!-- "menu" contains the pages from location where shortcode is included -->
	{{ range .menu }}
	<!-- I want to filter based on the next child section below. Reference to .CurrentSection returns full page path -->
<p><a href="{{.RelPermalink}}" >{{ .Title }}</a> - Section: {{.CurrentSection}}</p>
<p>{{.Summary}}</p>
	<!-- Continue down until depth -->
	{{ if lt $.count $.depth}}
		{{if eq $.style "li"}}
<ul>
		{{end}}

		{{ if .Sections}}
			{{ .Scratch.Set "pages" (.Pages | union .Sections) }}
		{{else}}
			{{ .Scratch.Set "pages" .Pages }}
		{{end}}

		{{ $pages := (.Scratch.Get "pages") }}

		{{template "childs" dict "menu" $pages.ByWeight "count" (add $.count 1) "depth" $.depth "pages" $.pages }}
		{{if eq $.style "li"}}
</ul>
		{{end}}
	{{end}}
{{end}}

<!-- shortcode end -->
{{end}}
```

---

<div class="post-metadata">

### Author: ![gadams999](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gadams999/32/8820_2.png) [@gadams999](https://discourse.gohugo.io/u/gadams999)
#### Post date: [April 15, 2021, 4:04pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/4 "2021-04-15T16:04:46Z")

</div>

Just to follow up. I can get the string value I want by calculating the string value of `.CurrentSection` with this:

```
{{ $elements := index (split .CurrentSection /) }}
{{ $section := index $elements (sub (len $elements) 2) }}

```

If `.CurrentSection` contains `Page(/animals/dog/_index.md)`, I will get `dog`. In reading the docs, I would think that’s the value `.CurrentSection` should return. Is the string value what is expected for that Section variable.

---

<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: [April 15, 2021, 4:51pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/5 "2021-04-15T16:51:45Z")

</div>

```auto
{{ .CurrentSection.Title }}

```

When troubleshooting, you can use this approach to determine type:

```auto
{{ printf "%T" .CurrentSection }}<br>
{{ printf "%T" .CurrentSection.Title }}<br>

```

---

<div class="post-metadata">

### Author: ![gadams999](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gadams999/32/8820_2.png) [@gadams999](https://discourse.gohugo.io/u/gadams999)
#### Post date: [April 15, 2021, 5:12pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/6 "2021-04-15T17:12:29Z")

</div>

`.Section` returns the folder name off of the `content/` folder, while `.CurrentSection.Title` returns the title, which is the same for `.Title`? It may be I don’t understand some of the terminology of returned section values and there are other aspects of Hugo where it is relevant?

In my case I have a working solution by splitting `.CurrentSection` which returns the folder path.

---

<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: [April 15, 2021, 5:19pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/7 "2021-04-15T17:19:14Z")

</div>

[https://gohugo.io/variables/page/#section-variables-and-methods](https://gohugo.io/variables/page/#section-variables-and-methods)

---

<div class="post-metadata">

### Author: ![gadams999](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gadams999/32/8820_2.png) [@gadams999](https://discourse.gohugo.io/u/gadams999)
#### Post date: [April 15, 2021, 5:28pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/8 "2021-04-15T17:28:50Z")

</div>

I’m starting to grok how the variables and methods are listed. When I read this for `.CurrentSection`:

> The page’s current section. The value can be the page itself if it is a section or the homepage.

then I don’t see the difference between `.Page` and `.CurrentSection`. Unless it’s only when processing the homepage?

---

<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: [April 15, 2021, 5:44pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/9 "2021-04-15T17:44:23Z")

</div>

```auto
content
├── animals
│ ├── cat
│ │ ├── persian
│ │ │ └── index.md (1) (no underscore in file name)
│ │ └── _index.md (2)
│ └── _index.md (3)
└── _index.md (4)

```

1) content/animals/cat/persian/index.md (kind: page)

.Title = Persian  
.CurrentSection.Title = Cats

2) content/animals/cat/\_index.md (kind: section)

.Title = Cats  
.CurrentSection.Title = Cats

3) content/animals/\_index.md (kind: section)

.Title = Animals  
.CurrentSection.Title = Animals

4) content/\_index.md (kind: section)

.Title = My Site  
.CurrentSection.Title = My Site

Note that cat was pluralized to cats when displaying the title. To disable, set `pluralizeListTitles = false` in config.toml.

---

<div class="post-metadata">

### Author: ![gadams999](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/gadams999/32/8820_2.png) [@gadams999](https://discourse.gohugo.io/u/gadams999)
#### Post date: [April 15, 2021, 7:22pm UTC](https://discourse.gohugo.io/t/struggling-to-use-currentsection-in-shortcode/32383/10 "2021-04-15T19:22:55Z")

</div>

Ah, now this makes sense. Value and pluralization set based on page type (leaf/branch), and that derives the section based on title. So I should be matching on actual frontmatter title.
