I want to take the URL from request.path in a template and do a test on just the first word. If my URL is this:
/estimates/commercial
I want to somehow remove the commercial from the request.path. I was hoping this is possible within the template, too, since I need to do this check at every page.
Is this possible within my Django template?
EDIT – for clarification
The purpose of this is to highlight the navigation link based on what page is currently being viewed. I have a main nav and a sub nav and I want to highlight as follows:
main nav --> [Systems][Estimates]
"Systems" sub nav ---> [New][Details][Invoives]
If I am in the Details section of the Systems section, I want the words System and Details to be a different colour, or underlined, or something.
Your options are:
Write a ContextProcessor
Write a custom template filter. I’d reccomend just writing a split template filter and then using the join and slice filters to get the desired affect. However, it might be easier to just write a filter that does the whole thing.
Truthfully I’d consider #1 to be the better option. You are using context processors already right? If not, now is the time =)
Edit:
If your path was /something/foo/bar/estimates/commercial/
would give you 2 context vars with ‘estimates’ and ‘commercial’ as their values. This idea is pretty easy to expand, or even make more abstract and allow an arbitrary number of context variables being added.