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 }}
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.
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
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.
{{/* 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 -}}
<! -- 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 }}
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