Created
January 8, 2015 18:35
-
-
Save stephenharris/e23e3d86bf6ac52a8362 to your computer and use it in GitHub Desktop.
Allows registering Backbone templates as scripts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @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