我有一个包含链接列表的 base.html 模板。
示例:
<div id="sidebar1">
<ul>
<li><a href="/" title="">Index</a></li>
<li><a href="/stuff/" title="" class="current">Stuff</a></li>
<li><a href="/about/" title="">About Me</a></li>
<li><a href="/contact/" title="">Contact Me</a></li>
</div>
然后我在views.py 中为每个index.html、stuff.html、about.html 和contact.html 定义了一个定义。 这些模板中的每一个都只是从 base.html 模板派生并设置各自的标题和内容。
我的问题是关于上面的/东西我有一个类=“当前”。
我想让我所在的当前页面具有该类属性。
我可以在每个视图中设置不同的变量,例如 current_page="about",然后在模板中与每个链接的每个类元素中的 {% ifequal %}
进行比较,但这似乎是重复工作(因为额外的视图变量)。
有没有更好的办法? 也许如果有一种方法可以获取模板自动填充的视图函数名称,我就不需要设置额外的变量? 而且看起来确实有很多 ifequals。
I have a base.html template that contains a list of links.
Example:
<div id="sidebar1">
<ul>
<li><a href="/" title="">Index</a></li>
<li><a href="/stuff/" title="" class="current">Stuff</a></li>
<li><a href="/about/" title="">About Me</a></li>
<li><a href="/contact/" title="">Contact Me</a></li>
</div>
Then I have in my views.py a definition for each of index.html, stuff.html, about.html and contact.html. Each of those templates simply derive from a base.html template and set their own respective titles and contents.
My question is about the above /stuff I have a class="current".
I'd like to make the current page that I'm on have that class attribute.
I could set a different variable in each view like current_page="about" and then do a compare in the template with {% ifequal %}
in each class element of each link , but that seems like duplicating work (because of the extra view variable).
Is there a better way? Maybe if there is a way to get the view function name that the template was filled from automatically I would not need to set the extra variable? Also it does seem like a lot of ifequals.
发布评论
评论(4)
这是一种优雅的方法,我从某个地方复制了它,我只希望我能记住在哪里,这样我就可以把功劳归给他们。 8-)
我为每个页面(或某个部分中的所有页面)分配一个
id
,如下所示:然后为相应的链接分配一个
id
: CSS 我设置了这样的规则:
所以这以一种主要是声明性的方式工作来控制当前页面所属的链接的样式。您可以在这里看到它的实际效果: http://entrian.com/source-search/
一旦你设置好它,它就是一个非常干净和简单的系统,因为:
switch
语句或if / else / else
语句我没有使用 Django,但这个系统可以在任何地方使用。 在您的情况下,您“设置各自的标题和内容”时,还需要设置
body id
,并且不需要其他 Django 标记。这个想法也很容易扩展到其他情况,例如。 “我希望除了下载页面本身之外,每个页面的侧边栏中都有一个下载链接。” 您可以在 CSS 中执行此操作,如下所示:
而不必将条件模板标记放入侧边栏 HTML 中。
Here's an elegant way to do this, which I copied from somewhere and I only wish I could remember where, so I could give them the credit. 8-)
I assign an
id
to each of my pages (or all the pages within a section) like this:And then an
id
for the corresponding links:And the in the CSS I set a rule like this:
So this works in a mostly declarative way to control the style of the link that the current page belongs in. You can see it in action here: http://entrian.com/source-search/
It's a very clean and simple system once you've set it up, because:
switch
statements orif / else / else
statementsI'm not using Django, but this system works anywhere. In your case, where you "set their own respective titles and contents" you also need to set the
body id
, and there's no other Django markup required.This idea extends easily to other situations as well, eg. "I want a download link in the sidebar on every page except the download pages themselves." You can do that in CSS like this:
rather than having to put conditional template markup in the sidebar HTML.
没有使用过 Django,但我在 Kohana (PHP) 和 Rails 中处理过同样的问题。
我在 Kohana 中做什么:
我在 Rails 中做什么:
Haven't used Django, but I've dealt with the same issue in Kohana (PHP) and Rails.
What I do in Kohana:
What I do in Rails:
我只看到几种方法可以做到这一点,同时避免重复的 ifequals:
Javascript. 类似于(jQuery)的东西:
更改视图以包含页面列表及其关联的标题和 URL,并在模板中创建一个 {% for %} 循环,该循环将遍历该列表,并添加一个单个 {% ifequal %}。
从我的立场来看,选项 2 是最喜欢的。 如果所有页面的逻辑都相同,只有模板不同,则您可以考虑为每个页面使用 FlatPages 模型。 如果逻辑不同,并且您需要不同的模型,您可以考虑使用某种菜单应用程序。 一个无耻的插件:我有一个我自己的菜单应用
I see only a couple of ways of doing it, while avoiding repeated ifequals:
Javascript. Something along the lines of (jQuery):
Change your views to contain a list of pages with their associated titles and URLs and create a {% for %} loop in your template, which will go through that list, and add a single {% ifequal %}.
Option 2 being the favorite from where I stand. If the logic for all of your pages is the same, and only the templates differ, you might consider using the FlatPages model for each of your pages. If the logic is different, and you need different models, you might consider using a menuing app of some sort. A shameless plug: I have a menuing app of my own
如果您添加
request
上下文处理器,则非常简单:现在您可以访问
HttpRequest
,其中包含请求路径。 突出显示当前页面只需检查路径是否与链接的目的地匹配,即您已经在那里:If you add the
request
context processor, it's pretty straightforward:Now you have access to the
HttpRequest
, which contains the request path. Highlighting the current page is a simple matter of checking if the path matches the link's destination, i.e., you're already there: