Skip to content

Commit

Permalink
Add support for site-enabled CMS_TEMPLATES_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
yakky committed May 7, 2014
1 parent a823ec8 commit 5f55b62
Show file tree
Hide file tree
Showing 8 changed files with 184 additions and 3 deletions.
@@ -0,0 +1,82 @@
{% load i18n cms_tags menu_tags sekizai_tags %}
<!doctype html><html lang="{{ LANGUAGE_CODE }}">
<head>
<meta charset="utf-8" />
<title>{% page_attribute page_title %}</title>
<meta name="description" content="{% page_attribute meta_description %}" />
<link href="{{ STATIC_URL }}css/style.css" media="all" rel="stylesheet" type="text/css" />
{% render_block "css" %}

<style type="text/css">
body { font:normal 16px/18px "Times New Roman", Times, serif; margin:0; background:#fcfcfc; }

#header { padding:15px 10px 15px; box-shadow:#eee 0px 2px 5px; background:#fff;
clear:both; overflow:hidden; }
#header #logo { float:left; background:#eee; border-radius:10px; }
#header #logo a { color:#999; display:block; text-align:center; padding:10px 25px; text-decoration:none; }
#header #nav_lang,
#header #nav_main { float:left; padding-left:15px; }
#header #nav_lang ul,
#header #nav_main ul { list-style-type:none; padding:0; margin:0; }
#header #nav_lang ul li,
#header #nav_main ul li { float:left; padding:0; margin:0; }
#header #nav_lang ul li a,
#header #nav_main ul li a { float:left; padding:10px 5px; }

#main, .breadcrumb { width:960px; margin:0 auto; }

.breadcrumb { padding:15px 0; list-style-type:none; }
.breadcrumb li { padding-right:10px; }

.grid_1, .grid_2, .grid_3, .grid_4, .grid_5, .grid_6,
.grid_7, .grid_8, .grid_9, .grid_10, .grid_11, .grid_12 { float:left; margin:0 10px; }

.grid_1 { width:60px; }
.grid_2 { width:140px; }
.grid_3 { width:220px; }
.grid_4 { width:300px; }
.grid_5 { width:380px; }
.grid_6 { width:460px; }
.grid_7 { width:540px; }
.grid_8 { width:620px; }
.grid_9 { width:700px; }
.grid_10 { width:780px; }
.grid_11 { width:860px; }
.grid_12 { width:940px; }

p { padding:0; margin:0 0 15px; }
img { max-width:100%; }
</style>
</head>
<body id="{% block tpl_id %}none{% endblock %}" class="{{ LANGUAGE_CODE }}">
{% cms_toolbar %}
<div id="page" class="container_16 ">
<div id="header">
<div id="logo">
<a href="/">Company name</a>
</div>
<div id="nav_lang">
{% spaceless %}
<ul>
{% language_chooser %}
</ul>
{% endspaceless %}
</div>
<div id="nav_main" class="clearfix">
{% spaceless %}
<ul>
{% show_menu 0 0 100 100 %}
</ul>
{% endspaceless %}
</div>
</div>
<ul class="breadcrumb clearfix">
{% show_breadcrumb %}
</ul>
<div id="main">
{% block content %}{% endblock content %}
</div>
</div>
{% render_block "js" %}
</body>
</html>
@@ -0,0 +1,32 @@
{% extends "base.html" %}
{% load i18n cms_tags %}

{% block tpl_id %}tpl_col_three{% endblock %}

{% block content %}
<h1>{% render_model request.current_page "page_title" %}</h1>
<div id="col1" class="grid_3">
{% include "sidebar_submenu.html" %}
{% block col_sidebar %}
{% with "220" as width %}
{% placeholder col_sidebar %}
{% endwith %}
{% endblock %}
</div>

<div id="col2" class="grid_6">
{% block col_left %}
{% with "340" as width %}
{% placeholder col_left %}
{% endwith %}
{% endblock %}
</div>

<div id="col3" class="grid_3">
{% block col_right %}
{% with "220" as width %}
{% placeholder col_right %}
{% endwith %}
{% endblock %}
</div>
{% endblock %}
@@ -0,0 +1,29 @@
{% extends "base.html" %}
{% load i18n cms_tags %}

{% block tpl_id %}tpl_col_two{% endblock %}

{% block content %}

<h1>{% render_model request.current_page "titles" %} {% render_model_icon request.current_page %}</h1>
<div id="col1" class="grid_3">
{% render_model request.current_page "menu_title" %}
{% include "sidebar_submenu.html" %}
{% block col_sidebar %}
{% with "220" as width %}
{% placeholder col_sidebar %}
{% endwith %}
{% endblock %}
</div>

