Skip to content

Commit

Permalink
Merge pull request #234 from fbinz/base_dir
Browse files Browse the repository at this point in the history
Don't rely on existence of BASE_DIR settings variable.
  • Loading branch information
wrabit authored Dec 6, 2024
2 parents bd96c00 + 6b876a6 commit b3ee2c6
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 5 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -474,6 +474,10 @@ In addition, Cotton enables you to navigate around some of the limitations with
The directory where your components are stored.
`COTTON_BASE_DIR` (default: None)
The directory that contains your project-level "templates" directory.
`COTTON_SNAKE_CASED_NAMES` (default: True)
Whether to search for component filenames in snake_case. If set to False, you can use kebab-cased / hyphenated filenames.
Expand Down
11 changes: 8 additions & 3 deletions django_cotton/cotton_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,14 @@ def get_dirs(self):
dirs.append(template_dir)

# Check project root templates, e.g. project/templates
root_template_dir = os.path.join(settings.BASE_DIR, "templates")
if os.path.isdir(root_template_dir):
dirs.append(root_template_dir)
base_dir = getattr(settings, "COTTON_BASE_DIR", None)
if base_dir is None:
base_dir = getattr(settings, "BASE_DIR", None)

if base_dir is not None:
root_template_dir = os.path.join(base_dir, "templates")
if os.path.isdir(root_template_dir):
dirs.append(root_template_dir)

return dirs

Expand Down
13 changes: 12 additions & 1 deletion docs/docs_project/docs_project/templates/configuration.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ <h1>Configuration</h1>
</div>
</div>

<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
<div>
<code>COTTON_BASE_DIR</code>
<div>str (default: None)</div>
</div>
<div>
The base directory where - in addition to the app folders - cotton will search for the "templates" directory (see above).
If not set, the `BASE_DIR` generated by `django-admin startproject` is used as a fallback, if it exists.
</div>
</div>

<c-hr />

<div class="grid grid-cols-1 sm:grid-cols-2 gap-6">
Expand Down Expand Up @@ -42,4 +53,4 @@ <h6>As <code>False</code></h6>
</div>


</c-layouts.with-sidebar>
</c-layouts.with-sidebar>
5 changes: 4 additions & 1 deletion docs/docs_project/docs_project/templates/quickstart.html
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ <h2>Templates location</h2>

<c-ul>
<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>
<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>
<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>
(where the <code>[project]</code> depends on the `BASE_DIR` or `COTTON_BASE_DIR` settings)
</li>
</c-ul>

<p>Any style will allow you to include your component the same way: <code>{{ '<c-row />'|force_escape }}</code></p>
Expand Down

0 comments on commit b3ee2c6

Please sign in to comment.