Skip to content

Instantly share code, notes, and snippets.

@langalex
Created May 2, 2010 10:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save langalex/387041 to your computer and use it in GitHub Desktop.
Save langalex/387041 to your computer and use it in GitHub Desktop.
var styles = heredoc(function() {
/*
<style>
.my_css {
...
}
</style>
*/
});
$("head").append(styles);
function heredoc(fun) {
return (fun + '').replace('function () {', '').replace(/\}$/, '').replace('/*', '').replace('*/', '');
};
@radiospiel
Copy link

I don't know if a function to string conversion is standardized in javascript? Besides, this would not survive javascript compression... is there some other way to embed the heredoc outside of a comment string?

@langalex
Copy link
Author

langalex commented May 4, 2010

see http://twitter.com/cramforce/status/13238989508 - firefox excludes comments from the function string. afaik there's no other way, no.

@radiospiel
Copy link

Well, we could have somewhere else docs, i.e. embedded in a web sites dom tree?

<pre id="a12">
The text
</pre>
<script>
  var s = $("#a12").html();
</script>

e4x allows to embed XML directly into JS, looks like some kind of heredoc after all:

var languages = <languages type="dynamic">  
  <lang>JavaScript</lang>  
  <lang>Python</lang>  
</languages>; 

(Example from https://developer.mozilla.org/en/Core_JavaScript_1.5_Guide/Processing_XML_with_E4X ) . And e4x does have a natural looking syntax for accessing XML nodes.

But I don't know about supporting platforms besides Mozilla.

@langalex
Copy link
Author

langalex commented May 5, 2010

well my problem is that i have html templates in a javascript file so i can't use the dom trick. the e4x syntax would be perfect but only if it was available in all browsers.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment