标签: mustache.php

PHP胡子.隐式迭代器:如何获取当前值的键(数字php数组)

如果我有像这样的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)

在我们的系统中太多这些类型的数组,它之前很难重新解析它们.什么是更好的解决方案?非常感谢你!

mustache mustache.php

7
推荐指数
2
解决办法
6761
查看次数

将条件添加到mustache / php

考虑:

<?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)

javascript php wordpress mustache mustache.php

7
推荐指数
1
解决办法
173
查看次数

服务器端或客户端{{mustache}}?

{{mustache}}在我看来,是一个很棒的模板库.我面临的问题是是在客户端还是在服务器端使用它.

我正在开发一个在线杂志,它将有一个主要页面,其中包含一篇大文章,其余文章的篇幅较小.

例如

+-------------------------------------------------+
|                                                 |
|             Big Article Image                   |
|                                                 |
|                                                 |
+-------------------------------------------------+
     Title
     Author

+------------+     +------------+     +-----------+
|   Image    |     |   Image    |     |   Image   |
+------------+     +------------+     +-----------+
  Title               Title              Title
  Author              Author             Author
...
Run Code Online (Sandbox Code Playgroud)

服务器端选项

使用{{mustache}}在服务器端,作为一个PHP库:当浏览器请求页面时,服务器将完成模板,并将其发送回.
优点:

  • 加载时间会更好.浏览器一旦收到html代码,就会知道它要向服务器询问哪些其他资源.

缺点:

  • 很难与客户端集成,{{mustache}}因为在解析模板时,所有未匹配的"胡须标签"都会被删除(我不知道是否可以轻松,干净地避免这种情况).
  • 我不喜欢从服务器端修改接口,我宁愿从客户端进行,因此3层架构似乎更清晰(你可能会有所不同).
  • 我以前没有服务器端的经验{{mustache}}.

客户端选项

而不是简单的{{mustache}}我通常使用ICanHas.js,它包装{{mustache}}并使其非常容易和舒适:当浏览器要求页面时,HTML发送,包含所有js代码以询问服务器JSON哪个包含标题,作者和文件名图片.
优点:

  • 增强3层架构.
  • 像无限滚动(其他ajax的东西)这样的事情非常简单.

缺点:

  • 加载时间恶化.浏览器需要接收代码,发出JSON请求,然后向服务器询问发现的资源JSON(如图像文件名).

根据您的经验,您认为哪一个是最佳解决方案?为什么?

architecture mustache web mustache.php

6
推荐指数
1
解决办法
2322
查看次数

Mustache.java 传递 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)

java json mustache mustache.php

5
推荐指数
1
解决办法
2159
查看次数

如何在小胡子php中使用用户定义的函数

我已经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 mustache.php

5
推荐指数
1
解决办法
130
查看次数

如何在mustache.php中使用函数包装器?

我开始在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)

php mustache mustache.php

2
推荐指数
1
解决办法
2643
查看次数

使用外部模板渲染Moustache块

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

mustache mustache.php

2
推荐指数
1
解决办法
445
查看次数

PHP表单 - 上传文件,想要使用Silex而不是FormBuilder

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

php upload file silex mustache.php

2
推荐指数
1
解决办法
1564
查看次数

标签 统计

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