<div id="col2" class="grid_9">
{% render_model request.current_page "page_title" %}
{% block col_left %}
{% with "640" as width %}
{% placeholder col_left %}
{% endwith %}
{% endblock %}
</div>

<footer>{% render_model request.current_page "title" %}</footer>
{% endblock %}
@@ -0,0 +1,3 @@
[templates]
right_col_two.html=Two columns
right_col_three.html=Three columns
1 change: 0 additions & 1 deletion cms/tests/page.py
Expand Up @@ -310,7 +310,6 @@ def test_meta_description_from_template_tags(self):
req.REQUEST = {}
self.assertEqual(t.render(template.Context({"request": req})), "Hello I am a page")


def test_page_obj_change_data_from_template_tags(self):
from django import template

Expand Down
23 changes: 23 additions & 0 deletions cms/tests/templates.py
@@ -1,5 +1,7 @@
# -*- coding: utf-8 -*-
import os.path
from cms.test_utils.util.context_managers import SettingsOverride

try:
from configparser import ConfigParser
except ImportError:
Expand All @@ -18,6 +20,11 @@
PATH_PREFIX = os.path.join('inner_dir', 'custom_templates')
GOOD_PATH = os.path.join(settings.PROJECT_PATH, 'project', 'templates', PATH_PREFIX)
BAD_PATH = os.path.join(settings.PROJECT_PATH, 'project', 'custom_templates')
SITE_PATH = {
1: os.path.join(settings.PROJECT_PATH, 'project', 'templates', PATH_PREFIX),
2: os.path.join(settings.PROJECT_PATH, 'project', 'templates', '%s_2' % PATH_PREFIX),
}


class TemplatesConfig(CMSTestCase):

Expand Down Expand Up @@ -65,6 +72,22 @@ def test_custom_templates_loading(self):
tpl = loader.get_template(template[0])
self.assertTrue(tpl.name in files)

@override_settings(CMS_TEMPLATES_DIR=SITE_PATH)
def test_multisite(self):
"""
Checking that templates can be loaded by the template loader
"""
templates = get_cms_setting('TEMPLATES')
for template in templates:
if template[0] != constants.TEMPLATE_INHERITANCE_MAGIC:
self.assertTrue(template[0].find('%s/' % SITE_PATH[1]) >= -1)
with SettingsOverride(SITE_ID=2):
templates = get_cms_setting('TEMPLATES')
for template in templates:
if template[0] != constants.TEMPLATE_INHERITANCE_MAGIC:
self.assertTrue(template[0].find('%s/' % SITE_PATH[2]) >= -1)


@override_settings(CMS_TEMPLATES_DIR=BAD_PATH)
def test_custom_templates_bad_dir(self):
"""
Expand Down
4 changes: 4 additions & 0 deletions cms/utils/conf.py
Expand Up @@ -87,6 +87,10 @@ def get_media_url():
def get_templates():
if getattr(settings, 'CMS_TEMPLATES_DIR', False):
tpldir = getattr(settings, 'CMS_TEMPLATES_DIR', False)
# CMS_TEMPLATES_DIR can either be a string poiting to the templates directory
# or a dictionary holding 'site: template dir' entries
if isinstance(tpldir, dict):
tpldir = tpldir[settings.SITE_ID]
# We must extract the relative path of CMS_TEMPLATES_DIR to the neares
# valid templates directory. Here we mimick what the filesystem and
# app_directories template loaders do
Expand Down
13 changes: 11 additions & 2 deletions docs/getting_started/resources/configuration.rst
Expand Up @@ -87,9 +87,18 @@ CMS_TEMPLATES_DIR
Default: ``None``

Instead of explicitly providing a set of templates via :setting:`CMS_TEMPLATES`
a single directory can be provided using this configuration.
a directory can be provided using this configuration.

The directory is scanned and all templates in it are loaded as templates for
`CMS_TEMPLATES_DIR` can be set to the (absolute) path of the templates directory,
or set to a dictionary with `SITE_ID: template path` items::

CMS_TEMPLATES_DIR: {
1: '/absolute/path/for/site/1/',
2: '/absolute/path/for/site/2/',
}


The provided directory is scanned and all templates in it are loaded as templates for
django CMS.

Template loaded and their names can be customized using a INI-file called
Expand Down

0 comments on commit 5f55b62

Please sign in to comment.