Variable for URL of current page in pelican templates
Asked Answered
A

1

11

I'm new to Pelican. I'm structuring my site so that I have 2 categories: Blog and Projects. I have 3 menu buttons: Home, Blog, and Projects. I'm trying to edit my base.html template file so that the Blog button is active if I'm in blog/ or any subdirectory thereof, and the Projects button is active if I'm in projects/ or any subdirectory thereof. If I had a variable accessible in base.html that gives me the relative URL of the current page, I could split it by / and get first directory in the path. I've searched around and I can't seem to find a variable for the relative URL of the current page. Is there either a built-in variable or a way for me to make a custom one for what I'm looking for?

Amatruda answered 2/12, 2016 at 2:39 Comment(0)
M
12

As I am also new to pelican, please take the following advice with a grain of salt.

This variable will give you the name of the current file, as stated in the documentation:

{{ output_file }}

For example: When generating menu items for my pages, I can check against the "save_as" property of the page to highlight the exact corresponding menu item:

{% if output_file == p.save_as %}active{% endif %}

But in your case it should be e.g. sufficent to check it the current page is an article at all (assuming that "Blog" contains all of your articles) to highlight the blog menu item. For example by checking if the variable "article" is present:

{% if article %}class="active"{% endif %}

If your projects are made of "pages", simply check against the primary variable of those content type:

{% if page %}class="active"{% endif %}

For the home page, categories, archives etc. one can check the content of "page_name":

{% if page_name == 'index' %}class="active"{% endif %}
Markley answered 12/2, 2017 at 21:40 Comment(1)
you cannot know how your answer helped me! I was looking how to check if not article and pages.. your if page_name is exactly what I needed!Simonne

© 2022 - 2024 — McMap. All rights reserved.