I've noticed that since WP 3.5 that we now have the Backbone.js and Underscore.js libraries included. The new Media Modal uses Underscore templates and I was wondering how would I be able to insert them, especially in the admin side of things.Is there a correct way to insert these?
-
1Please search the site for questions related to how to enqueue scripts.– Chip BennettCommented Feb 25, 2013 at 15:05
-
1possible duplicate of Where is the right place to register/enqueue scripts & styles– Chip BennettCommented Feb 25, 2013 at 15:05
-
4@ChipBennett this question is not about register/enqueue scripts, it is about using underscore templates in new Media popup– Eugene ManuilovCommented Feb 25, 2013 at 15:08
-
Technically you're both right. My fault for posting a question while half asleep.– Manny FleurmondCommented Feb 25, 2013 at 15:14
-
Can you load an Underscore template from an external file via a script tag?– Manny FleurmondCommented Feb 25, 2013 at 15:27
|
Show 3 more comments
1 Answer
Your plugin index file:
add_action( 'print_media_templates', 'wpse8170_admin_footer' );
function wpse8170_admin_footer() {
require 'templates.php';
}
Your templates.php:
<script id="tmpl-mytemplate" type="text/html">
<h1>Hello {{data.name}}!</h1>
</script>
Your media js file:
wp.media.view.MyView = wp.media.View.extend({
template: wp.media.template('mytemplate'),
render: function() {
this.$el.html(this.template({name: 'world'}));
}
});