如果我有像这样的PHP数组:
$a = array (
99 => 'Something1',
184 => 'Something2',
);
Run Code Online (Sandbox Code Playgroud)
密钥提供重要信息 - 它可以是一些常量值,ID等
然后我如何从模板中获取当前元素的关键字.例如:
{{#data}}
{.} - it is current value, but I need key also.
{{/data}}
Run Code Online (Sandbox Code Playgroud)
在我们的系统中太多这些类型的数组,它之前很难重新解析它们.什么是更好的解决方案?非常感谢你!
考虑:
<?php
/**
* Single variation display
*
* This is a javascript-based template for single variations (see https://codex.wordpress.org/Javascript_Reference/wp.template).
* The values will be dynamically replaced after selecting attributes.
*
* @see https://docs.woocommerce.com/document/template-structure/
* @author WooThemes
* @package WooCommerce/Templates
* @version 2.5.0
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<script type="text/template" id="tmpl-variation-template">
<div class="woocommerce-variation-description">{{{ data.variation.variation_description }}}</div>
<div class="woocommerce-variation-price">{{{ data.variation.price_html }}}</div>
<div class="woocommerce-variation-availability">{{{ data.variation.availability_html }}}</div>
<div class="woocommerce-variation-custom-text-field">
GTIN: <span class="sku">{{{ data.variation.wccaf_gtin }}}</span>
</div>
<div class="woocommerce-variation-custom-text-field">
MPN: …Run Code Online (Sandbox Code Playgroud) {{mustache}}在我看来,是一个很棒的模板库.我面临的问题是是在客户端还是在服务器端使用它.
我正在开发一个在线杂志,它将有一个主要页面,其中包含一篇大文章,其余文章的篇幅较小.
例如
+-------------------------------------------------+
| |
| Big Article Image |
| |
| |
+-------------------------------------------------+
Title
Author
+------------+ +------------+ +-----------+
| Image | | Image | | Image |
+------------+ +------------+ +-----------+
Title Title Title
Author Author Author
...
Run Code Online (Sandbox Code Playgroud)
使用{{mustache}}在服务器端,作为一个PHP库:当浏览器请求页面时,服务器将完成模板,并将其发送回.
优点:
缺点:
{{mustache}}因为在解析模板时,所有未匹配的"胡须标签"都会被删除(我不知道是否可以轻松,干净地避免这种情况). {{mustache}}.而不是简单的{{mustache}}我通常使用ICanHas.js,它包装{{mustache}}并使其非常容易和舒适:当浏览器要求页面时,HTML发送,包含所有js代码以询问服务器JSON哪个包含标题,作者和文件名图片.
优点:
缺点:
JSON(如图像文件名).根据您的经验,您认为哪一个是最佳解决方案?为什么?
我正在使用 Mustache.java 并希望插入 json 字符串而不是对象。我不确定为什么以前没有人遇到过这个问题。
// works since an 'Example' object is passed in
mustache.execute(new BufferedWriter(new FileWriter(objFile)), new Example()).flush();
// does not work since a json object is passed in directly
JSONObject jsonObject = new JSONObject("{\n" +
" \"header\": \"Colors\",\n" +
" \"items\": [\n" +
" {\"name\": \"red\", \"first\": true, \"url\": \"#Red\"},\n" +
" {\"name\": \"green\", \"link\": true, \"url\": \"#Green\"},\n" +
" {\"name\": \"blue\", \"link\": true, \"url\": \"#Blue\"}\n" +
" ],\n" +
" \"empty\": false\n" +
"}");
mustache.execute(new BufferedWriter(new FileWriter(objFile)), …Run Code Online (Sandbox Code Playgroud) 我已经mustache php在我的项目中设置了.
echo $template->render(array(
'data'=>$data,
'lang'=>$lang,
'getMaritialStatus' => function($text, Mustache_LambdaHelper $helper) {
return Common::getTextInHindi(ucwords(strtolower($helper->render($text))));
}
));
Run Code Online (Sandbox Code Playgroud)
我的用户定义的功能是
public static function getTextInHindi($maritialStatus) {
return $GLOBALS['lang'][$maritialStatus];
}
Run Code Online (Sandbox Code Playgroud)
现在在我的用户定义函数中,我在上面尝试打印时可以看到
print_r($GLOBALS['lang']['Married']); //gives correct output
print_r($GLOBALS['lang'][$maritialStatus]); //gives undefined index error
Run Code Online (Sandbox Code Playgroud)
即使$maritialStatus包含字符串'Married'.
为什么会这样呢?
我开始在PHP上使用Mustache,但我没有设法使包装函数成为债务.
我有这个模板
{{#skill_level}}
<span class="stars">
{{#stars}}
{{skill_level}}
{{/stars}}
</span>
{{/skill_level}}
Run Code Online (Sandbox Code Playgroud)
我有这些数据
$data = new StdClass;
$data->skill_level = 3;
$data->stars = function($level) {
$aux = "";
$l = intVal($level);
for ($i = 0; $i < $l; $i++) {
$aux .= "+";
}
for ($i = $l; $i < 5; $i++) {
$aux .= ".";
}
return $aux;
};
Run Code Online (Sandbox Code Playgroud)
我渲染m.render($tenplate, $data);,我想得到类似的东西:
<span class="stars">
+++..
</span>
Run Code Online (Sandbox Code Playgroud)
但它不起作用.
我明白了
<span class="stars">
.....
</span>
Run Code Online (Sandbox Code Playgroud)
因为Mustache传递"{{skill_level}}"给我的函数而不是值3.
此外,如果我更改模板,在胡子标签中放置退格:
{{ …Run Code Online (Sandbox Code Playgroud) 我正在使用Mustache 2.7.0并尝试首次使用Blocks pragma.
基本上,我称之为basic.mustache
{{< layout }}
{{$ title}}{{page.meta.name}}{{/ title}}
{{/ layout }}
Run Code Online (Sandbox Code Playgroud)
调用块layout.mustache
<!DOCTYPE html>
<html>
<head></head>
<body>
<h1>{{$ title}}test{{/ title}}</h1>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
我看到page.meta.name的值出现在页面上,而不是layout.mustache中写的标签.
任何人都知道为什么?
PHP
$mustache = new Mustache_Engine(array(
'pragmas' => [Mustache_Engine::PRAGMA_BLOCKS],
'loader' => new Mustache_Loader_FilesystemLoader('htdocs/templates'),
'partials_loader' => new Mustache_Loader_FilesystemLoader('htdocs/templates/partials/')
));
$tpl = $mustache->loadTemplate('basic');
echo $tpl->render( $this );
Run Code Online (Sandbox Code Playgroud) 我正在使用Silex构建应用程序,尽管它不是典型的Silex设置.
我有胡子作为模板引擎.
我没有使用Doctrine for ORM/DBAL,我正在使用Capsule (Silex-Eloquent),并且遇到了一些严重问题.
目前我有一个表格:
<form class="form-horizontal" role="form" action="app.php/listing" method="POST" id="listing-submit">
<div class="form-group">
<label class="control-label col-sm-2" for="title">Listing Title</label>
<div class="col-sm-6">
<input id="title" class="form-control" type="text" size="40" autocomplete="off"
data-encrypted-name="title"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="description">Description</label>
<div class="col-sm-6">
<textarea id="description" class="form-control" rows="8"></textarea>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Images</label>
<div class="col-sm-6">
<input type="file" name="images[]" id="images" multiple/>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-6">
<button type="submit" class="btn btn-default">Submit</button>
</div>
</div>
</form>
Run Code Online (Sandbox Code Playgroud)
我用它来选择文件,但是到目前为止我看到的所有Silex和文件上传的例子都使用了我不熟悉的东西,并且不确定我是否可以使它适应我的使用:FormBuilder ,FormBuilderInterface,注册自定义类型和所有爵士乐......
到目前为止,这是Controller-Part:
$app->post("/plisting", function () use ($app) …Run Code Online (Sandbox Code Playgroud) mustache.php ×8
mustache ×7
php ×4
architecture ×1
file ×1
java ×1
javascript ×1
json ×1
silex ×1
upload ×1
web ×1
wordpress ×1