Skip to content

Commit 084c516

Browse files
authored
Merge pull request #225 from wrabit/test_app_subdirectories
Allow `templates` in project root
2 parents 94b56d9 + 149578a commit 084c516

File tree

6 files changed

+47
-5
lines changed

6 files changed

+47
-5
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
button!
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
i'm sub in project root
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
I'm from project roo templates!

django_cotton/cotton_loader.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import os
33
from functools import lru_cache
44

5+
from django.conf import settings
56
from django.template.loaders.base import Loader as BaseLoader
67
from django.core.exceptions import SuspiciousFileOperation
78
from django.template import TemplateDoesNotExist, Origin
@@ -50,14 +51,20 @@ def _get_template_string(self, template_name):
5051

5152
@lru_cache(maxsize=None)
5253
def get_dirs(self):
53-
"""This works like the file loader with APP_DIRS = True."""
54+
"""Retrieves possible locations of cotton directory"""
5455
dirs = list(self.dirs or self.engine.dirs)
5556

57+
# Include any included installed app directories, e.g. project/app1/templates
5658
for app_config in apps.get_app_configs():
5759
template_dir = os.path.join(app_config.path, "templates")
5860
if os.path.isdir(template_dir):
5961
dirs.append(template_dir)
6062

63+
# Check project root templates, e.g. project/templates
64+
root_template_dir = os.path.join(settings.BASE_DIR, "templates")
65+
if os.path.isdir(root_template_dir):
66+
dirs.append(root_template_dir)
67+
6168
return dirs
6269

6370
def reset(self):

django_cotton/tests/test_basic.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,21 @@ def test_hyphen_naming_convention(self):
203203
rendered = get_rendered(html)
204204

205205
self.assertTrue("I have a hyphenated component name" in rendered)
206+
207+
def test_multiple_app_subdirectory_access(self):
208+
self.create_template(
209+
"cotton/app_dir.html",
210+
"I'm from app templates!",
211+
)
212+
213+
html = """
214+
<c-app-dir />
215+
<c-project-root />
216+
<c-app2.sub />
217+
"""
218+
219+
rendered = get_rendered(html)
220+
221+
self.assertTrue("I'm from app templates!" in rendered)
222+
self.assertTrue("I'm from project roo templates!" in rendered)
223+
self.assertTrue("i'm sub in project root" in rendered)

docs/docs_project/docs_project/templates/quickstart.html

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
<c-slot name="page_index">
33
<c-index-link><a href="#install" class="no-underline">Install Cotton</a></c-index-link>
44
<c-index-link><a href="#create-a-component" class="no-underline">Create a component</a></c-index-link>
5+
<c-index-link><a href="#templates-location" class="no-underline">Templates location</a></c-index-link>
56
<c-index-link><a href="#include-a-component" class="no-underline">Include a component</a></c-index-link>
67
<c-index-link><a href="#usage" class="no-underline">Usage</a></c-index-link>
78
<c-index-sublink><a href="#basics" class="no-underline text-opacity-70">Basics</a></c-index-sublink>
@@ -18,17 +19,17 @@ <h2>Install cotton</h2>
1819

1920
<p>Then update your settings.py:</p>
2021

21-
<h3>Automatic configuration:</h3>
22+
<h3>For automatic configuration:</h3>
2223

2324
<c-snippet language="python" label="settings.py">{% cotton_verbatim %}{% verbatim %}
2425
INSTALLED_APPS = [
2526
'django_cotton',
2627
]
2728
{% endverbatim %}{% endcotton_verbatim %}</c-snippet>
2829

29-
<p>This will automatically handle the settings.py adding the required loader and templatetags.</p>
30+
<p>This will attempt to automatically handle the settings.py by adding the required loader and templatetags.</p>
3031

31-
<h3>Customised configuration</h3>
32+
<h3>For custom configuration</h3>
3233

3334
<p>If your project requires any non-default loaders or you do not wish Cotton to manage your settings, you should instead provide `django_cotton.apps.SimpleAppConfig` in your INSTALLED_APPS:</p>
3435

@@ -72,6 +73,18 @@ <h2>{{ title }}</h2>
7273
</div>
7374
{% endverbatim %}{% endcotton_verbatim %}</c-snippet>
7475

76+
<c-hr id="templates-location" />
77+
78+
<h2>Templates location</h2>
79+
80+
<p>Cotton supports 2 common approaches regarding the location of the <code>templates</code> directory:</p>
81+
82+
<c-ul>
83+
<li><strong>App level</strong> - You can place your cotton folder in any of your installed app folders, like: <div><code>[project]/[app]/templates/cotton/row.html</code></div></li>
84+
<li><strong>Project root</strong> - You can place your cotton folder in a project level templates directory, like: <div><code>[project]/templates/cotton/row.html</code></div></li>
85+
</c-ul>
86+
87+
<p>Any style will allow you to include your component the same way: <code>{{ '<c-row />'|force_escape }}</code></p>
7588

7689
<c-hr id="include-a-component" />
7790

@@ -111,7 +124,8 @@ <h2 id="usage">Usage</h2>
111124

112125
<h3 id="basics">Basics</h3>
113126
<c-ul>
114-
<li>Cotton components should be placed in the <c-highlight>templates/cotton</c-highlight> folder (unless you have set COTTON_DIR).</li>
127+
<li>Cotton components should be placed in the <c-highlight>templates/cotton</c-highlight> folder ('cotton' directory is <a href="{% url 'configuration' %}">configurable</a> using COTTON_DIR).</li>
128+
<li>The <code>templates</code> folder can be located in either an app-level or top-level project root folder.</li>
115129
</c-ul>
116130

117131
<h3 id="naming">Naming</h3>

0 commit comments

Comments
 (0)