Display/ Hide Page

Hi,

I added a shortcode to website for display/hide complete page according to the params. Actually it works but what I want is completely hide this page from my website… I mean it should not be seen also a sidebar or anywhere.

I thought maybe ı can set title and linkTitle as blank as below in else part but not sure that its a proper solution or not.ıf it is how can I do that or what is the proper way of doing this ?

title: ""

linkTitle: ""

baseof.html

    {{ if not .Site.Params.ui.breadcrumb_disable }}{{ partial "breadcrumb.html" . }}{{ end }}
    {{- if .Params.audience }}
      {{- if in .Site.Params.audience (.Params.audience) }}
       {{ block "main" . }}{{ end }}
      {{- else }}
        This page is only accessible for {{ .Params.audience }}
      {{- end }}
    {{- else }}
     {{ block "main" . }}{{ end }}
   {{- end }}

frontendmatter


---

title: "API Management"

linkTitle: "API Management"

weight: 1

audience: "dealers"

description: >

Regards,
Semih

Have a look at the Build Options configuration:

But ı would like to do this according to parameter not always or not never…

Here is the sidebar.html by the way

{{ $shouldDelayActive := ge (len .Site.Pages) 2000 }}
<div id="td-sidebar-menu" class="td-sidebar__inner{{ if $shouldDelayActive }} d-none{{ end }}">
  {{ if not .Site.Params.ui.sidebar_search_disable }}
  <form class="td-sidebar__search d-flex align-items-center">
    {{ partial "search-input.html" . }}
    <button class="btn btn-link td-sidebar__toggle d-md-none p-0 ml-3 fas fa-bars" type="button" data-toggle="collapse" data-target="#td-section-nav" aria-controls="td-docs-nav" aria-expanded="false" aria-label="Toggle section navigation">
    </button>
  </form>
  {{ end }}
  <nav class="collapse td-sidebar-nav pt-2 pl-4" id="td-section-nav">
    {{ if  (gt (len .Site.Home.Translations) 0) }}
    <div class="nav-item dropdown d-block d-lg-none">
      {{ partial "navbar-lang-selector.html" . }}
    </div>
    {{ end }}
    {{ template "section-tree-nav-section" (dict "page" . "section" .FirstSection "delayActive" $shouldDelayActive)  }}
  </nav>
</div>
{{ define "section-tree-nav-section" }}
{{ $s := .section }}
{{ $p := .page }}
{{ $shouldDelayActive := .delayActive }}
{{ $active := eq $p.CurrentSection $s }}
{{ $show := or (and (not $p.Site.Params.ui.sidebar_menu_compact) ($p.IsAncestor $s)) ($p.IsDescendant $s) }}
{{ $sid := $s.RelPermalink | anchorize }}
<ul class="td-sidebar-nav__section pr-md-3">
  <li class="td-sidebar-nav__section-title">
    <a  href="{{ $s.RelPermalink }}" class="align-left pl-0 pr-2{{ if not $show }} collapsed{{ end }}{{ if $active}} active{{ end }} td-sidebar-link td-sidebar-link__section">{{ $s.LinkTitle }}</a>
  </li>
  <ul>
    <li class="collapse {{ if $show }}show{{ end }}" id="{{ $sid }}">
      {{ $pages := where (union $s.Pages $s.Sections).ByWeight ".Params.toc_hide" "!=" true }}
      {{ $pages := $pages | first 50 }}
      {{ range $pages }}
      {{ if .IsPage }}
      {{ $mid := printf "m-%s" (.RelPermalink | anchorize) }}
      {{ $active := eq . $p }}
      <a class="td-sidebar-link td-sidebar-link__page {{ if and (not $shouldDelayActive) $active }} active{{ end }}" id="{{ $mid }}" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
      {{ else }}
      {{ template "section-tree-nav-section" (dict "page" $p "section" .) }}
      {{ end }}
      {{ end }}
    </li>
  </ul>
</ul>
{{ end }}

You can also use the Build options as needed or remove them from a content file once you need it re-listed. Anyway Build Options offer the cleanest way to partially hide a page in a Hugo project.

I’m sure others can help you with what you want.

How should I pass the params to build ?

There are no build options CLI flags to pass during site generation if that is what you’re asking.

The Docs offer concise use cases for Build Options:

Yeap that was what I tried to ask… So cant I add a same params to sidebar.html ı tried but it did not work though it is completely remove the sidebar where I would like to hide only the related title and linktitle so then it was hidden completely from the page

