Skip to content

Instantly share code, notes, and snippets.

@stephenharris
Created January 8, 2015 18:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stephenharris/e23e3d86bf6ac52a8362 to your computer and use it in GitHub Desktop.
Save stephenharris/e23e3d86bf6ac52a8362 to your computer and use it in GitHub Desktop.
Allows registering Backbone templates as scripts
/**
* @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 )
{
$dom = new \DOMDocument;
$dom->loadHTML( $html );
/** @var \DOMElement $tag */
foreach ( $dom->getElementsByTagName( 'script' ) as $tag )
{
if (
! empty( $src )
and $tag->hasAttribute( 'type' )
and strstr( $src, '.tmpl' )
)
{
$tag->setAttribute( 'type', 'text/template' );
$tag->setAttribute( 'id', $handle );
$tag->nodeValue = file_get_contents( $src );
$tag->removeAttribute( 'src' );
$html = $dom->saveHTML( $tag );
}
}
return $html;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment