2010-10-17 3 views

Répondre

50

Selon the spec's changelog, l'itérateur implicite (.) a été ajouté à v1.1.0 de la spécification. Chaque bibliothèque Moustache qui implémente au moins v1.1.0 devrait supporter ceci.

{{#array_of_strings}}<li>{{.}}</li>{{/array_of_strings}} 
+0

Remarque: Les tableaux doivent disposer de clés implicites pour que cela fonctionne. L'utilisation de cette méthode lorsque votre tableau a des index entraînera la sortie d'une instance du mot 'Array'. – Popnoodles

9

Je me suis un peu éloigné de mon code et je me suis souvenu que Ruby était dactylographiée. Depuis mon tableau était de cordes, tout ce que je avais besoin était: Je vais laisser cette question ici

{{#my_array}} 
    <p>{{to_s}}</p> 
{{/my_array}} 

dans l'espoir de sauver quelqu'un d'autre un certain temps.

22

À partir du code source https://github.com/bobthecow/mustache.php

/** 
* The {{%IMPLICIT-ITERATOR}} pragma allows access to non-associative array data in an 
* iterable section: 
* 
*  $context = array('items' => array('foo', 'bar', 'baz')); 
* 
* With this template: 
* 
*  {{%IMPLICIT-ITERATOR}}{{#items}}{{.}}{{/items}} 
* 
* Would render as `foobarbaz`. 
* 
* {{%IMPLICIT-ITERATOR}} accepts an optional 'iterator' argument which allows implicit 
* iterator tags other than {{.}} ... 
* 
*  {{%IMPLICIT-ITERATOR iterator=i}}{{#items}}{{i}}{{/items}} 
*/ 
Questions connexes