I have 2 pages index.htm and links.htm and a partial sidebar.htm.
I'm trying to get my sidebar to display different content based on whether index.htm or links.htm is active.
Reading through the documentation it looks as if you can do this with the param.tab but I can't get it to work and not sure if this is the best way to accomplish this seeing as it's a partial.
sidebar.htm
description = "sidebar"
url = "/sidebar/:{% this.page.url %}"
==
{% if this.param.tab == 'links' %}
Display this if page = links
{% elseif this.param.tab == 'index' %}
Display this is page = index
{% endif %}
In blade I used - @elseif(isset($guidesslugs) && $guidesslugs->guidesslug == 'area-guide') but I'm not sure how to accomplish the same thing using twig or whether it's possible to used the param.tab on a partial?
There's multiple ways to do it.
First of all, partials may not have the url
parameter into config section.
The Configuration section is optional for partials and can contain the optional
description
parameter which is displayed in the back-end user interface.
You could use param
if the sidebar has different content based on the same page. Example:
pages/home.htm
url = "/home/:type"
==
{% partial 'sidebar' type=this.param.type %}
partials/sidebar.htm
{% if type == 'foo' %}
{# foo type #}
{% elseif type == 'bar' %}
{# bar type #}
{% else %}
{# anything... #}
{% endif %}
When including partials you can pass variables, in this case, we injected type
as this.param.type
value. You could also access this.param
(global) variable inside partial:
partials/sidebar.htm
{% if this.param.type == 'foo' %}
{# foo type #}
{% endif %}
If I understand your question, you have two different pages where each has his own distinct url so doesn't seems like a case for param
property.
Use this.page
variable.
You're able to access the page url
defined into page configuration section by using:
{{ this.page.url }}
This property seems like hidden from docs .
So considering your structure:
pages/home
url = "/home"
layout = "default"
==
{% partial 'sidebar' %}
pages/links
url = "/links"
layout = "default"
==
{% partial 'sidebar' %}
partials/sidebar
{% if this.page.url == '/home' %}
{# home page #}
{% elseif this.page.url == '/links' %}
{# links page #}
{% else %}
{# other pages #}
{% endif %}
As I said, there's so many ways to do this by defining variables, passing parameters to partial and others.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.