Browse Source

Added top and islice filters

main
Lakshmi Vyasarajan 13 years ago
parent
commit
c41354a304
2 changed files with 45 additions and 1 deletions
  1. +9
    -0
      hyde/ext/templates/jinja.py
  2. +36
    -1
      hyde/tests/test_jinja2template.py

+ 9
- 0
hyde/ext/templates/jinja.py View File

@@ -5,6 +5,7 @@ Jinja template utilties

from datetime import datetime, date
import re
import itertools

from hyde.fs import File, Folder
from hyde.model import Expando
@@ -67,6 +68,12 @@ def date_format(ctx, dt, fmt=None):
return dt.strftime(format)


def islice(iterable, start=0, stop=3, step=1):
return itertools.islice(iterable, start, stop, step)

def top(iterable, count=3):
return islice(iterable, stop=count)

def xmldatetime(dt):
if not dt:
dt = datetime.now()
@@ -555,6 +562,8 @@ class Jinja2Template(Template):
self.env.filters['syntax'] = syntax
self.env.filters['date_format'] = date_format
self.env.filters['xmldatetime'] = xmldatetime
self.env.filters['islice'] = islice
self.env.filters['top'] = top

config = {}
if hasattr(site, 'config'):


+ 36
- 1
hyde/tests/test_jinja2template.py View File

@@ -523,4 +523,39 @@ two:
assert ones == ["A", "B", "C"]

twos = [item.text for item in actual("ul.two li")]
assert twos == ["D", "E", "F"]
assert twos == ["D", "E", "F"]

def test_top_filter(self):

text = """
{% yaml test %}
item_list:
- A
- B
- C
- D
- E
- F
{% endyaml %}
<ul class="top">
{% for value in test.item_list|top(3) %}
<li>{{ value }}</li>
{% endfor %}
</ul>
<ul class="mid">
{% for value in test.item_list|islice(3, 6) %}
<li>{{ value }}</li>
{% endfor %}
</ul>
"""
t = Jinja2Template(JINJA2.path)
t.configure(None)
html = t.render(text, {}).strip()
print html
actual = PyQuery(html)
assert actual("ul").length == 2
assert actual("li").length == 6
items = [item.text for item in actual("ul.top li")]
assert items == ["A", "B", "C"]
items = [item.text for item in actual("ul.mid li")]
assert items == ["D", "E", "F"]

Loading…
Cancel
Save