1

クライアント側でSmarty 3 テンプレートをレンダリングするためにjsmartを使用しようとしています。それらの経験がない場合は、読み続けてください。単純な JavaScript エラーである可能性があります。


シンプルなテンプレートで機能します。

ドキュメントに従って、テンプレートを作成し (AJAX 経由で受け取る)、レンダリングします (データを渡します)。

var template = new jSmart(templateReceivedViaAJAX);
var content = template.fetch({"firstname":"adam", "secondname":"lynch"});

次に、レンダリングされた出力をdiv:

$('#dest').html(content);   

テンプレートの継承

includeextendsなどを含むテンプレートをレンダリングしようとすると問題が発生します。

ドキュメントから:

jSmart は、いずれかのテンプレート インクルージョン タグに遭遇するたびに、jSmart.prototype.getTemplate() メソッドを呼び出し、タグのファイル パラメータの値を渡します。メソッドは、テンプレートのテキストを返す必要があります。

getTemplate() のデフォルトの実装は例外をスローします。したがって、このメソッドをオーバーライドしてテンプレートのテキストを提供するのは、jSmart ユーザー次第です。


関数のオーバーライドgetTemplate():

jSmart.prototype.getTemplate = function(name) {
    $.ajax({type: 'GET', url: name, async:false, success: function(data) {
        console.log('got template at '+name+'. The following is the contents:');
        console.debug(data);
                
        return data;
    }});
}

テンプレートへの呼び出しを含むテンプレートをレンダリングするときのコンソール出力:include

<div class="row">

    <label for="second" class="span4">Second Name:</label>  

    <input type="text" class="span4" placeholder="{$secondname}" id="second" /> 

</div>

<p>B;lsdsfasfsfds</p>
Uncaught Error: No template for /bundles/templatedemo/templates/form_include.html.smarty 

テンプレートへの呼び出しを含む子テンプレートをレンダリングするときのコンソール出力:extend

got template at /bundles/templatedemo/templates/form.html.smarty. The following is the contents: templates:58
<form class="well">  

    <div class="row">

        <label for="first" class="span4">First Name:</label>  

        <input type="text" class="span4" placeholder="{$firstname}" id="first" /> 

    </div>

    {block name=form_include}{/block}

    <input type="submit" class="btn btn-inverse" />  

</form>
Uncaught Error: No template for /bundles/templatedemo/templates/form.html.smarty 
(expanded:)
S 
(anonymous function) 
jSmart.fetch 
(anonymous function) 
f.event.dispatch 
f.event.add.h.handle.i

編集:

テンプレートのコンテンツが事前に存在する場合 (たとえば、AJAX 経由で取得するのではなく、ハードコードされている場合) は継承が機能します。

4

1 に答える 1

0

無名関数ではなく関数ポインタを使用します。

function foo() 
  {
  $.ajax({type: 'GET', url: foo.url, async:false, success: bar});
  }

function bar(data) 
  {
  console.log(['got template at', foo.url, 'The following is the contents:']);
  console.debug(data);

  return data;
  }

foo.url = "http://www.stackoverflow.com";

jSmart.prototype.getTemplate = foo;
于 2013-11-01T22:17:42.427 に答える