Source

garter / docs / content / urls.markdown

Full commit

<h1><a href="">URL Convenience Functions</a></h1>

URLs are a pain. Garter tries to help by providing some useful functions to make working with URLs easier.

The permalink decorator was taken from this snippet on the Flask site. It's used to wrap functions so they only need to return the arguments to Flask's url_for function, instead of calling the function themselves.

For example, say you have several classes that represents items on your site:

from flask import url_for

class Event(object):
    def __init__(self, event_id):
        self.event_id = event_id

    def link(self):
        return url_for('event', event_id=self.event_id)

class User(object):
    def __init__(self, username):
        self.username = username

    def link(self):
        return url_for('profile', username=self.username)

Using the permalink decorator can make the link functions a bit cleaner:

from garter.urls import permalink

class Event(object):
    def __init__(self, event_id):
        self.event_id = event_id

    @permalink
    def link(self):
        return 'event', { 'event_id': self.event_id }

class User(object):
    def __init__(self, username):
        self.username = username

    @permalink
    def link(self):
        return 'profile', { 'username': self.username }