Write templates in Django with Dominate
Project description
Subjugate
Write your Django templates with Python!
- All built-in Django loaders work.
- All built-in filters work.
- Caching and autoreloading works.
The missing companion library for Django to use dominate to write templates.
Installation
pip install subjugate
# Subjugate doesn't actually depend on Domainate but it's highly highly recommended.
pip install domainate
INSTALLED_APPS = [
...,
"subjugate",
...
]
TEMPLATES = [
...,
{
"BACKEND": "subjugate.template.backends.subjugate.SubjugateTemplates",
"DIRS": [],
"APP_DIRS": True,
"OPTIONS": {"debug": False},
}
]
Usage
Assuming you're using APP_DIRS
create a file yourapp/subjugate/yourapp/page.py
.
from subjugate import SubjugateTemplate
from dominate import tags as t
class PageTemplate(SubjugateTemplate):
def render(self, title="Page", description="Cool stuff here", **kwargs):
document = dominate.document(title=title)
abs_url = self.request.build_absolute_uri()
with document.head:
t.meta(name="charset", content="UTF-8")
t.meta(name="viewport", content="width=device-width, initial-scale=1")
t.title(title)
t.script(
"""
document.documentElement.classList.remove('no-js');\
document.documentElement.classList.add('js');
""",
type="module",
)
t.base(href=f"{abs_url}")
t.link(rel="canonical", href=f"{abs_url}")
t.link(rel="author", href=f"{self.static('humans.txt')}")
t.link(rel="license", href=f"{self.url('copyright')}")
t.meta(name="description", cotent=description)
t.meta(property="og:title", content=title)
t.meta(property="og:locale", content="en_US")
t.meta(property="og:type", content="website")
t.meta(property="og:url", content=f"{abs_url}")
with document:
t.p("Hello World!")
return document
Then in urls.py
urlpatterns = [
...,
path("page/", TemplateView.as_view(template_name="yourapp/page.py")),
]
Template Reuse
class BaseTemplate(SubjugateTemplate):
def render(self, title="Blah", **kwargs):
document = dominate.document(title=title)
with document.head:
t.title(title)
return document
class DerivedTemplate(SubjugateTemplate):
def render(self, **kwargs):
base = self.extend("yourapp/base.py", title="Title Works")
with base:
t.p("Hello from the derived template!")
return base
Other Features
class TemplateVars(SubjugateTemplate):
def render(self, **kwargs):
base = self.extend("yourapp/base.py", title="Title Works")
with base:
# Context Vars
t.p(self.vars.message)
# Page URLs
t.p(self.url('yourapp/copyright.py'))
# Static URLs
t.p(self.static('yourapp/humans.txt'))
# CSRF Tokens
t.p(self.csrf_token())
# Filters
t.p(self.filter("upper", "yes"))
# Lorem Ipsum
t.p(self.lorem_words(count=15))
t.p(self.lorem_paragraphs(count=3))
return base
But I don't want to use Domainate
Cool cool, like I said this library doesn't actually require Domainate. Just
make sure that the thing your render
function returns has a __str__
method
that actually outputs the renderd HTML.
Authors
- Estelle Poulin (dev@inspiredby.es)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
subjugate-1.0.0.tar.gz
(9.9 kB
view details)
File details
Details for the file subjugate-1.0.0.tar.gz
.
File metadata
- Download URL: subjugate-1.0.0.tar.gz
- Upload date:
- Size: 9.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d767f2e58c4e74c1b9a9ffe002dcd9f68a527722db6442c0b08ce542945f6e5b |
|
MD5 | 98253650929b0fea53a6aeab9c3429e4 |
|
BLAKE2b-256 | 0e4e09801cf3b14a11d5ce6e4990d79ec1dbcab799d17cb983c2d3cd3860c052 |