首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >结合使用mustache.js和mustache.php

结合使用mustache.js和mustache.php
EN

Stack Overflow用户
提问于 2011-08-09 01:48:26
回答 1查看 1.9K关注 0票数 2

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

代码语言:javascript
复制
<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返回的八字胡数据来填充模板值的最佳方法。例如:

代码语言:javascript
复制
<?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?

EN

回答 1

Stack Overflow用户

发布于 2011-08-09 13:55:19

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

在php中,你可以这样做:(请原谅,我不熟悉php语法)

代码语言:javascript
复制
<script>
var globalData= <?php //out json_encode($array); ?></script>

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

代码语言:javascript
复制
<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方式。

代码语言:javascript
复制
$.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);
}
});
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6986271

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档