-1

我的代码将从数据文件中捕获项目。我需要用逗号分隔。我没有运气!

数据文件socialmedia.yml

facebook:
  id: 'dpcgco'
  href: 'https://www.facebook.com/'
  title: 'Facebook'
  fa-icon: 'fa-facebook-square'

twitter:
  id: 'DenverProphitJr'
  href: 'https://www.twitter.com/'
  title: 'Twitter'
  fa-icon: 'fa-twitter-square'

我试过这个。但是,它不会用逗号分隔它们:

 "sameAs":[ 
{% if site.data.socialmedia %}
    {% assign sm = site.data.socialmedia %}
    {% for entry in sm %}
        {% assign key = entry | first | split%}
        {% if sm[key].id %}
{% capture social %}{{ sm[key].href }}{{ sm[key].id }}{% endcapture %}
    {{ social | replace: " ", "," | jsonify }}
        {% endif %}
    {% endfor %}
{% endif %}
   ],

所需的输出格式:

 "sameAs": [
    "http://www.facebook.com/your-profile",
    "http://instagram.com/yourProfile",
    "http://www.linkedin.com/in/yourprofile",
    "http://plus.google.com/your_profile"
  ]

实际无效输出:

"sameAs":[ "https://www.facebook.com/dpcgco" "https://www.twitter.com/DenverProphitJr" ],
4

1 回答 1

1

您必须检查一个项目是否是forloop.last.

  {% if site.data.socialmedia %}
  {% assign sm = site.data.socialmedia %}
"sameAs":[
    {% for entry in sm %}
      {% assign key = entry | first %}
        {% if sm[key].id %}"{{ sm[key].href }}
        {{ sm[key].id }}",
        {% if forloop.last %}
        "{{ sm[key].href }}{{ sm[key].id }}"
       {% endif %}
    {% endif %}
  {% endfor %}
  {% endif %}
 ],
于 2019-09-15T02:39:39.663 回答