简体   繁体   中英

How to print array with jekyll and liquid

I have a json document with some data

{"teams":[{"team":"Team A","evolution":[1,2]},{"team":"Team B","evolution":[3,4]}]}

I try to print it to my view with liquid

{% for team in teams %}
  <tr>
    <td><a href="#">{{team.team}}</a></td>
    <td>{{team.evolution}}</td>
  </tr>
{% endfor%}

The html result is

<tr>
    <td><a href="#">Team A</a></td>
    <td>12</td>
</tr>
<tr>
    <td><a href="#">Team B</a></td>
    <td>34</td>
</tr>

But what I would like to print is the raw array for the second <td>

<tr>
    <td><a href="#">Team A</a></td>
    <td>[1,2]</td>
</tr>
<tr>
    <td><a href="#">Team B</a></td>
    <td>[3,4]</td>
</tr>

Presuming that you get your datas from a _data/teams.json file, this works :

{% assign teams = site.data.teams.teams %}
<table>
{% for team in teams %}
  <tr>
    <td><a href="#">{{team.team}}</a></td>
    <td>{{team.evolution | join: "," | prepend: "[" | append: "]"}}</td>
  </tr>
{% endfor%}
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM