lzb_pro/block_script/template
Points a block at a view script the theme does not carry, which is what a plugin shipping its own template blocks needs.
A block whose Output Method is Theme Template picks up an optional blocks/BLOCK-SLUG/view.js next to its template. find_script_template() resolves that name with locate_template(), fires this filter, and registers the file as the block's view_script when file_exists() passes, so WordPress loads it only on pages where the block is present. Blocks using any other output method never reach it, because their JavaScript comes from the block builder's own script field.
Attributes
| Name | Type | Description |
|---|---|---|
$template | String | path found by locate_template(), empty when the theme has no such file |
$template_name | String | name being looked up, blocks/BLOCK-SLUG/view.js |
$block | Array | block data, the same array lzb/block_data returns |
$context | String | always view, the only script a block template has |
Additional Filters
| Name | Arguments | Description |
|---|---|---|
lazyblock/BLOCK_SLUG/view_script_template | $template, $template_name, $block | one block, positional variant |
lazyblock/BLOCK_SLUG/script_template | $template, $template_name, $block, $context | one block |
Usage
function my_lzb_pro_block_script_template( $template, $template_name, $block, $context ) {
// Serve the minified build in production and the readable file while
// SCRIPT_DEBUG is on.
if ( ! $template || ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ) {
return $template;
}
$minified = preg_replace( '/\.js$/', '.min.js', $template );
return file_exists( $minified ) ? $minified : $template;
}
add_filter( 'lzb_pro/block_script/template', 'my_lzb_pro_block_script_template', 10, 4 );Return an absolute filesystem path, not a URL. Lazy Blocks Pro reads the file's modification time for the version string and then converts the path to a URL by matching it against the content, theme, child theme and plugin directories in that order, falling back to ABSPATH. A path outside all of those registers no script and fails silently.
The script is registered in the footer with no dependencies, so a file expecting jQuery or wp.data has to wait for them itself. The stylesheet counterpart is lzb_pro/block_style/template, and where these files live is covered in Styles and Scripts.