django-treemenus icon indicating copy to clipboard operation
django-treemenus copied to clipboard

Default model additions for expanding menus

Open alastc opened this issue 14 years ago • 0 comments

Hi,

I was trying to get treemenus to create an expanding menu effect, where the children of the current page get expanded, but not other sections. (e.g. http://ukwindsurfing.com/information/regions/). NB: I'm not a programmer as such, so I may have missed a point or two.

I've got it working, as it's a similar problem to highlighting the current page. However, it's much easier with a couple of additions to the menu item model:

def this_section(self):
    url_part = self.url
    list_of_sections = url_part.split("/")
    section = list_of_sections[-2]
    return section

def matching_url(self):
    return "^" + self.url + "$"

Combined with the code from 'Automatically select menu items' section you can use this in the templates:

{% load tree_menu_tags %}
{% if menu_item.has_children %}
<li>{% if menu_item.matching_url|match_path:request.path %}<strong class="nolink">{{ menu_item.caption }}</strong>{% else %}
    <a href="{{ menu_item.url }}">{{ menu_item.caption }}</a>{% endif %}
    {% if menu_item.this_section in request.path %}
    <ul>
        {% for child in menu_item.children %}
            {% show_menu_item child %}
        {% endfor %}
    </ul>{% endif %}
</li>{% else %}
<li>{% if menu_item.matching_url|match_path:request.path %}<strong class="nolink">{{ menu_item.caption }}</strong>{% else %}
<a href="{{ menu_item.url }}">{{ menu_item.caption }} </a>{% endif %}</li>{% endif %}

I know you've outlined extension methods, but as someone with limited programming experience, it seems a lot simpler to make a couple of additions to the model (and the match_path addition to the template tags).

Could those be included by default?

I've created a patch if that helps, which is my first foray into Git.

alastc avatar Nov 27 '10 18:11 alastc