テンプレートシステムが必要で、マニフェストバージョン2で動作するものを探して、これを見つけました...
http://code.google.com/p/json-template/wiki/Reference
...どこにもありませんコードはevalを使用しますか。残念ながら、私はそれを完全に使用する方法を理解するのが少し難しいと感じており、それを自分で見始めたばかりなので、私の次のコメントは完全に真実ではないかもしれません。
私はJQueryを使用していないので、実際に比較することはできませんが、ほとんどの場合、JQueryは必要なことを実行できると思います。
jqueryのようなスクリプトタグを使用したインラインコードは実行しませんが、実行は非常に簡単です。インラインテンプレートを使用できるように、これをかなりすばやく作成しました。
jsontemplate.inlineSCRIPT = function(source,templateoptions) {
var templates = source.querySelectorAll('script[type="text/json-template"]');
for (var i = 0; i < templates.length; i++) {
var template = templates[i];
var options = JSON.parse(template.getAttribute('options'));
if (!options.object) continue;
var sourceObjectBits = options.object.split('.');
var sourceObject = window[sourceObjectBits[0]];
for (var i = 1; i < sourceObjectBits.length; i++) {
if (!sourceObject[sourceObjectBits[i]]) break;
sourceObject = sourceObject[sourceObjectBits[i]];
}
if (i != sourceObjectBits.length) continue;
var target;
if (options.target) {
options.target == 'self' ? target = template : target = document.querySelector(options.target);
} else {
target = template;
}
if (!target) continue;
if (options.innerOuter) {
options.innerOuter == 'inner' ? target.innerHTML = jsontemplate.expand(template.innerHTML, sourceObject,templateoptions) : target.outerHTML = jsontemplate.expand(template.innerHTML, sourceObject,templateoptions);
} else {
target.outerHTML = jsontemplate.expand(template.innerHTML, sourceObject,templateoptions);
}
}
}
....そして私のhtmlで私は持つことができます...
<script type='text/json-template' options='{"object":"a.b.details"}'>
{# This is a comment and will be removed from the output.}
{.section songs}
{.repeated section @}
{.if equals test title}
test equaled title
{.end}
{.end}
{.or}
<p><em>(No page content matches)</em></p>
{.end}
</script>
テンプレートにはいくつかの条件がありますが、変数= trueの場合のように、かなり基本的なものです。しかし、変数が変数と等しい場合、これを行うための何も見つかりませんでした。カスタム述語を使用してそれらを追加することもできますが、jsontemplateのソースを変更せずに、引数を使用して述語を追加する方法を理解していません。述語を追加すると、必要な条件に対応できるはずですが、テンプレートの構文は見栄えがよくありません。if equalsを追加すると、テンプレートのソースは次のようになります。
{.if equals test title}
test equaled title
{.end}
...つまり、変数testの値がtitleの値と等しい場合は、このブロックを実行し、orブロックとelseのifをさらに使用できます。jsontemplateのソースを変更せずに追加する方法はわかりませんが、その方法は次のとおりです
。関数function _Pluralize(value, unused_context, args) {
行76の後に、次のような別の関数を追加します。
// Are two variables equal, gotta add the error checking
function _Mine(value, context, args) {
var s, p;
switch (args.length) {
case 0:
s = value;
break;
case 1:
s = context.get(args[0]);
break;
case 2:
s = context.get(args[0])==context.get(args[1]);
break;
default:
// Should have been checked at compile time
throw {
name: 'EvaluationError', message: 'pluralize got too many args'
};
}
return s;
}
..次に、コンパイル関数function _Compile(template_str, options) {
でその行(関数宣言のすぐ下)を探し、次のvar default_predicates = PrefixRegistry([
ように追加します。
// default predicates with arguments
var default_predicates = PrefixRegistry([
{name: 'equals', func: _Mine},
{name: 'test', func: _TestAttribute}
]);
...述語を使用して変数を設定することもできると確信していますが、それは試していません。とにかく、間違いなく一見の価値があります。