Skip to main content

This package allows you to define and organise static assets for your Wagtail blocks frontend.

Project description

Wagtail CRX block frontend assets rendering

PyPI version Python versions Django tested Wagtail tested CodeRed CMS tested License

Define and organize frontend assets (like js or css files) for your Wagtail CRX blocks.

Supported versions

  • See the badges above for tested versions.
  • "Locked" and "floating" dependency sets are tested.
  • Locked sets pin Django + Wagtail + CodeRed via constraints/*.txt.
  • Floating sets pin Wagtail + CodeRed only, letting Django float within their ranges.
  • To support a new version, add a constraint file in constraints/ and a matching tox/CI entry.

Getting started

  1. Add the app to INSTALLED_APPS:

    INSTALLED_APPS = [
     ...
     "wagtail_crx_block_frontend_assets",
     ...
     ]
    
  2. Integrate your blocks with this app.

     from wagtail.blocks import CharBlock, StructBlock
     from wagtail_crx_block_frontend_assets.blocks import BlockStaticAssetsRegistrationMixin
    
     class FrontendAssetsBlock(BlockStaticAssetsRegistrationMixin, StructBlock):
    
         title = CharBlock(
             required=False,
             label="Title",
         )
    
         def register_assets(self, block_value):
             static_assets = []
    
             static_assets += [
                 self.StaticAsset("path/to/asset.js", target="_blank"),
                 self.StaticAsset("path/to/style.css", media="print"),
    
             ]
    
             return static_assets
    

    Your block class has to inherit from BlockStaticAssetsRegistrationMixin and you have to implement register_assets function. This function returns array of BlockStaticAssetsRegistrationMixin.StaticAsset instances. You can use block_value parameter to conditionally render assets based on current block values.

  3. Then you can define place in your templates where you want your block assets to be rendered like this:

    {% extends "coderedcms/pages/base.html" %}
    {% load block_assets_tags %}
    
    {% block custom_assets %}
    {{ block.super }}
    {% render_block_assets required_file_extension=".css" %}
    {% endblock custom_assets %}
    
    {% block custom_scripts %}
    {{ block.super }}
    {% render_block_assets required_file_extension=".js" %}
    {% endblock custom_scripts %}
    

Development

  1. Make sure you have Python virtual env installed
    $ python -m venv .venv
    
  2. Install this app in editable mode
    $ pip install -e .
    
  3. Run tests (single environment)
    $ pip install -e .[test]
    $ pytest
    
  4. Run the compatibility matrix
    $ tox
    
  5. Migrate testapp DB
    $ python manage.py migrate
    
  6. Run the testapp
    $ python manage.py runserver
    
    Or hit F5 if you use Visual Studio Code

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

wagtail_crx_block_frontend_assets-1.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file wagtail_crx_block_frontend_assets-1.1.0.tar.gz.

File metadata

File hashes

Hashes for wagtail_crx_block_frontend_assets-1.1.0.tar.gz
Algorithm Hash digest
SHA256 36959f165f0bb0e63273d87ccf58d48ed1157b02ba04b4862c667075250924d8
MD5 ff5bde573c3b58af8e086b7def62332a
BLAKE2b-256 11b62f60ab3e2ef13622cc3f880f3eb87d1dd51a88c40c9b15cb40ff166acc87

See more details on using hashes here.

File details

Details for the file wagtail_crx_block_frontend_assets-1.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for wagtail_crx_block_frontend_assets-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 25c08134857bc9c1c0a8791d95f7b91b44929540d8cc46dc3f2e27c0efbd0290
MD5 16dd4b8deaf75bc4c04f5f60f10fd9a3
BLAKE2b-256 d0a34e039c31b100a5fc5525646b0e079d34b79e7fe3f7b1060b4f40c3e20ba3

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