You can do the above by using with

Rebinds the context ( . ) within its scope and skips the block if the variable is absent or empty.

e.g.

{{ with .Param.foo }}
<--- sidebar page links --->
{{ end }}

OR

Filter the sidebar links with where

e.g.

range where .Pages "Params.foo" "!=" "foo"

I don’t know what kind of link you’re trying to hide. Is it for a regular page or a section list?

If it’s a regular page, I suppose that you could add a parameter in the relevant content file and then in sidebar.html try limiting the scope of range $pages with where.

So at the present, if I set audience parameter as below, and if I build my webpage via> hugo server -e dealers or without setting audience parameters the content should be seen.

But if the webpage build for employees lets say then it should be hidden completely. so far I can do that somehow but not able hidden the complete page or content from the my site.

So somehow, I need to this logic for sidebar as well but I am struggling to implement this.

---

title: "API Management"

linkTitle: "API Management"

weight: 1

audience: "dealers"

description: >

---

here is the sidebar-tree.html

{{ $shouldDelayActive := ge (len .Site.Pages) 2000 }}
<div id="td-sidebar-menu" class="td-sidebar__inner{{ if $shouldDelayActive }} d-none{{ end }}">
  {{ if not .Site.Params.ui.sidebar_search_disable }}
  <form class="td-sidebar__search d-flex align-items-center">
    {{ partial "search-input.html" . }}
    <button class="btn btn-link td-sidebar__toggle d-md-none p-0 ml-3 fas fa-bars" type="button" data-toggle="collapse" data-target="#td-section-nav" aria-controls="td-docs-nav" aria-expanded="false" aria-label="Toggle section navigation">
    </button>
  </form>
  {{ end }}
  <nav class="collapse td-sidebar-nav pt-2 pl-4" id="td-section-nav">
    {{ if  (gt (len .Site.Home.Translations) 0) }}
    <div class="nav-item dropdown d-block d-lg-none">
      {{ partial "navbar-lang-selector.html" . }}
    </div>
    {{ end }}
    {{ template "section-tree-nav-section" (dict "page" . "section" .FirstSection "delayActive" $shouldDelayActive)  }}
  </nav>
</div>
{{ define "section-tree-nav-section" }}
{{ $s := .section }}
{{ $p := .page }}
{{ $shouldDelayActive := .delayActive }}
{{ $active := eq $p.CurrentSection $s }}
{{ $show := or (and (not $p.Site.Params.ui.sidebar_menu_compact) ($p.IsAncestor $s)) ($p.IsDescendant $s) }}
{{ $sid := $s.RelPermalink | anchorize }}
<ul class="td-sidebar-nav__section pr-md-3">
  <li class="td-sidebar-nav__section-title">
    <a  href="{{ $s.RelPermalink }}" class="align-left pl-0 pr-2{{ if not $show }} collapsed{{ end }}{{ if $active}} active{{ end }} td-sidebar-link td-sidebar-link__section">{{ $s.LinkTitle }}</a>
  </li>
  <ul>
    <li class="collapse {{ if $show }}show{{ end }}" id="{{ $sid }}">
      {{ $pages := where (union $s.Pages $s.Sections).ByWeight ".Params.toc_hide" "!=" true }}
      {{ $pages := $pages | first 50 }}
      {{ range $pages }}
      {{ if .IsPage }}
      {{ $mid := printf "m-%s" (.RelPermalink | anchorize) }}
      {{ $active := eq . $p }}
      <a class="td-sidebar-link td-sidebar-link__page {{ if and (not $shouldDelayActive) $active }} active{{ end }}" id="{{ $mid }}" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
      {{ else }}
      {{ template "section-tree-nav-section" (dict "page" $p "section" .) }}
      {{ end }}
      {{ end }}
    </li>
  </ul>
</ul>
{{ end }}

here is the sidebar.html

{{/* The "active" toggle here may delay rendering, so we only cache this side bar menu for bigger sites. */}}
{{ $shouldCache := ge (len .Site.Pages) 2000 }}
{{ if $shouldCache }}
{{ $mid := printf "m-%s" (.RelPermalink | anchorize) }}
<script>
$(function() {
$("#td-sidebar-menu #{{ $mid }}").toggleClass("active");
$("#td-sidebar-menu").toggleClass("d-none");
});
</script>
{{ partialCached "sidebar-tree.html" . .CurrentSection.RelPermalink }}
{{ else }}
{{ partial "sidebar-tree.html" . }}
{{ end }}

