General: Throw a more descriptive error when templates are not found.

When `wp.template()` is called in JavaScript for a template element that does not exist, a very nondescript error message is currently returned (“Uncaught TypeError: Cannot read property `replace` of `undefined`”).

This updates adds a check for this scenario and a new “Template not found” error is now thrown instead.

Props joehoyle, noisysocks, hilayt24.
Fixes #36631.

git-svn-id: https://develop.svn.wordpress.org/trunk@54241 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers
2022-09-20 03:50:40 +00:00
parent 7a6ba32941
commit 826e1a3f89
+3
View File
@@ -36,6 +36,9 @@ window.wp = window.wp || {};
};
return function ( data ) {
if ( ! document.getElementById( 'tmpl-' + id ) ) {
throw new Error( 'Template not found: ' + '#tmpl-' + id );
}
compiled = compiled || _.template( $( '#tmpl-' + id ).html(), options );
return compiled( data );
};