A route is a key/value in your configuration file.
-The key starts with ``path`` and then has a number after that. For
-example: ``path1``, ``path2``, ... ``path15``, ...
+The key for the route is the route name You can use this with `url()`
+in templates to have MediaGoblin automatically build the urls for
+you. It's very handy.
-The value has three parts separated by commas:
+It should be "unique" and it should be alphanumeric characters and
+hyphens. I wouldn't put spaces in there.
-1. **route name**: You can use this with `url()` in templates to have
- MediaGoblin automatically build the urls for you. It's very handy.
+Examples: ``flatpages-about``, ``about-view``, ``contact-view``, ...
- It should be "unique" and it should be alphanumeric characters and
- hyphens. I wouldn't put spaces in there.
+The value has two parts separated by commas:
- Examples: ``flatpages-about``, ``about-view``, ``contact-view``, ...
-
-2. **route path**: This is the url that this route matches.
+1. **route path**: This is the url that this route matches.
Examples: ``/about``, ``/contact``, ``/pages/about``, ...
- Technically, this is a regular expression and you can do anything
- with this that you can do with the routepath parameter of
- `routes.Route`. For more details, see `the routes documentation
- <http://routes.readthedocs.org/en/latest/>`_.
+ You can do anything with this that you can do with the routepath
+ parameter of `routes.Route`. For more details, see `the routes
+ documentation <http://routes.readthedocs.org/en/latest/>`_.
Example: ``/siteadmin/{adminname:\w+}``
For example: ``'/siteadmin/{adminname:\w+}'``
-3. **template**: The template to use for this url. The template is in
+2. **template**: The template to use for this url. The template is in
the flatpagesfile template directory, so you just need to specify
the file name.
"About this site" page and one for a "Terms of service" page::
[[mediagoblin.plugins.flatpagesfile]]
- page1 = about-view, '/about', about.html
- page2 = terms-view, '/terms', terms.html
+ about-view = '/about', about.html
+ terms-view = '/terms', terms.html
+
+
+.. Note::
+
+ The order in which you define the routes in the config file is the
+ order in which they're checked for incoming requests.
Templates
another, take advantage of Jinja2 template extending and create a
base template that the others extend.
+
+Recipes
+=======
+
+Url variables
+-------------
+
+You can handle urls like ``/about/{name}`` and access the name that's
+passed in in the template.
+
+Sample route::
+
+ about-page = '/about/{name}', about.html
+
+Sample template::
+
+ {% extends "flatpagesfile/base.html" %}
+ {% block mediagoblin_content %}
+
+ <h1>About page for {{ request.matchdict['name'] }}</h1>
+
+ {% endblock %}
+
+See the `the routes documentation
+<http://routes.readthedocs.org/en/latest/>`_ for syntax details for
+the route. Values will end up in the ``request.matchdict`` dict.
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-# This imports the main module which has the FlatPagesPlugin class
-# which does all the work.
-import mediagoblin.plugins.flatpagesfile.main
+import logging
+import os
+
+import jinja2
+from routes.route import Route
+
+from mediagoblin.tools import pluginapi
+from mediagoblin.tools.response import render_to_response
+
+
+PLUGIN_DIR = os.path.dirname(__file__)
+
+
+_log = logging.getLogger(__name__)
+
+
+@jinja2.contextfunction
+def print_context(c):
+ s = []
+ for key, val in c.items():
+ s.append('%s: %s' % (key, repr(val)))
+ return '\n'.join(s)
+
+
+def flatpage_handler_builder(template):
+ """Flatpage view generator
+
+ Given a template, generates the controller function for handling that
+ route.
+
+ """
+ def _flatpage_handler_builder(request):
+ return render_to_response(
+ request, 'flatpagesfile/%s' % template,
+ {'request': request})
+ return _flatpage_handler_builder
+
+
+class FlatpagesFilePlugin(pluginapi.Plugin):
+ """
+ This is the flatpages plugin class. See the README for how to use
+ flatpages.
+ """
+ def setup_plugin(self):
+ self.config = pluginapi.get_config('mediagoblin.plugins.flatpagesfile')
+
+ _log.info('Setting up flatpagesfile....')
+
+ # Register the template path.
+ pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
+
+ pages = self.config.items()
+
+ routes = []
+ for name, (url, template) in pages:
+ name = 'flatpagesfile.%s' % name.strip()
+ controller = flatpage_handler_builder(template)
+ routes.append(
+ Route(name, url, controller=controller))
+
+ pluginapi.register_routes(routes)
+ _log.info('Done setting up flatpagesfile!')
+++ /dev/null
-# GNU MediaGoblin -- federated, autonomous media hosting
-# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
-#
-# This program is free software: you can redistribute it and/or modify
-# it under the terms of the GNU Affero General Public License as published by
-# the Free Software Foundation, either version 3 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-# GNU Affero General Public License for more details.
-#
-# You should have received a copy of the GNU Affero General Public License
-# along with this program. If not, see <http://www.gnu.org/licenses/>.
-
-import logging
-import os
-
-from routes.route import Route
-
-from mediagoblin.tools import pluginapi
-from mediagoblin.tools.response import render_to_response
-
-
-PLUGIN_DIR = os.path.dirname(__file__)
-
-
-_log = logging.getLogger(__name__)
-
-
-def flatpage_handler(template):
- """Flatpage view generator
-
- Given a template, generates the controller function for handling that
- route.
-
- """
- def _flatpage_handler(request, *args, **kwargs):
- return render_to_response(
- request, 'flatpagesfile/%s' % template,
- {'args': args, 'kwargs': kwargs})
- return _flatpage_handler
-
-
-class FlatpagesFilePlugin(pluginapi.Plugin):
- """
- This is the flatpages plugin class. See the README for how to use
- flatpages.
- """
- def setup_plugin(self):
- self.config = pluginapi.get_config('mediagoblin.plugins.flatpagesfile')
-
- _log.info('Setting up flatpagesfile....')
-
- # Register the template path.
- pluginapi.register_template_path(os.path.join(PLUGIN_DIR, 'templates'))
-
- # Set up and register routes.
- pages = [(int(key.replace('page', '')), val)
- for key, val in self.config.items()
- if key.startswith('page')]
-
- pages = [mapping for index, mapping in sorted(pages)]
- routes = []
- for name, url, template in pages:
- name = 'flatpagesfile.%s' % name.strip()
- controller = flatpage_handler(template)
- routes.append(
- Route(name, url, controller=controller))
-
- pluginapi.register_routes(routes)
- _log.info('Done setting up flatpagesfile!')