将mustache.js与mustache.php结合使用

时间:2011-08-08 17:48:26

标签: php jquery json kohana mustache

我使用mustache.php(Kohana的KOstache)用持久性填充我的表单值。我的模板就是这样的:

<input type="checkbox" id="copy-billing-address"><span>My billing address is the same as my shipping address</span><br />

<label for="project[billing-organization]" class="pb-label">Organization:</label> <input type="text" name="project[billing-organization]" value="{{#field_data}}{{billing-organization}}{{/field_data}}"  />
{{#errors}}{{#billing-organization}}<span class="validation-error">{{billing-organization}}</span>{{/billing-organization}}{{/errors}}

<label for="project[billing-address-1]" class="pb-label">Address:</label> <input type="text" name="project[billing-address-1]" value="{{#field_data}}{{billing-address-1}}{{/field_data}}"  />

{{#errors}}{{#billing-address-2}}<span class="validation-error">{{billing-address-2}}</span>{{/billing-address-2}}{{/errors}}

<label for="project[billing-city]" class="pb-label">City:</label> <input type="text" name="project[billing-city]" value="{{#field_data}}{{billing-city}}{{/field_data}}"  />
{{#errors}}{{#billing-city}}<span class="validation-error">{{billing-city}}</span>{{/billing-city}}{{/errors}}

等...

我很想知道是否有一种方法/什么是我可以访问的最佳方式,并使用PHP返回的胡须数据来填充模板的值。例如:

<?php $view->jsondata = json_encode($array); ?>

//javascript
var template = $("#billing-address").innerHtml();
var data = {{jsondata}}
html = Mustache.to_html(template, data);
template.innerHtml(html);

如何使用mustache.php将{{jsondata}}发送到我的javascript?

1 个答案:

答案 0 :(得分:2)

您需要使用XHR请求执行相同的操作,或者如果在页面加载期间发生这种情况,则在呈现页面时将json存储在javascript变量中,并在页面初始化时调用mustache来呈现标记。

在php中你做了如下的事情:(原谅我,我不熟悉php语法)

<script>
var globalData= <?php //out json_encode($array); ?></script>

并且您的模板应该位于脚本标记中,如下所示:

<script id="billing-address" type="tmpl/js" >
<input type="checkbox" id="copy-billing-address"><span>My billing address is the same as my shipping address</span><br />

<label for="project[billing-organization]" class="pb-label">Organization:</label> <input type="text" name="project[billing-organization]" value="{{#field_data}}{{billing-organization}}{{/field_data}}"  />
{{#errors}}{{#billing-organization}}<span class="validation-error">{{billing-organization}}</span>{{/billing-organization}}{{/errors}}

<label for="project[billing-address-1]" class="pb-label">Address:</label> <input type="text" name="project[billing-address-1]" value="{{#field_data}}{{billing-address-1}}{{/field_data}}"  />

{{#errors}}{{#billing-address-2}}<span class="validation-error">{{billing-address-2}}</span>{{/billing-address-2}}{{/errors}}

<label for="project[billing-city]" class="pb-label">City:</label> <input type="text" name="project[billing-city]" value="{{#field_data}}{{billing-city}}{{/field_data}}"  />
{{#errors}}{{#billing-city}}<span class="validation-error">{{billing-city}}</span>{{/billing-city}}{{/errors}}
</script>

<div id="123"> </div> //div that you want to insert the markup tp

$(document).ready(function(){
    var template = $("#billing-address").html();  //use jquery methods when possible
    html = Mustache.to_html(template, globalData);
    $("#123").html(html); 
});
你应该好好去。

使用Jquery ajax方法的XHR方式。

$.ajax(
{
url: "getJson.php",
data: {id:123}, // data optional,
type: "GET",  //or POST depending on what you need
dataType:"json", //data you are receiving from server
success: function(jsonData)
{
var template = $("#billing-address").html();  //use jquery methods when possible
html = Mustache.to_html(template, jsonData);
$("#123").html(html);
}
});