Skip to main content

HTML-style includes for Plain.

Project description

plain.elements

Use HTML tags to include HTML template components.

Elements are an alternative to Jinja {% include %} or macros and flow better with existing HTML by using a compatible syntax. They are distinguished from built-in HTML tags by using a capitalized tag name (so <Button> doesn't clash with <button>).

To make a <Submit> element, for example, you would create a template named templates/elements/Submit.html.

<!-- templates/elements/Submit.html -->
<button type="submit" class="btn">
    {{ children }}
</button>

An element can be used in any other template by enabling them with {% use_elements %} and then using the caplitalized tag name.

{% extends "admin/base.html" %}

{% use_elements %}

{% block content %}
<form method="post">
    {{ csrf_input }}
    <!-- Form fields here -->
    <Submit>Save</Submit>
</form>
{% endblock %}

Installation

Install plain.elements from PyPI and add it to your INSTALLED_PACKAGES setting. That's it! The Jinja extension will be enabled automatically.

# settings.py
INSTALLED_PACKAGES = [
    # ...
    "plain.elements",
]

Element attributes

Attributes will be passed through using regular strings or single braces {} for Python variables.

{% extends "admin/base.html" %}

{% use_elements %}

{% block content %}
<form method="post">
    {{ csrf_input }}
    <FormInput field={form.username} placeholder="Username" label="Username" />
    <Submit>Save</Submit>
</form>
{% endblock %}

The attributes are passed to the element as named variables. By default in Plain, you will get an error if you try to use an undefined variable, so Jinja features like |default and is defined are useful for optional attributes.

<!-- templates/elements/FormInput.html -->
<label for="{{ field.html_id }}">
    {{ label }}
</label>
<input
    id="{{ field.html_id }}"
    type="{{ type|default('text') }}"
    name="{{ field.html_name }}"
    value="{{ field.value() or '' }}"
    placeholder="{{ placeholder }}"
    {% if field.field.required %}required{% endif %}
    />

Namespaced elements

Especially for reusable packages, it can be useful to namespace your elements by putting them in a subdirectory of templates/elements/. To use namespaced elements, you need to include the full dot-separated path in your HTML tag.

For example, an element in templates/elements/admin/Submit.html would be used like this:

{% use_elements %}

{% block content %}
<form method="post">
    {{ csrf_input }}
    <admin.Submit>Save</admin.Submit>
</form>
{% endblock %}

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

plain_elements-0.7.0.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

plain_elements-0.7.0-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file plain_elements-0.7.0.tar.gz.

File metadata

  • Download URL: plain_elements-0.7.0.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.7.2

File hashes

Hashes for plain_elements-0.7.0.tar.gz
Algorithm Hash digest
SHA256 bca8a9d38170acd1e8acb572bfd5e4266276805321e7f400ee27942253a6d502
MD5 78e4c64b5b5ea2c276401ebefe52b9dc
BLAKE2b-256 1283d687fa2a5c9ac783ad1f04ff949d3dd3b3b2a57b480dbeff29ea6bf8f726

See more details on using hashes here.

File details

Details for the file plain_elements-0.7.0-py3-none-any.whl.

File metadata

File hashes

Hashes for plain_elements-0.7.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2879cd8103cf2a2eaf91f486ce52020367d8d479a4535aff32a82171dfa2d09
MD5 6f48aefedc4be3ff2fd8ed9cce1a19ca
BLAKE2b-256 bb43d184ae87bbf3c1e2414e441cd8787232eceea5a7cb20a6ecd3aafcd34126

See more details on using hashes here.

Supported by

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