2

JSON data

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

Mustache template

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

Expecting

> Title name
> Another title

But it is not showing anything. Using {{.}} showing the string representation of the product object. I want to access property of object on each iteration.

UPDATE

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 var_dump() of $data

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)
varuog
  • 3,031
  • 5
  • 28
  • 56

0 Answers0