Ticket #6932: 6932-template-tag-for-flatpages.diff
File 6932-template-tag-for-flatpages.diff, 8.1 KB (added by , 16 years ago) |
---|
-
django/contrib/flatpages/templatetags/flatpages.py
1 2 from django import template 3 from django.contrib.flatpages.models import FlatPage 4 from django.utils.translation import ugettext as _ 5 from django.conf import settings 6 7 register = template.Library() 8 9 class FlatpageNode(template.Node): 10 def __init__(self, context_name, starts_with=None): 11 self.context_name, self.starts_with = context_name, starts_with 12 if self.starts_with: 13 self.starts_with = template.Variable(starts_with) 14 self.flatpages = FlatPage.objects.filter(sites__id=settings.SITE_ID) 15 16 def render(self, context): 17 if self.starts_with: 18 self.flatpages = self.flatpages.filter(\ 19 url__startswith=self.starts_with.resolve(context)) 20 context[self.context_name] = self.flatpages 21 return '' 22 23 24 def get_flatpages(parser, token): 25 """ 26 Template tag that returns flatpages availible on the site. 27 An optional argument, 'starts_with', can be applied only return pages 28 whose urls start with that base url. This can be a variable or a string, 29 it resolves from context. 30 31 Syntax is {% get_flatpages ['url_starts_with'] as context_name %} 32 example: 33 {% get_flatpages '/about/' as about_pages %} 34 {% for page in about_pages %}...{% endfor %} 35 """ 36 bits = token.split_contents() 37 if len(bits) is 3: 38 if bits[1] != 'as': 39 raise template.TemplateSyntaxError, _("%s expects a syntax of %s " 40 "['url_starts_with'] as context_name" % (bits[0], bits[0])) 41 return FlatpageNode(bits[2]) 42 elif len(bits) is 4: 43 if bits[2] != 'as': 44 raise template.TemplateSyntaxError, _("%s expects a syntax of %s " 45 "['url_starts_with'] as context_name" % (bits[0], bits[0])) 46 return FlatpageNode(bits[3], bits[1]) 47 else: 48 raise template.TemplateSyntaxError, _("%s expects a syntax of %s " 49 "['url_starts_with'] as context_name" % (bits[0], bits[0])) 50 51 52 register.tag('get_flatpages', get_flatpages) 53 54 55 -
tests/regressiontests/flatpages/views.py
1 # Create your views here. -
tests/regressiontests/flatpages/tests.py
1 from django.test import TestCase 2 from django.contrib.flatpages.models import FlatPage 3 from django.contrib.sites.models import Site 4 5 current_site = Site.objects.get_current() 6 7 8 class FlatpageTest(TestCase): 9 """Test for contrib.flatpages""" 10 # Site.objects.clear_cache() 11 12 13 def setUp(self): 14 15 page = FlatPage( 16 url='/page1/', 17 title='Some page', 18 content="<p>Some text that shouldn't be automatcially escaped</p>", 19 registration_required=False, 20 ) 21 page.save() 22 page.sites.add(current_site) 23 page.save() 24 25 registrationPage = FlatPage( 26 url='/registered/', 27 title='register for this page', 28 content="great you are registered", 29 registration_required=True, 30 ) 31 registrationPage.save() 32 registrationPage.sites.add(current_site) 33 registrationPage.save() 34 35 fallbackPage = FlatPage( 36 url='/accounts/login/', 37 title='This page should not be seen', 38 content=" ", 39 registration_required=True, 40 ) 41 fallbackPage.save() 42 fallbackPage.sites.add(current_site) 43 fallbackPage.save() 44 45 def testPage(self): 46 page = self.client.get('/page1/') 47 self.failUnlessEqual(page.status_code, 200) 48 self.assertContains(page, "<p>Some text that shouldn't be automatcially escaped</p>") 49 50 def testRegistration(self): 51 registrationPage = self.client.get('/registered/') 52 self.assertRedirects(registrationPage, '/accounts/login/?next=/registered/', target_status_code=302) 53 54 def testFallback(self): 55 fallbackPage = self.client.get('/accounts/login/') 56 self.assertNotContains(fallbackPage, 'This page should not be seen', status_code=302) 57 58 def testTag(self): 59 from django import template 60 # because of the way flatpages are loaded in the tests this is the 61 # equivilent of {% load flatpages %} 62 template.add_to_builtins('django.contrib.flatpages.templatetags.flatpages') 63 t = template.Template("""{% get_flatpages as flatpages %} 64 {% for page in flatpages %} 65 {{ page.title }} 66 {% endfor %} 67 68 {% get_flatpages '/registered/' as registered_pages %} 69 {% for page in registered_pages %} 70 {{ page.title }} 71 {% endfor %} 72 73 {% get_flatpages some_var as registered_pages %} 74 {% for page in registered_pages %} 75 {{ page.title }} 76 {% endfor %}""") 77 r = t.render(template.Context({'some_var':'/accounts/'})) 78 self.assertEquals(r, 79 """\n\nThis page should not be seen\n\nSome page\n\nregister""" +\ 80 """ for this page\n\n\n\n\nregister for this page\n\n\n\n\n""" +\ 81 """This page should not be seen\n""") 82 -
tests/regressiontests/flatpages/models.py
+
1 # Create your models here. -
tests/regressiontests/flatpages/templates/flatpages/default.html
1 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" 2 "http://www.w3.org/TR/REC-html40/loose.dtd"> 3 <html> 4 <head> 5 <title>{{ flatpage.title }}</title> 6 </head> 7 <body> 8 {{ flatpage.content }} 9 </body> 10 </html> -
docs/flatpages.txt
134 134 135 135 .. _automatic HTML escaping: ../templates/#automatic-html-escaping 136 136 137 138 Getting a list of Flatpages in your Templates 139 ============================================= 140 141 **New in Django development version** 142 143 The Flatpages contrib app also comes with a template tag. From your templates 144 you can access this tag via {% load flatpages %} and you get access to a 145 get_flatpages tag that returns flatpages availible on the site. 146 An optional argument, 'starts_with', can be applied only return pages 147 whose urls start with that base url. This can be a variable or a string, 148 it resolves from context. 149 150 Syntax is {% get_flatpages ['url_starts_with'] as context_name %} 151 example: 152 153 {% get_flatpages '/about/' as about_pages %} 154 <ul> 155 {% for page in about_pages %} 156 <li><a href="{{ page.url }}">{{ page.title }}</a></li> 157 {% endfor %} 158 </ul> 159 160 161 If you wish to filter flatpages out by registration required, you can place an 162 if statment around your for loop: 163 164 {% get_flatpages '/contact/' as contact_pages %} 165 {% for page in contact_pages %} 166 {% if not user.is_authenticated and page.registration_required %} 167 <h3><a href="{{page.url}}">{{page.title}}</a></h3> 168 {% endif %} 169 {% endfor %} 170 171