Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix #93 by implementing a title algorithm, instead of relying on stdlib's lame version... #102

Closed
wants to merge 3 commits into from

2 participants

@njl

The stdlib title function provides a fundamentally correct and
completely useless version of the title function, that defines
words as things seperated by not letters. This changes the semantics to
make words things that are seperated by spaces.

@njl njl Fixes Issue #93 by implementing a title algorithm
The stdlib title function provides a fundamentally correct and
completely useless version of the title function, that defines
words as things seperated by not letters. This changes the semantics to
make words things that are seperated by spaces.
7fe70fd
@mitsuhiko
Owner

This will destroy different kinds of whitespace. This is better solved with unicode regular expressions.

@mitsuhiko
Owner

Eg:

for item in re.compile(r'(\s+)(?u)').split(...):
  #item is either whitespace or item
  ...
njl added some commits
@njl njl Fix #77 by adding note to switching document
Explaining that {% empty %} in Django loops is {% else %} in Jinja2.
37bcc69
@njl njl Fix to #93, this time respecting whitespace
Rejigger of title split to respect whitespace, also capitalize follow-on
hyphenated words.
c4b6fd3
@njl njl closed this
@njl

Closed, separate, cleaner pull is #103

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 12, 2012
  1. @njl

    Fixes Issue #93 by implementing a title algorithm

    njl authored
    The stdlib title function provides a fundamentally correct and
    completely useless version of the title function, that defines
    words as things seperated by not letters. This changes the semantics to
    make words things that are seperated by spaces.
  2. @njl

    Fix #77 by adding note to switching document

    njl authored
    Explaining that {% empty %} in Django loops is {% else %} in Jinja2.
  3. @njl

    Fix to #93, this time respecting whitespace

    njl authored
    Rejigger of title split to respect whitespace, also capitalize follow-on
    hyphenated words.
This page is out of date. Refresh to see the latest.
View
22 docs/switching.rst
@@ -177,9 +177,25 @@ operator. Here are some examples::
Loops
~~~~~
-For loops work very similar to Django, the only incompatibility is that in
-Jinja2 the special variable for the loop context is called `loop` and not
-`forloop` like in Django.
+For loops work very similar to Django. Notably, in Jinja2 the special variable for
+the loop context is called `loop` and not `forloop` like in Django.
+
+In addition, the Django `empty` argument is called `else` in Jinja2. For example, the
+Django template::
+
+ {% for item in items %}
+ {{item}}
+ {% empty %}
+ No items!
+ {% endfor %}
+
+would be handled in Flask as::
+
+ {% for item in items %}
+ {{item}}
+ {% else %}
+ No items!
+ {% endfor %}
Cycle
~~~~~
View
8 jinja2/filters.py
@@ -176,7 +176,13 @@ def do_title(s):
"""Return a titlecased version of the value. I.e. words will start with
uppercase letters, all remaining characters are lowercase.
"""
- return soft_unicode(s).title()
+ rv = []
+ for item in re.compile(r'([-\s]+)(?u)').split(s):
+ if not item:
+ rv.append(item)
+ continue
+ rv.append(item[0].upper() + item[1:])
+ return ''.join(rv)
def do_dictsort(value, case_sensitive=False, by='key'):
View
10 jinja2/testsuite/filters.py
@@ -193,6 +193,16 @@ def test_string(self):
def test_title(self):
tmpl = env.from_string('''{{ "foo bar"|title }}''')
assert tmpl.render() == "Foo Bar"
+ tmpl = env.from_string('''{{ "foo's bar"|title }}''')
+ assert tmpl.render() == "Foo's Bar"
+ tmpl = env.from_string('''{{ "foo bar"|title }}''')
+ assert tmpl.render() == "Foo Bar"
+ tmpl = env.from_string('''{{ "f bar f"|title }}''')
+ assert tmpl.render() == "F Bar F"
+ tmpl = env.from_string('''{{ "foo-bar"|title }}''')
+ assert tmpl.render() == "Foo-Bar"
+ tmpl = env.from_string('''{{ "foo\tbar"|title }}''')
+ assert tmpl.render() == "Foo\tBar"
def test_truncate(self):
tmpl = env.from_string(
Something went wrong with that request. Please try again.