Thursday 1 January 2009 — This is almost 16 years old. Be careful.
I like using unusual text characters to decorate my site, for example, my home page uses lots of mid-dots (· ·) and chevrons (» »), as well as other special characters. To keep the HTML source from being cluttered with those inscrutable numeric entities, I wrote this Django tag:
special_ch = {
'': '',
'>>': '»', # RIGHT-POINTING DOUBLE ANGLE QUOTATION MARK
'<<': '«', # LEFT-POINTING DOUBLE ANGLE QUOTATION MARK
'(c)': '©', # COPYRIGHT SIGN
'S': '§', # SECTION SIGN
'*': '•', # BULLET
'.': '·', # MIDDLE DOT
'-': '–', # EN DASH
'--': '—', # EM DASH
':>': '▶', # BLACK RIGHT-POINTING TRIANGLE
'o': '◦', # WHITE BULLET
'[]': '▫', # WHITE SMALL SQUARE
'<>': '◇', # WHITE DIAMOND
}
@register.simple_tag
def ch(value):
return ' '.join([special_ch[s] for s in value.split(' ')])
Now I can use the ch tag with a mnemonic representation of the character in question. Spaces become non-breaking spaces to help control the layout around these characters:
<p>{% ch ">> " %}more text..</p>
<p>{% ch "(c) " %}2002{% ch "-" %}2009</p>
becomes
» more text..
© 2002–2009
The tag reference takes more space than the entities, but I can tell how they will display, without having to memorize the Unicode code points.
Comments
http://www.chami.com/tips/internet/050798I.html seems to be a comprehensive list of named entities.
I thought we had a conversation about this a few years ago, but that was Keith Devens, not you. :-p
@Bryan: I've got a bit of Stockholm syndrome from working with XML files that makes me think I have to use numeric entities. Named entities are a good option for HTML files (and templates) though.
Add a comment: