Skip to content

Instantly share code, notes, and snippets.

@roborourke
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roborourke/77b137362171b238efc1 to your computer and use it in GitHub Desktop.
Save roborourke/77b137362171b238efc1 to your computer and use it in GitHub Desktop.
Enqueue HTML snippets using WP Dependencies API
<?php
add_action( 'init', array( 'Enqueue_HTML', 'get_instance' ) );
class Enqueue_HTML {
public $scripts = array();
protected static $instance;
public static function get_instance() {
if ( self::$instance === null ) {
self::$instance = new self;
}
return self::$instance;
}
public function __construct() {
add_filter( 'script_loader_src', array( $this, 'loader_src' ), 9, 2 );
add_filter( 'script_loader_tag', array( $this, 'loader_tag' ), 9, 3 );
}
public function loader_src( $src, $handle ) {
$str_src = preg_replace( '/^[^<]*(<script.*?<\/script>).*$/sm', '$1', $src );
if ( strpos( $str_src, '<script' ) === 0 ) {
$this->scripts[ $handle ] = $str_src;
}
return $src;
}
public function loader_tag( $html, $handle, $src ) {
if ( isset( $this->scripts[ $handle ] ) ) {
return $this->scripts[ $handle ];
}
return $html;
}
}
<?php
add_action( 'admin_enqueue_scripts', function() {
// backbone template
wp_enqueue_script( 'html', '
<script type="text/html" id="example-backbone">
<div class="my-template" style="left: 0;">
<h1>{{ data.title }}</h1>
</div>
</script>
', array( 'jquery' ) );
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment