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

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

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" %}
    
    {% block custom_assets %}
    {{ block.super }}
    {% include "wagtail_crx_block_frontend_assets/includes/block_assets.html" with required_file_extension=".css" %}
    {% endblock custom_assets %}
    
    {% block custom_scripts %}
    {{ block.super }}
    {% include "wagtail_crx_block_frontend_assets/includes/block_assets.html" with 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. Migrate testapp DB
    $ python manage.py migrate
    
  4. 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

Built Distribution

File details

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

File metadata

File hashes

Hashes for wagtail_crx_block_frontend_assets-0.0.2.tar.gz
Algorithm Hash digest
SHA256 76ea485d2794041f22683ddf7653f70d7bc744b45eacb03b7db0fd069b431b28
MD5 5b2d1d68ddec2e53cd20bacb9e86dfc4
BLAKE2b-256 1fa609d35eca190fec3e5bd0fdb383186cb196ebb1101358fcc69dd1780c4d77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for wagtail_crx_block_frontend_assets-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 57ab59bad99966927a4f1737b05175875e253d0d52affccd42e60fc89e5613fb
MD5 d102f8ac4bd5392729c78f839b2261bf
BLAKE2b-256 e29aafa5872e85a8fff58a5d81743cee2c658894522d12527f6319ae7868b156

See more details on using hashes here.

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