[wp-trac] [WordPress Trac] #31281: Register JavaScript/Underscore templates using the WP Dependency API

WordPress Trac noreply at wordpress.org
Tue Feb 10 11:14:45 UTC 2015


#31281: Register JavaScript/Underscore templates using the WP Dependency API
---------------------------+-----------------------------
 Reporter:  F J Kaiser     |      Owner:
     Type:  enhancement    |     Status:  new
 Priority:  normal         |  Milestone:  Awaiting Review
Component:  Script Loader  |    Version:  trunk
 Severity:  normal         |   Keywords:
  Focuses:                 |
---------------------------+-----------------------------
 It would be much easier to handle JavaScript templates when they could get
 registered using `wp_register/enqueue_script()`. This would allow tight
 binding between (Backbone) scripts and their respective templates. It
 would also allow to easily switch out single (core or plugin) templates to
 extend the UI with custom extensions in plugins. Further more it would
 have the benefit of more fine grained control of what template to load
 where and therefore a lower memory footprint.

 For a brief look at the overall concept I'm proposing, please
 [https://gist.github.com/franz-josef-kaiser/6f5448d83558427c5e7d visit
 this Gist]. This is pretty much what I'm currently using.

 So instead of stuffing all templates as endless `<script
 type=text/template>` list in a single file, we can then split them up into
 single files (job for a later ticket - plugins can make use of it
 immediately). This also allows plugin developers to easily pull in core
 scripts and templates without modification, which should result in a more
 native and integrated UI and UX (when it comes to animations) without
 knowing every single bit of what is happening behind the scenes.

 {{{#!php
 wp_enqueue_script(
         'template',
         plugin_dir_url( __FILE__ ).'templates/template.tmpl',
         [ 'underscore', 'backbone', 'jquery', ],
         @filemtime( plugin_dir_path( __FILE__ ).'templates/template.tmpl'
 ),
         true
 );
 }}}

 In the added patch, there's also a filter named
 `'allowed_js_template_extensions'` to add custom extensions aside from the
 default `.tmpl` extension which should ease the usage of third party
 scripts and templates who might use other extension names.

 Note to other patchers: The patch makes use of the `\DOMDocument` and
 `\DOMElement` classes. Please do not try to switch this out for some
 Regex. The DOM classes are by far the fastest solution and much more
 reliable and readable than any Regex can ever be.

 This tickets/patches overall goal is to bring WP JS template handling
 closer to how native JS applications make use of it.

 ''Additional Benefits'': This also allows us to run performance
 optimization like HTML minification on it and further compress the served
 files (Grunt tasks available). Another option would be to concatenate
 those scripts on a per screen basis.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/31281>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list