diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index 6224c97..9791bb1 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -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'): diff --git a/hyde/tests/test_jinja2template.py b/hyde/tests/test_jinja2template.py index 822aab5..6d15d53 100644 --- a/hyde/tests/test_jinja2template.py +++ b/hyde/tests/test_jinja2template.py @@ -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"] \ No newline at end of file + assert twos == ["D", "E", "F"] + + def test_top_filter(self): + + text = """ +{% yaml test %} +item_list: + - A + - B + - C + - D + - E + - F +{% endyaml %} + + +""" + 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"] \ No newline at end of file