2. Templates & Placeholders

In this tutorial we’ll introduce Placeholders, and we’re also going to show how you can make your own HTML templates CMS-ready.

2.1. Templates

You can use HTML templates to customise the look of your website, define Placeholders to mark sections for managed content and use special tags to generate menus and more.

You can define multiple templates, with different layouts or built-in components, and choose them for each page as required. A page’s template can be switched for another at any time.

You’ll find the site’s templates in tutorial-project/mysite/mysite/templates.

By default, pages in your site will use the fullwidth.html template, the first one listed in the project’s settings.py CMS_TEMPLATES tuple:

  1. CMS_TEMPLATES = (
  2. ## Customize this
  3. ('fullwidth.html', 'Fullwidth'),
  4. ('sidebar_left.html', 'Sidebar Left'),
  5. ('sidebar_right.html', 'Sidebar Right')
  6. )

2.2. Placeholders

Placeholders are an easy way to define sections in an HTML template that will be filled with content from the database when the page is rendered. This content is edited using django CMS’s frontend editing mechanism, using Django template tags.

fullwidth.html contains a single placeholder, {% placeholder "content" %}.

You’ll also see {% load cms_tags %} in that file - cms_tags is the required template tag library.

If you’re not already familiar with Django template tags, you can find out more in the Django documentation.

Add a couple of new placeholders to fullwidth.html, {% placeholder "feature" %} and {% placeholder "splashbox" %} inside the {% block content %} section. For example:

  1. {% block content %}
  2. {% placeholder "feature" %}
  3. {% placeholder "content" %}
  4. {% placeholder "splashbox" %}
  5. {% endblock content %}

If you switch to Structure mode (button in the upper-right corner of the page), you’ll see the new placeholders available for use.

the new 'splashbox' placeholder

2.3. Static Placeholders

The content of the placeholders we’ve encountered so far is different for every page. Sometimes though you’ll want to have a section on your website which should be the same on every single page, such as a footer block.

You could hard-code your footer into the template, but it would be nicer to be able to manage it through the CMS. This is what static placeholders are for.

Static placeholders are an easy way to display the same content on multiple locations on your website. Static placeholders act almost like normal placeholders, except for the fact that once a static placeholder is created and you added content to it, it will be saved globally. Even when you remove the static placeholders from a template, you can reuse them later.

So let’s add a footer to all our pages. Since we want our footer on every single page, we should add it to our base template (mysite/templates/base.html). Place it near the end of the HTML <body> element:

  1. <footer>
  2. {% static_placeholder 'footer' %}
  3. </footer>
  4. {% render_block "js" %}
  5. </body>

Save the template and return to your browser. Refresh any page in Structure mode, and you’ll see the new static placeholder.

a static placeholder

Note

To reduce clutter in the interface, the plugins in static placeholders are hidden by default. Click or tap on the name of the static placeholder to reveal/hide them.

If you add some content to the new static placeholder in the usual way, you’ll see that it appears on your site’s other pages too.

2.4. Rendering Menus

In order to render the CMS’s menu in your template you can use the show_menu tag.

Any template that uses show_menu must load the CMS’s menu_tags library first:

  1. {% load menu_tags %}

The menu we use in mysite/templates/base.html is:

  1. <ul class="nav">
  2. {% show_menu 0 100 100 100 %}
  3. </ul>

The options control the levels of the site hierarchy that are displayed in the menu tree - but you don’t need to worry about exactly what they do at this stage.

Next we’ll look at Integrating applications.