Code Examples

Psycojoker edited this page Jul 24, 2011 · 10 revisions

Table of Contents

  • Storing CONTEXT-entities in YAML-files
  • Django: Sorted looping over dictionaries
  • Installing new Django apps and filters

Storing CONTEXT-entities in YAML-files

In this example, data resides in ROOTPATH/data/stuff.yaml. It is read in settings.py and can then be used in all your templates and documents.

stuff.yaml:

 -
   name: "table"
   price: "100$"
 -
   name: "chair"
   price: "30$"

settings.py:

import yaml
DATA_PATH = ROOT_PATH + "/data"
CONTEXT['stuff'] = yaml.load( open(DATA_PATH + "/stuff.yaml") )

layout_example.html:

{% extends "skeleton/_body.html" %}
{% block content_body %}
{% for item in stuff %}
<p>A {{item.name}} is worth {{item.price}}.</p>
{% endfor %}
{% endblock %}

Django: Sorted looping over dictionaries

This is not hyde-specific, but a general Django issue. Suppose you have a dictionary of name abbrevs and full names

presidents = {
    'gebush': {'forename': 'George', 'surname': 'Bush'},
    'baobam': {'forename': 'Barack', 'surname': 'Obama'},
    'biclin': {'forename': 'Bill', 'surname': 'Clinton'},
}

Now, suppose you want to loop over the dictionary while sorting by surnames. Use the following Django template code:
{% for president in presidents.values|dictsort:"surname" %}
{{president.forename}} {{president.surname}}
{% endfor %}

Installing new Django apps and filters

In this example we want to obfuscate e-mail addresses with ROT13. It is as simple as that

  • create a dir django/ in your hyde directory
  • create the empty files django/mailobfuscate/__init__.py and django/mailobfuscate/templatetags/__init__.py
  • Install the mail obfuscation snippet to django/mailobfuscate/templatetags/mailobfuscate.py
  • add to your settings.py
    import sys
    DJANGO_PATH = os.path.join(ROOT_PATH, "django")
    sys.path.append(DJANGO_PATH)
  • Add to INSTALLED_APPS in settings.py
    INSTALLED_APPS = (
        'hydeengine',
        'django.contrib.webdesign',
        'mailobfuscate',
    )
  • In your content HTML file simply use
    {% load mailobfuscate %}{% filter obfuscate %}blablubb.org{% endfilter %}@
You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.
Press h to open a hovercard with more details.