2013-05-17 9 views
10

Devo effettuare la sostituzione con merge_vars.Come rendere un elenco con modello Mandrill fuori dall'array passato in MERGE VARS

{ 
    "key":"some key", 
    "template_name":"order-confirmation", 
    "template_content":[ 
     { 
     "name":"ORDERNUMBER", 
     "content":"12312312321" 
     }, 
     { 
     "name":"PRICE", 
     "content":"35.10" 
     }, 
     { 
     "name":"NAME", 
     "content":"Some name" 
     }, 
     { 
     "name":"PICKUPDATE", 
     "content":"2013-05-10" 
     }, 
     { 
     "name":"ORDERITEMS", 
     "content":[ 
      { 
       "NUMPRODUCTS":"26", 
       "PRODUCTNAME":"Milk", 
       "PRODUCTPRICE":"1.35 EUR" 
      } 
     ] 
     }, 
     { 
     "name":"SERVICENUMBER", 
     "content":"12345" 
     }, 
     { 
     "name":"PICKUPPOINT", 
     "content":"AAA 350" 
     }, 
     "message":{ 
     "from_email":"[email protected]", 
     "to":[ 
      { 
       "email":"[email protected]" 
      } 
     ], 
     "subject":"Subject text", 
     "attachments":[ 

     ] 
     }, 
     "async":false 
    } 
} 

come devo creare segnaposto html? L'ho fatto così ma non funziona. Mi interessa solo lo ORDERITEMS.

<tr> 
<td>*|ORDERITEMS:NUMPRODUCTS|*</td> 
<td>*|ORDERITEMS:PRODUCTNAME|*</td> 
<td>*|ORDERITEMS:PRODUCTPRICE|*</td> 
</tr> 
+0

hai provato *** var api = nuovo MandrillApi (ApiKey); var resultRender = api.Render (templateExample, templateContent, null); *** – Kiquenet

risposta

8

quanto ne so, usando il normale motore di template nel mandrillo, l'unico tipo supportato per l'attributo content è string, quindi non c'è modo per fornire una struttura.

Recentemente, (13 gennaio 2015) Mandrill ha annunciato il supporto per i modelli di manubri e, con questo, la possibilità di includere gli array nello content.

http://blog.mandrill.com/handlebars-for-templates-and-dynamic-content.html

Con questo nuovo linguaggio dei template è possibile non solo l'accesso sub-propertys come avete bisogno, ma anche all'interno del ciclo array e fare cicli anche nidificati. (Vedi this comment sul post del blog)

Questo è un esempio estratto dal post del blog per fare un ciclo:

La variabile di fusione o un modello di contenuti:

{ 
    "name": "products", 
    "content": [ 
     { 
      "img": "http://kbcdn.mandrill.com/nesting-penguin.png", 
      "qty": 2, 
      "sku": "PENG001", 
      "name": "Penguin", 
      "description": "Solid wood, hand-painted penguin nesting doll with 5 different sizes included. Limited Edition.", 
      "price": "12.99", 
      "ordPrice": "25.98" 
     }, 
     { 
      "img": "http://kbcdn.mandrill.com/nesting-bear.png", 
      "qty": 3, 
      "sku": "BBEAR001", 
      "name": "Brown bear", 
      "description": "Solid wood, hand-painted brown bear nesting doll. Coordinates with our entire Bear collection. Includes 6 nested sizes.", 
      "price": "12.99", 
      "ordPrice": "38.97" 
     } 
    ] 
} 

E questo è il modo di consumare nel modello:

{{#each products}} 
<tr class="item"> 
    <td valign="top" class="textContent"> 
     <img src="{{img}}" width="50" height="75" class="itemImage" /> 
     <h4 class="itemName">{{name}}</h4> 
     <span class="contentSecondary">Qty: {{qty}} x ${{price}}/each</span><br /> 
     <span class="contentSecondary sku"><em>{{sku}}</em></span><br /> 
     <span class="contentSecondary itemDescription">{{description}}</span> 
    </td> 
    <td valign="top" class="textContent alignRight priceWidth"> 
     ${{ordPrice}} 
    </td> 
</tr> 
{{/each}} 

nel tuo caso, si potrebbe fare:

{{#each ORDERITEMS}} 
<tr> 
<td>{{NUMPRODUCTS}}*</td> 
<td>{{PRODUCTNAME}}</td> 
<td>{{PRODUCTPRICE}}</td> 
</tr> 
{{/each}} 
Problemi correlati