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

发布于 2024-11-28 06:33:11 字数 1655 浏览 1 评论 0原文

我正在使用 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?

I am using mustache.php (Kohana's KOstache) to populate my form values with persistence. My template is something along the lines of this:

<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}}

etc...

I'm curious to know if there is a way / what's the best way that I can access and use the mustache data that PHP is returning to populate the values of the template. For example:

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

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

How can I send {{jsondata}} to my javascript using mustache.php?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

不甘平庸 2024-12-05 06:33:11

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

在 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); 
});

您应该可以开始了。

XHR方式使用Jquery ajax 方法。

$.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);
}
});

you need to use an XHR request to do the same or if this happens during page load store the json in a javascript variable when the page is rendered and when the page is initialized call mustache to render your markup.

in php you do something as follows: (forgive me I am not familiar with php syntax)

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

and your template should live in a script tag as follows:

<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); 
});

and you shoud be good to go.

The XHR way using Jquery ajax method.

$.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);
}
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文