===================================
Display page's content in templates
===================================
Django Basic CMS provide several template tags to extract data from the CMS.
To use these tags in your templates you must load them first::
{% load pages_tags %}
.. contents::
:local:
:depth: 2
get_content
-----------
Store a content type from a page into a context variable that you can reuse after::
{% get_content current_page "title" as content %}
You can also use the slug of a page::
{% get_content "my-page-slug" "title" as content %}
You can also use the id of a page::
{% get_content 10 "title" as content %}
.. note::
You can use either the page object, the slug, or the id of the page.
show_content
------------
Output the content of a page directly within the template::
{% show_content current_page "title" %}
.. note::
You can use either the page object, the slug, or the id of the page.
get_page
------------
Retrieve a Page object and store it into a context variable that you can reuse after. Here is
an example of the use of this template tag to display a list of news::
Latest news
{% get_page "news" as news_page %}
{% for new in news_page.get_children %}
-
{{ new.title }}
{{ new.publication_date }}
{% show_content new body %}
{% endfor %}
.. note::
You can use either the slug, or the id of the page.
get_pages_with_tag
------------
Retrieve a Pages objects with given tag and store it into a context variable that you can reuse after. Here is
an example of the use of this template tag to display a list of footer pages::
Footer
{% get_pages_with_tag "footer" as footer_pages %}
show_absolute_url
-----------------
This tag show the absolute url of a page. The difference with the `Page.get_url_path` method is
that the template knows which language is used within the context and display the URL accordingly::
{% show_absolute_url current_page %}
.. note::
You can use either the page object, the slug, or the id of the page.
Display content from other applications
----------------------------------------
There is several ways to change the way the default view provided
by the CMS render the pages. This list try explain the most common.
Using the PAGE_EXTRA_CONTEXT setting
======================================
Considering you have a simple news model::
class News(models.Model):
title = models.CharField(max_length=200)
postdate = models.DateTimeField(default=datetime.now)
body = models.CharField(max_length=200)
And that you would like to display a list of news into some of your page's templates::
{% for new in news %}
-
{{ news.title }}
{{ news.body }}
{% endfor %}
Then you might want to use the `PAGE_EXTRA_CONTEXT` setting. You should set this setting to be a function.
This function should return a Python dictionary. This dictionary will be merged with the context of
every page of your website.
Example in the case of the news::
def extra_context():
from news.models import News
lastest_news = News.object.all()
return {'news': lastest_news}
PAGE_EXTRA_CONTEXT = extra_context
Delegate the page rendering to another application
===================================================
:doc:`You can set another application to render certain pages of your website 3rd-party-apps>`.
Subclass the default view
===================================================
New in 1.3.0: The default view is now a real class. That will
help if you want to override some default behavior::
from basic_cms.views import Details
from news.models import News
class NewsView(Details):
def extra_context(self, request, context):
lastest_news = News.object.all()
return {'news': lastest_news}
details = NewsView()
And don't forget to redefine the urls to point to your new view with something similar to this code::
from django.conf.urls.defaults import url, include, patterns
from YOUR_APP.views import details
from basic_cms import page_settings
if page_settings.PAGE_USE_LANGUAGE_PREFIX:
urlpatterns = patterns('',
url(r'^(?P[-\w]+)/(?P.*)$', details,
name='pages-details-by-path')
)
else:
urlpatterns = patterns('',
url(r'^(?P.*)$', details, name='pages-details-by-path')
)