PHP Mustache对象访问数组

时间:2015-10-02 16:25:34

标签: php mustache template-engine mustache.php

JSON数据

 {
        "product_list": [
            {
                "title": "Title Name",
             }
             ,
             {
                  "title": "another title"
              }
           ]
    }

小胡子模板

{{#product_list}}
   {{title}}
{{/product_list}}

期待

> Title name
> Another title

但它没有显示任何东西。使用{{。}}显示产品对象的字符串表示形式。我想在每次迭代时访问对象的属性。

更新

public function render()
{
    $this->mustache=new \Mustache_Engine(array(
                'template_class_prefix' => '__MyTemplates_',
                'cache' => dirname(__FILE__) . '/tmp/cache/mustache',
                'cache_file_mode' => 0666, // Please, configure your umask instead of doing this :)
                'cache_lambda_templates' => true,
                'loader' => new \Mustache_Loader_FilesystemLoader($templateDir),
                'partials_loader' => new \Mustache_Loader_FilesystemLoader($templateDir . DIRECTORY_SEPARATOR .'partials'),
                'helpers' => array('i18n' => function($text) {
                        // do something translatey here...
                    }),
                'escape' => function($value) {
                    return htmlspecialchars($value, ENT_COMPAT, 'UTF-8');
                },
                'charset' => 'ISO-8859-1',
                'logger' => new \Mustache_Logger_StreamLogger('php://stderr'),
                'strict_callables' => true,
                'pragmas' => [\Mustache_Engine::PRAGMA_FILTERS],
            ));


        return $this->mustache->render($this->template,$data);
}

UPDATE $ data的var_dump()

array (size=2)
  'product_list' => 
    array (size=2)
      0 => 
        object(Product)[7]
          private 'title' => string 'Title Name' (length=10)
      1 => 
        object(Product)[7]
          private 'title' => string 'another title' (length=13)

0 个答案:

没有答案