Skip to content
Browse files

Added the *into* argument. You can now display any instance directly …

…without creating and rendering a template.
  • Loading branch information...
1 parent e32dbfc commit dc9e2f91c2dbddaf0a16f60ab700ea96b4746cc8 @bartTC committed Mar 21, 2009
View
56 README
@@ -70,9 +70,46 @@ The arguments in detail:
default template ``<applabel>/<modelname>/flatblock.html`` is used. This
argument can be a context-variable.
+ In this template are all context-variables from the *parent* template
+ available plus some extra variables:
+
+ - ``object``: This variable is the model-instance for the generic block.
+
+ - ``generic_object``: This variable is the model-instance for the generic
+ content object itself. Mostly you don't need this.
+
+ - ``admin_url``: A URL to the change view of the current object. This variable
+ is ``None`` if the current user has no change permissions for the object.
+
- *as* **"variable name"** (optional): If you provide a variable name, the
- rendered content node is stored in it. Otherwise it's displayed directly.
+ *rendered content node* is stored in it. Otherwise it's displayed directly.
This argument can be a context-variable.
+
+- *into* **"variable_name"** (optional): If you provide a variable name, the
+ *related object* is stored in it. No template rendering is done. The *with*
+ and the *as* arguments are ignored. This argument can be a context-variable.
+
+ After calling the gblock templatetag, you have the same variables available
+ as in the *with* template:
+
+ - ``variable_name``: This variable is the model-instance for the generic block.
+
+ - ``variable_name`` + ``"_genric_object"``: This variable is the model-instance for
+ the generic content object itself. Mostly you don't need this.
+
+ - ``variable_name`` + ``"_admin_url"``: A URL to the change view of the current object.
+ This variable is ``None`` if the current user has no change permissions for
+ the object.
+
+ This is the quickest way to display any model instance or content-node
+ directly without creating a template.
+
+ {% gblock 1 for "auth.User" into "theuser" %}
+ The first user is {{ theuser.username }}! (<a href="{{ theuser_admin_url }}">edit</a>)
+
+ would be rendered as:
+
+ The first user is johndoe! (<a href="/admin/auth/user/1/">edit</a>)
Create your own content node
============================
@@ -105,17 +142,9 @@ content node, if you do not provide a unique template for it (*with*
argument).
In this template are all context-variables from the *parent* template
-available plus some extra variables:
+available plus some extra variables. See arguments/with above for details.
-- ``object``: This variable is the model-instance for the generic block.
-
-- ``generic_object``: This variable is the model-instance for the generic
- content object itself. Mostly you don't need this.
-
-- ``admin_url``: A URL to the change view of the current object. This variable
- is ``None`` if the current user has no change permissions for the object.
-
-A common template source for the above content node would be::
+A common template source for the content node would be::
<h1>{{ object.title }}</h1>
{{ object.content|safe }}
@@ -155,6 +184,11 @@ example::
Changelog
=========
+
+**v0.3.0** (2009-03-21)
+ Added the *into* argument. You can now display any instance directly
+ without creating and rendering a template.
+
**v0.2.1** (2009-03-20)
You can now pass a context variable with a integer to fetch a specific
object.
View
17 django_generic_flatblocks/templatetags/generic_flatblocks.py
@@ -9,11 +9,13 @@
register = Library()
class GenericFlatblockNode(Node):
- def __init__(self, slug, modelname=None, template_path=None, variable_name=None):
+ def __init__(self, slug, modelname=None, template_path=None,
+ variable_name=None, store_in_object=None):
self.slug = slug
self.modelname = modelname
self.template_path = template_path
self.variable_name = variable_name
+ self.store_in_object = store_in_object
def generate_slug(self, slug, context):
"""
@@ -81,11 +83,20 @@ def render(self, context):
# Get the generic and related object
generic_object, related_object = self.get_content_object(related_model, slug)
+ admin_url = self.generate_admin_link(related_object, context)
+
+ # if "into" is provided, store the related object into this variable
+ if self.store_in_object:
+ into_var = self.resolve(self.store_in_object, context)
+ context[into_var] = related_object
+ context["%s_generic_object" % into_var] = generic_object
+ context["%s_admin_url" % into_var] = admin_url
+ return ''
# Add the model instances to the current context
context['generic_object'] = generic_object
context['object'] = related_object
- context['admin_url'] = self.generate_admin_link(related_object, context)
+ context['admin_url'] = admin_url
# Resolve the template(s)
template_paths = []
@@ -111,6 +122,7 @@ def render(self, context):
def do_genericflatblock(parser, token):
"""
{% genericflatblcok "slug" for "appname.modelname" %}
+ {% genericflatblcok "slug" for "appname.modelname" into "slug_object" %}
{% genericflatblcok "slug" for "appname.modelname" with "templatename.html" %}
{% genericflatblcok "slug" for "appname.modelname" with "templatename.html" as "variable" %}
"""
@@ -127,6 +139,7 @@ def next_bit_for(bits, key, if_none=None):
'modelname': next_bit_for(bits, 'for'),
'template_path': next_bit_for(bits, 'with'),
'variable_name': next_bit_for(bits, 'as'),
+ 'store_in_object': next_bit_for(bits, 'into'),
}
return GenericFlatblockNode(**args)
View
2 example_project/templates/current_user.html
@@ -1 +1 @@
-<p>Selected user: {{ object }}</p>
+<p>First user: {{ object }}</p>
View
13 example_project/templates/example.html
@@ -49,6 +49,19 @@
{% with 1 as admin_user %}
{% gblock admin_user for "auth.user" with "current_user.html" %}
{% endwith %}
+
+ {% comment %}
+ You can store the related object directly in the context using
+ the "into" argument. This is the quickest way to display any
+ model. The "for" and "as" arguments are ignored.
+ {% endcomment %}
+ {% with 1 as admin_user %}
+ {% with "the_user_object" as varname %}
+ {% gblock 1 for "auth.user" into varname %}
+ <p>The first user is {{ the_user_object.username }}!</p>
+ {% if the_user_object_admin_url %}<a href="{{ the_user_object_admin_url }}">edit</a>{% endif %}
+ {% endwith %}
+ {% endwith %}
<hr/>
{% comment %}
View
2 setup.py
@@ -2,7 +2,7 @@
setup(
name='django-generic-flatblocks',
- version='0.2.1',
+ version='0.3.0',
description='A flatpages/flatblock application using generic relations to content models.',
long_description=open('README').read(),
author='Martin Mahner',

0 comments on commit dc9e2f9

Please sign in to comment.
Something went wrong with that request. Please try again.