Skip to main content

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

Project details


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 hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page