Well… since you are using Environment Variables to generate different versions of the project you can use the .Hugo.Environment variable in the templates to conditionally display/hide the relevant sidebar links for the two different versions of the site.

For example

{{- if eq .Hugo.Environment "dealers" -}}
<-- sidebar links for dealers -->
{{- else -}}
<-- sidebar links for others -->
{{- end -}}

Also see:

https://gohugo.io/variables/hugo/

Nope this is not ı want cause ı can not solve this if I have over 50 users…

So here is the part how the titles listed in sidebar.As you said before probably I need to use where clause for {{ range $pages }}

But ı am struggling how can ı the do the samething like I did in baseofhtml using params. The rules are,

  • If there is no any audience parameter set in frontmatter list all the LinkTitle.

  • If there is any audience parameter set, and if it is equal to one when building a site show the page if it is not equal then hidden the page.

<ul>
        <li class="collapse {{ if $show }}show{{ end }}" id="{{ $sid }}">
          {{ $pages := where (union $s.Pages $s.Sections).ByWeight ".Params.toc_hide" "!=" true }}
          {{ $pages := $pages | first 50 }}
          {{ range $pages }}
          {{ if .IsPage }}
          {{ $mid := printf "m-%s" (.RelPermalink | anchorize) }}
          {{ $active := eq . $p }}
          <a class="td-sidebar-link td-sidebar-link__page {{ if and (not $shouldDelayActive) $active }} active{{ end }}" id="{{ $mid }}" href="{{ .RelPermalink }}">{{ .LinkTitle }}</a>
          {{ else }}
          {{ template "section-tree-nav-section" (dict "page" $p "section" .) }}
          {{ end }}
          {{ end }}
        </li>
      </ul>

The above can be done dynamically.

<! -- store the audience parameter in a variable -->
{{ $audience := .Params.audience }}
<! -- then check if the audience parameter is set in a content file -->
{{ with $audience }}
<! -- check if the audience parameter matches the environment variable -->
{{ if eq .Hugo.Environment $audience }}
<! -- show the page -->
{{ else }}
<! -- hide the page -->
{{ end }}
{{ else }}
<! -- list all .LinkTitle since there is no audience parameter set -->
{{ end }}

The above should meet your stated requirements, more or less.
Anyway you should get how conditional logic works in Hugo Templates.

ı could not explain the case really I feel (( I can able to hide or display the content but can hide the linktitle in side bar and I could not get how should I use the above code inside sidebar.html.

Let me ask another way… Can I able to title and Linktitle in frontmatter inside else section ? What ı mean instead of using this, This page is only accessible for {{ .Params.audience }}, if I can able to set the title and linkTitle like as below in else section that I will able to solve my issue i believe.

Can I override them as below ?

title: ""
linkTitle: ""

        {{- if .Params.audience }}
          {{- if in .Site.Params.audience (.Params.audience) }}
           {{ block "main" . }}{{ end }}
          {{- else }}
            This page is only accessible for {{ .Params.audience }}
          {{- end }}
        {{- else }}
         {{ block "main" . }}{{ end }}

No. You cannot set frontmatter parameters in content files with conditional logic, if that is what you’re asking.

Template logic can only be used within Hugo Archetypes and when these are invoked with the command hugo new then the generated frontmatter will honor whatever logic exists in the Archetype.

Anyway. I think that in this topic I’ve given you a fairly extensive introduction to conditional logic in Hugo/Go templates.

I cannot help further without seeing the project in question or at least a sample repository that follows the same structure with dummy content.

If you intend to provide one then in the README you need to specify precisely which page links should be displayed in the sidebar and under which conditions.

It s a private repository but let me try to find a solution…ok last question,ı ve another idea. so right now , due to ı can able to hide the content, as you can see below image there is nothing to display but unfortunatelly API Management still seen in side bar. So can ı use len here… What I mean just display the titles here where the len is greater than 0 or some specific amount… maybe that wway, due to len is zero here ı can hide the title in side bar as well

  {{ $pages := where (union $s.Pages $s.Sections).ByWeight ".Params.toc_hide" "!=" true }}
  {{ $pages := $pages | first 50 }}
  {{ range $pages }}
  {{ if .IsPage }}

You can use len to check for the length of the title and linkTitle values.

No I am actually asking add a where condition here {{ range $pages }} if the len of content greater than zero and it will display, if not it will hide probably

Depending on the way you want to hide your pages, be sure to check the RSS feed and the sidemap.xml too