Sorry, we don't support your browser.  Install a modern browser

@auto include CSS/JS for snippets/blocks

I sometimes package blocks into plugins to be able to re-use them more easily. When a block includes a CSS or JS, I would like Kirby to auto include it when the css(‘@auto’) is called. This eway the CSS and JS files for the specific block I packaged into a plugin are only loaded when the block is actually in use.

I’m talking about these helpers:

Example

Plugin index-php

<?php
    // Slider block plugin

    Kirby::plugin('my/plugin', [
        'snippets' => [
            'blocks/block' => __DIR__ . '/snippets/blocks/block.php'
        ],
        'blueprints' => [
            'blocks/block' => __DIR__ . '/blueprints/blocks/block.yml'
        ],
        'js' => [
            'assets/js/block' => __DIR__ . '/assets/js/block.js'
        ]
    ]);
?>

Inside template HTML header

<?php
    css('@auto');
    js('@auto')
?>
2 years ago

Was thinking that this could rather be combined/realized as auto assets for snippets (since blocks are just snippets):

  • Similar fixed patterns for path as with auto asset loading for templates
  • Cms\App::snippet() actually tracks each requested snippet
  • Maybe new css('@snippets') which grabs the tracked list and adds CSS/JS for each used snippet
a year ago
1
a year ago