Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save franz-josef-kaiser/6f5448d83558427c5e7d to your computer and use it in GitHub Desktop.
Save franz-josef-kaiser/6f5448d83558427c5e7d to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: (WCM) Backbone/Underscore template loader
* Description: Loads Underscore (or other) templates using the WP Dependency API
*/
/**
* @author Franz Josef Kaiser http://unserkaiser.com/
* @link http://chat.stackexchange.com/transcript/message/19439060#19439060
*/
// Allows registering Backbone templates as scripts
// Add type="text/template" and id="{handle}" for Backbones .tmpl <script>s
add_filter( 'script_loader_tag', function( $html, $handle, $src )
{
if (
empty( $src )
or ! strstr( $src, '.tmpl' )
)
return $html;
$dom = new \DOMDocument;
$dom->loadHTML( $html );
/** @var \DOMElement $tag */
foreach ( $dom->getElementsByTagName( 'script' ) as $tag )
{
if ( $tag->hasAttribute( 'type' ) )
{
$tag->setAttribute( 'type', 'text/template' );
$tag->setAttribute( 'id', $handle );
$tag->appendChild( $dom->createTextNode( file_get_contents( $src ) ) );
# new node: 25% faster than
# @link http://chat.stackexchange.com/transcript/message/19567599#19567599
//$tag->nodeValue = esc_html( file_get_contents( $src ) );
$tag->removeAttribute( 'src' );
$html = $dom->saveHTML( $tag );
}
}
return $html;
}, 10, 3 );
<?php
/** Plugin Name: Underscore Template Test */
add_action( 'admin_enqueue_scripts', function( $screen )
{
if ( ! in_array( $screen, [ 'profile.php', 'user-edit.php', ] ) )
return;
$ext = (
! defined( 'SCRIPT_DEBUG' )
xor ( defined( 'SCRIPT_DEBUG' ) and ! SCRIPT_DEBUG )
&& ( defined( 'COMPRESS_SCRIPTS' ) and COMPRESS_SCRIPTS )
)
? '.min' : '';
$url = plugin_dir_url( __FILE__ ).'assets/';
$path = plugin_dir_path( __FILE__ ).'assets/';
$deps = (
defined( 'CONCATENATE_SCRIPTS' )
and CONCATENATE_SCRIPTS
)
? [ 'main', ]
: [ 'controller', 'models', 'views' ];
// Load template
// Currently without {$ext}
// Use HTMLMinify to compress your templates
wp_enqueue_script(
'template',
"{$url}/templates/template.tmpl",
$deps,
filemtime( "{$path}/templates/template.tmpl" ),
true
);
// Load main, concatenated file or single files
if (
defined( 'CONCATENATE_SCRIPTS' )
and CONCATENATE_SCRIPTS
)
{
wp_enqueue_script(
'main',
"{$url}/main{$ext}.js",
[ 'underscore', 'backbone', 'jquery', ],
filemtime( "{$path}/main{$ext}.js" ),
true
);
}
else
{
wp_enqueue_script(
'models',
"{$url}/models.js",
[ 'underscore', 'backbone', 'jquery', ],
filemtime( "{$path}/models{$ext}.js" ),
true
);
wp_enqueue_script(
'views',
"{$url}/views.js",
[ 'underscore', 'backbone', 'jquery', 'models', ],
filemtime( "{$path}/views{$ext}.js" ),
true
);
wp_enqueue_script(
'controller',
"{$url}/controller.js",
[ 'underscore', 'backbone', 'jquery', 'models', 'views', ],
filemtime( "{$path}/controller{$ext}.js" ),
true
);
}
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment