Skip to content

Instantly share code, notes, and snippets.

@jasonkarns
Created February 22, 2010 21:31
Show Gist options
  • Save jasonkarns/311531 to your computer and use it in GitHub Desktop.
Save jasonkarns/311531 to your computer and use it in GitHub Desktop.
JS Templating that upholds separation of concerns
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>JSHTML</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript" src="jquery.jshtml.js"></script>
<script type="text/javascript">
jQuery(function($){
$('script.jshtml').jshtml();
console.log(some_global);
});
</script>
</head>
<body>
<h1>Some header</h1>
<p>Some content</p>
<script type="text/html" class="jshtml">
<p>I'm a set of UI controls that require JS but now I'm contextually relevant and I'm not littering anyone's JS files with HTML!</p>
</script>
<p>I'm a separating paragraph that should not be altered.</p>
<script type="text/html" class="jshtml">
<p>I'm a second JSHTML paragraph that shouls only exist with JS on.</p>
</script>
<script type="text/javascript">
var some_global = "I'm just some normal JS that should be executed and not injected into the DOM as HTML.";
</script>
</body>
</html>
/*global jQuery */
(function($){
$.fn.jshtml = function(options){
var settings = $.extend({}, $.fn.jshtml.defaults, options);
return this.each(function(){
$(this).replaceWith($(this).text());
});
};
$.fn.jshtml.defaults = {};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment