quick mustache-like templates for Javascript and Perl

I needed some quick templates for Javascript (with a touch of jQuery, which you can easily replace with forEach) without incorporating the mustache script.

function templatize(text, hash) {
	function deep(key) {
		var result = hash;
		key = key.split('.');
		while (result && key.length) { result = result[key.shift()]; }
		return result;
	}
	// very simple version of mustache templates
	// replace all '{{#key}}contents{{/key}}' with list of 'contents'
	// if value of hash['key'] is not 0 or is not an empty list
	text = text.replace(/\{\{#([^\}]+?)\}\}(.*?)\{\{\/\1\}\}/g, function(match, key, subtext) {
		var val = deep(key);
		if (val.constructor == Array) {
			return $.map(val, function(el, i) {
				console.log(el);
				return templatize(subtext, el);
			}).join("");
		}
		else {
			return (val? subtext  :'')
		}
	});
	// replace all '{{key}}' with the value at hash['key']
	text = text.replace(/\{\{([^\}]+?)\}\}/g, function(match, key) {
		return deep(key);
	});
	return text;
}

It also quickly ports to perl, though this version is a bit less sophisticated.

sub templatize {
        my($text, %hash) = @_;
        # very simple version of mustache templates
        # replace all '{{#key}}contents{{/key}}' with 'contents' 
        # if value of $hash{'key'} is not 0
        $text =~ s/\{\{#([^\}]+?)\}\}(.*?)\{\{\/\1\}\}/($hash{$1}?$2:'')/eg;
        # replace all '{{key}}' with the value at $hash{'key'}
        $text =~ s/\{\{([^\}]+?)\}\}/$hash{$1}/g;
        return $text;
}