-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added template tag. #11
Open
leandro-gomez
wants to merge
1
commit into
chronossc:master
Choose a base branch
from
leandro-gomez:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
*.pyc | ||
.*.sw* | ||
build | ||
.idea/ | ||
breadcrumbs_sample/test.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<div id='breadcrumb' class="breadcrumb"> | ||
{% for breadcrumb in breadcrumbs %} | ||
<a href="{{ breadcrumb.url }}">{{ breadcrumb.name }}</a> {% if extras or extra %} > {% endif %} | ||
{% endfor %} | ||
{% for name, url in extras %} | ||
{% if forloop.first and breadcrumb %} > {% endif %} | ||
<a href="{{ url }}">{{ name }}</a> {% if not forloop.last or extra %} > {% endif %} | ||
{% endfor %} | ||
{% if extra %} | ||
<span> {{ extra }} </span> | ||
{% endif %} | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#! coding: utf-8 | ||
|
||
|
||
|
||
__author__ = 'lgomez' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
#! coding: utf-8 | ||
from django import template | ||
|
||
register = template.Library() | ||
|
||
|
||
#http://stackoverflow.com/questions/4356329/creating-a-python-dictionary-from-a-line-of-text/4356415#4356415 | ||
def pairwise(iterable): | ||
#s -> (s0,s1), (s2,s3), (s4, s5), ... | ||
a = iter(iterable) | ||
return zip(a, a) | ||
|
||
|
||
@register.inclusion_tag('breadcrumbs/partial/breadcrumb.html', takes_context=True) | ||
def breadcrumbs(context, *args, **kwargs): | ||
""" | ||
This tag renders all breadcrumbs registered. | ||
But you can pass some arguments too. | ||
|
||
If you pass one parameter, it will be shown without an anchor, just what you write. | ||
i.e.: | ||
{% breadcrumb "profile" %} or {% breadcrumb my_context_var %} will print | ||
each breadcrumb registered and "profile" or what is in 'my_context_var' variable. | ||
|
||
e.g.: Registering two breadcrumb (see "utils.py" module): | ||
|
||
{% breadcrumb "profile" %} | ||
=> | ||
<a href="http://eg1.example.com"> breadcrumb_1</a> > | ||
<a href="http://eg2.example.com"> breadcrumb_2</a> > | ||
<span> profile</span> > | ||
|
||
If you pass more that one parameter, it takes in pairs. The first must be the what you | ||
want to show in the breadcrumb, and the second the url. If you pass a odd number of | ||
parameters, the last must be a "name". | ||
i.e.: You can do: | ||
{% breadcrumb "my foos" list_link %} and | ||
{% breadcrumb "my foos" list_link "foo_settings" %} and | ||
{% breadcrumb "my foos" list_link "that foo" foo_url %} and | ||
{% breadcrumb "my foos" list_link "that foo" foo_url "foo_settings" %} and so on | ||
|
||
e.g.: For readability I supposed no another breadcrumbs, just what I wrote here. | ||
But you can register breadcrumb and they will be shown before that: | ||
|
||
{% url "foo_detail" foo.id as foo_url %} | ||
{% url "foo_list" as list %} | ||
{% breadcrumb "my foos" list "that foo" foo_url "foo_settings" %} | ||
=> | ||
<a href="{{ list }}"> my foos </a> > | ||
<a href="{{ foo_url }}"> that foo </a> > | ||
<a span> "foo_settings" </span> > | ||
|
||
If you pass the kwarg "unpack" with value True, the template tag only takes 2 arguments. | ||
the first a iterable that contains tuple of (name, url,), and the second a extra argument, | ||
Thar will be shown (just a "name") | ||
i.e.: | ||
{% breadcrumb my_context_var unpack=True %} or | ||
{% breadcrumb my_context_var "settings_foo" unpack=True %} | ||
|
||
e.g.: | ||
In some class-based view: | ||
# ... | ||
|
||
class FooSettings(DetailView): | ||
model = Foo | ||
template = "foo_settings.html" | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super(FooSettings, self).get_context_data(**kwargs) | ||
context["list"] = ("my_foos", reverse("foos"),), ("foo", reverse("foo", args=[self.object.id,]),) | ||
return context | ||
|
||
# ... | ||
|
||
In "foo_settings.html" template: | ||
|
||
{% breadcrumb list unpack=True %} | ||
|
||
""" | ||
template_context = dict() | ||
unpack = kwargs.get('unpack', False) | ||
args_list = list(args) | ||
extras = [] | ||
extra = None | ||
args_len = len(args_list) | ||
|
||
if unpack: | ||
if args_len == 0: | ||
raise TypeError("Tag breadcrumb takes at least one argument with 'unpack=True' option.") | ||
iterable = args_list.pop(0) | ||
try: | ||
for arg in iterable: | ||
extras.append(arg) | ||
except TypeError as e: | ||
raise TypeError("Tag breadcrumbs take a iterable like first parameter with 'unpack=True'. %s" % e) | ||
args_len = len(args_list) | ||
|
||
if args_len % 2 != 0: | ||
extra = args_list.pop(-1) | ||
|
||
for name, url in pairwise(args_list): | ||
extras.append((name, url)) | ||
|
||
template_context['breadcrumbs'] = context['request'].breadcrumbs | ||
template_context['extras'] = extras | ||
template_context['extra'] = extra | ||
return template_context | ||
|
||
|
||
__author__ = 'lgomez' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from django.db import models | ||
|
||
# Create your models here. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{% extends "index.html" %} | ||
{% load breadcrumb_tags %} | ||
|
||
{% block content %} | ||
{% url "tag-index" as index_url %} | ||
|
||
{% breadcrumbs "tag-index-page" index_url "dynamic-breadcrumb" %} | ||
|
||
<p> You are generating dynamic breadcrumbs </p> | ||
You can click on them! | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
{% load breadcrumb_tags %} | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title></title> | ||
</head> | ||
<body> | ||
{% block content %} | ||
{% breadcrumbs "breadcrumb-tag-index" %} | ||
|
||
<p> You are on breadcrumb template tag page </p> | ||
Click on some link and you will see the "index" breadcrumb. | ||
{% endblock content %} | ||
|
||
<a href="{% url "tag-index" %}"> | ||
<div>Tag index page</div> | ||
</a> | ||
<a href="{% url "dynamic" %}"> | ||
<div>Dynamic page and url</div> | ||
</a> | ||
<a href="{% url "unpacking" %}"> | ||
<div>Unpack of first parameter</div> | ||
</a> | ||
|
||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{% extends "index.html" %} | ||
{% load breadcrumb_tags %} | ||
|
||
{% block content %} | ||
|
||
{% breadcrumbs extra_params "unpacking-breadcrumb" unpack=True %} | ||
|
||
<p> You are unpack breadcrumbs </p> | ||
You can click on them! | ||
{% endblock content %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
""" | ||
This file demonstrates writing tests using the unittest module. These will pass | ||
when you run "manage.py test". | ||
Replace this with more appropriate tests for your application. | ||
""" | ||
|
||
from django.test import TestCase | ||
|
||
|
||
class SimpleTest(TestCase): | ||
def test_basic_addition(self): | ||
""" | ||
Tests that 1 + 1 always equals 2. | ||
""" | ||
self.assertEqual(1 + 1, 2) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# Create your views here. | ||
from django.views.generic import TemplateView | ||
from breadcrumbs.utils import register_referer | ||
|
||
|
||
class IncludeReferer(TemplateView): | ||
def get_context_data(self, **kwargs): | ||
context = super(IncludeReferer, self).get_context_data(**kwargs) | ||
register_referer(self.request) # Including the referer if it exists | ||
return context | ||
|
||
|
||
class IncludingDynamicURLs(IncludeReferer): | ||
template_name = "dynamic.html" | ||
pass | ||
|
||
|
||
class UnpackingAVariableAndMore(IncludeReferer): | ||
template_name = "unpacking.html" | ||
|
||
def get_context_data(self, **kwargs): | ||
context = super(UnpackingAVariableAndMore, self).get_context_data(**kwargs) | ||
extra_params = ("name1", "example.com"), ("name2", "example2.com") | ||
context["extra_params"] = extra_params | ||
return context | ||
|
||
|
||
class Index(TemplateView): | ||
template_name = "index.html" |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you mind remove this file since it do nothing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, sorry