Ticket #3169: pagelist_tag.diff

File pagelist_tag.diff, 10.0 KB (added by frankie@…, 18 years ago)
  • django/template/defaulttags.py

     
    33from django.template import Node, NodeList, Template, Context, resolve_variable
    44from django.template import TemplateSyntaxError, VariableDoesNotExist, BLOCK_TAG_START, BLOCK_TAG_END, VARIABLE_TAG_START, VARIABLE_TAG_END, SINGLE_BRACE_START, SINGLE_BRACE_END, COMMENT_TAG_START, COMMENT_TAG_END
    55from django.template import get_library, Library, InvalidTemplateLibrary
     6from django.template.loader import get_template
    67from django.conf import settings
    78import sys
    89
     
    222223        and_ = 0,
    223224        or_ = 1
    224225
     226class PageListNode(Node):
     227    def __init__(self, surround=0, firstlast=False, urlprefix='?page=', template='pagelists/default.html'):
     228        self.surround = surround
     229        self.firstlast = firstlast
     230        self.urlsuffix = "/"
     231        if urlprefix.startswith('?'):
     232            self.urlprefix = urlprefix
     233            self.urlsuffix = ""
     234        elif urlprefix.startswith('/'):
     235            self.urlprefix = urlprefix
     236        else:
     237            self.urlprefix = '../'+urlprefix
     238        self.template_name = template
     239
     240    def render(self, context):
     241        if self.surround:
     242            surround_start = context['page'] - self.surround
     243            surround_end = context['page'] + self.surround
     244            if self.firstlast and surround_start <= 1:
     245                surround_start = 2
     246            elif surround_start < 1:
     247                surround_start = 1
     248            if self.firstlast and surround_end >= context['pages']:
     249                surround_end = context['pages'] - 1
     250            elif surround_end > context['pages']:
     251                surround_end = context['pages']
     252            page_list = range(surround_start, surround_end+1)
     253        else:
     254            if self.firstlast:
     255                page_list = range(2, context['pages'])
     256            else:
     257                page_list = range(1, context['pages'] + 1)
     258        page_index = []
     259        for page in page_list:
     260            page_index.append({'number': page, 'link': self.urlprefix+str(page)+self.urlsuffix})
     261        tmpl_context = Context()
     262        tmpl_context.update(context)
     263        tmpl_context['page_index'] = page_index
     264        tmpl_context['firstlast'] = self.firstlast
     265        if self.firstlast:
     266            tmpl_context['first_link'] = self.urlprefix+"1"+self.urlsuffix
     267            tmpl_context['last_link'] = self.urlprefix+str(context['pages'])+self.urlsuffix
     268
     269        try:
     270            t = get_template(self.template_name)
     271            return t.render(tmpl_context)
     272        except:
     273            if settings.TEMPLATE_DEBUG:
     274                raise
     275            else:
     276                return ''
     277       
    225278class RegroupNode(Node):
    226279    def __init__(self, target, expression, var_name):
    227280        self.target, self.expression = target, expression
     
    675728ifchanged = register.tag(ifchanged)
    676729
    677730#@register.tag
     731def pagelist(parser, token):
     732    """
     733    Lists an index of pages.
     734
     735    This tag uses a template to display an index of pages. It requires ``page``
     736    and ``pages`` to be in the context where it is used. These variable will
     737    already be in the context if you are using the ``object_list`` generic
     738    view. It can be used like this::
     739
     740        {% pagelist %}
     741
     742    If it is it will output a list of all the pages with links. If you only
     743    want a certain number of pages surrounding the current page on the list
     744    then you can use ``surround`` followed by a number. For example::
     745
     746        {% pagelist surround 3 %}
     747
     748    If you want the first and last pages to be displayed as well then you can
     749    use ``firstlast``. For example::
     750
     751        {% pagelist surround 3 firstlast true %}
     752
     753    By default, pagelists assume that pages are specified by the GET variable
     754    page. So any links it creates look like "?page=1". You can override this
     755    and specify either absolute urls like "/stuff/list/page" or relative urls
     756    like "page". These are specified using ``urlprefix``. For example::
     757
     758        {% pagelist urlprefix /stuff/list/page %}
     759
     760    Another example::
     761
     762        {% pagelist urlprefix page %}
     763
     764    These would use create links that look like "/stuff/list/page1/" and "../page1/"
     765    respectively.
     766
     767    By default, pagelists are rendered via the template
     768    ``pagelists/default.html``, but you can override that for a particular
     769    pagelist using ``template``. For example::
     770
     771        {% pagelist template pagelists/mypagelist.html %}
     772
     773    Creating the ``pagelists/default.html`` template is your responsibility; in
     774    your template directory, just create a ``pagelists`` directory containing a
     775    file ``default.html``.
     776
     777    Pagelist templates are passed two context variables in addition to all
     778    all context variables in the template the pagelist tag was used in.
     779
     780        * ``firstlast``: Whether to display the first and last pages.
     781
     782        * ``page_index``: A list of pages. Each page has two attributes: ``number``,
     783        the page number and ``link``: the link to the page based on
     784        ``urlprefix``.
     785
     786    If ``firstlast`` is true two more context variables are passed in.
     787
     788        * ``first_link``: A link to the first page based on ``urlprefix``.
     789
     790        * ``last_link``: A link to the last page based on ``urlprefix``.
     791
     792    Here's a sample ``pagelists/default.html`` template::
     793
     794        <ul class="pagelist">
     795            {% if firstlast %}<li><a href="{{ first_link }}" title="First">&lt;&lt; First</a></li>{% endif %}
     796            {% for other_page in page_index %}
     797                <ol
     798                    {% ifequal other_page.number previous %}class="previous"{% endifequal %}
     799                    {% ifequal other_page.number page %}class="current"{% endifequal %}
     800                    {% ifequal other_page.number next %}class="next"{% endifequal %}
     801                >
     802                    <a href="{{ other_page.link }}" title="Page {{ other_page.number }}">
     803                        {{ other_page.number }}
     804                    </a>
     805                </ol>
     806            {% endfor %}
     807            {% if firstlast %}<li><a href="{{ last_link }}" title="Last">Last &gt;&gt;</a></li>{% endif %}
     808        </ul>
     809    """
     810    bits = token.contents.split()
     811    kwargs = {}
     812    is_key = True
     813    for bit in bits[1:]:
     814        if is_key:
     815            key = bit
     816        else:
     817            if key == 'surround':
     818                bit = int(bit)
     819            elif key == 'firstlast':
     820                bit = bit.lower() == 'true'
     821            kwargs[key] = bit
     822        is_key = not is_key
     823    return PageListNode(**kwargs)
     824
     825pagelist = register.tag(pagelist)
     826
     827#@register.tag
    678828def ssi(parser, token):
    679829    """
    680830    Output the contents of a given file into the page.
  • docs/templates.txt

     
    699699
    700700(Displays "It is the 4th of September" %}
    701701
     702pagelist
     703~~~~~~~~
     704
     705Lists an index of pages.
     706
     707This tag uses a template to display an index of pages. It requires ``page``
     708and ``pages`` to be in the context where it is used. These variable will
     709already be in the context if you are using the ``object_list`` generic
     710view. It can be used like this::
     711
     712    {% pagelist %}
     713
     714If it is it will output a list of all the pages with links. If you only
     715want a certain number of pages surrounding the current page on the list
     716then you can use ``surround`` followed by a number. For example::
     717
     718    {% pagelist surround 3 %}
     719
     720If you want the first and last pages to be displayed as well then you can
     721use ``firstlast``. For example::
     722
     723    {% pagelist surround 3 firstlast true %}
     724
     725By default, pagelists assume that pages are specified by the GET variable
     726page. So any links it creates look like "?page=1". You can override this
     727and specify either absolute urls like "/stuff/list/page" or relative urls
     728like "page". These are specified using ``urlprefix``. For example::
     729
     730    {% pagelist urlprefix /stuff/list/page %}
     731
     732Another example::
     733
     734    {% pagelist urlprefix page %}
     735
     736These would use create links that look like "/stuff/list/page1/" and "../page1/"
     737respectively.
     738
     739By default, pagelists are rendered via the template
     740``pagelists/default.html``, but you can override that for a particular
     741pagelist using ``template``. For example::
     742
     743    {% pagelist template pagelists/mypagelist.html %}
     744
     745Creating the ``pagelists/default.html`` template is your responsibility; in
     746your template directory, just create a ``pagelists`` directory containing a
     747file ``default.html``.
     748
     749Pagelist templates are passed two context variables in addition to all
     750all context variables in the template the pagelist tag was used in.
     751
     752    * ``firstlast``: Whether to display the first and last pages.
     753
     754    * ``page_index``: A list of pages. Each page has two attributes: ``number``,
     755      the page number and ``link``: the link to the page based on
     756      ``urlprefix``.
     757
     758If ``firstlast`` is true two more context variables are passed in.
     759
     760    * ``first_link``: A link to the first page based on ``urlprefix``.
     761
     762    * ``last_link``: A link to the last page based on ``urlprefix``.
     763
     764Here's a sample ``pagelists/default.html`` template::
     765
     766    <ul class="pagelist">
     767        {% if firstlast %}<li><a href="{{ first_link }}" title="First">&lt;&lt; First</a></li>{% endif %}
     768        {% for other_page in page_index %}
     769            <ol
     770                {% ifequal other_page.number previous %}class="previous"{% endifequal %}
     771                {% ifequal other_page.number page %}class="current"{% endifequal %}
     772                {% ifequal other_page.number next %}class="next"{% endifequal %}
     773            >
     774                <a href="{{ other_page.link }}" title="Page {{ other_page.number }}">
     775                    {{ other_page.number }}
     776                </a>
     777            </ol>
     778        {% endfor %}
     779        {% if firstlast %}<li><a href="{{ last_link }}" title="Last">Last &gt;&gt;</a></li>{% endif %}
     780    </ul>
     781
    702782regroup
    703783~~~~~~~
    704784
Back to Top