String.prototype.evalSharps

prototype.js の Template 周りの複雑さには正直驚いた。何も考えずに実装すると

String.prototype.evalSharps = function(context){
  with(context === undefined || context === null ? window : context)
    return this.replace(
      /(\\?)#\{((?:[^{}]+|\{[^{}]*?\})*)\}/g, // 面倒なのでネストは一重のみ考慮
      function(_all, _esc, _exp){ return _esc ? _all.slice(1) : eval(_exp) });
};

confirm(['hoge#{prompt("","fuga")}piyo'.evalSharps(),
         '#{hoge.fuga}'.evalSharps({hoge:{fuga:'piyo'}}),
         '#{"#{fuga}".evalSharps(hoge)}'.evalSharps({hoge:{fuga:'piyo'}}),
         '<a href="#{location}">#{title}</a>'.evalSharps(document)].join('\n'));

で済んでしまうのだが…。