用于PHP部分模板的Mustache

时间:2012-08-07 20:46:17

标签: php templates

我正在尝试使用Mustache for PHP做一些Mustache模板并遇到一个我不知道该怎么做的问题。

我有一个名为Designations的成员数组,它可以有大约6个值的倍数。 我需要返回一个带有每个指定图像的html表。

假设数组具有以下值:

$data = array("Designations"=>"CC,RM,SR", "YearsDesignated" => "1989, 2003, 2006");

如何使用Mustache生成此表:

<table class="table table-condensed" style="width: 100%;">
  <tbody>
    <tr>
      <td style="text-align:center; width: 16%;"><img src="images/cc-46x40.png" alt=""><br />CC Since: 1989</td>
      <td style="text-align:center; width: 16%;"><img src="images/rm-30x40.png" alt=""><br />RM Since: 2003</td>
      <td style="text-align:center; width: 16%;"><img src="images/sr-40x40.png" alt=""><br />SR Since: 2006</td>
      <td style="text-align:center; width: 16%;">&nbsp;</td>
      <td style="text-align:center; width: 16%;">&nbsp;</td>
      <td style="text-align:center; width: 16%;">&nbsp;</td>
    </tr>
  </tbody>
</table>

我假设使用了Partial但不知道如何做到这一点。

2 个答案:

答案 0 :(得分:0)

最终,我最终这样做:

胡子模板:

<table class="table table-condensed" style="width: 100%;">
  <tbody>
    <tr>
    {{# Designations}} 
      <td style="text-align:center; width: 16%;"><img src="images/{{Desig}}-46x40.png" alt=""><br />{{Desig}} Since: {{Year}}</td>
    {{/ Designations}} 
    </tr>
  </tbody>
</table>

数据:

$data = array("Designations"=>array(
            array("Desig"=>"CIC", "Year"=>1989), 
            array("Desig"=>"CRM", "Year"=>2003), 
            array("Desig"=>"CSRM", "Year"=>2006)
    )
);

最终渲染:

<table class="table table-condensed" style="width: 100%;">
  <tbody>
    <tr>
      <td style="text-align:center; width: 16%;"><img src="images/CIC-46x40.png" alt=""><br />CIC Since: 1989</td>
      <td style="text-align:center; width: 16%;"><img src="images/CRM-46x40.png" alt=""><br />CRM Since: 2003</td>
      <td style="text-align:center; width: 16%;"><img src="images/CSRM-46x40.png" alt=""><br />CSRM Since: 2006</td>
    </tr>
  </tbody>
</table>

答案 1 :(得分:-1)

尝试exploding数组值

$data = array("Designations"=>"CC,RM,SR", "YearsDesignated" => "1989, 2003, 2006");
$array1 = explode(",",$data["Designations"]);
$array2 = explode(",",$data["YearsDesignated"]);

并且对于标记,虽然没有使用该特定库 ..

echo '<table class="table table-condensed" style="width: 100%;"><tbody><tr>';
foreach($array1 as $offset=>$value){
 echo '<td style="text-align:center; width: 16%;">
       <img src="images/'.strtolower($value).'-thumb.png" alt="">
       <br />'.$value.'Since: '.$array2[$offset].'</td>';
}
echo "</tr></tbody></table>";