0

Stack overflow

I’m having a noob problem trying to figure out how to print some YML data in a ‘structured random order’. What I mean by that is that I’m using Contenful CMS to order content in a lookbook (https://www.contentful.com/blog/2015/09/10/creating-a-digital-lookbook/) kind of fashion.

Drag and drop CMS interface I can drag and drop blocks from here, which will change the order for the YML

The content editors can have the option to drag and drop the order of how the modules are shown. So the order of the YML data will always be different.

A quick mockup of what I want to achieve:

enter image description here

This is currently how the data looks

:caseModules:
- :id: 2sY3tXCqSIcIM2IUgqCCgG
  :quoteBody: Either you run the day or you day runs you
  :quoteOriginName: Ghandi
  :quoteDate: !ruby/object:DateTime 2017-03-23 00:00:00.000000000 Z
- :id: 2qpSXdyiqg8iQucGoQweio
  :caseImage:
    :title: Test image title
    :description: Test image description
    :url: "//images.contentful.com/d65nu5c5ibm8/3l2tizTPRSACEIUK0OugUi/65a718dfa621cb6d1d384251ef83a787/account.png"
  :caseImageName: Test image title
  :caseImageCaption: This test image is a nice example of how we want to showcase
    images on the page
- :id: 3HaLvYSEs86ACoqMYE0QSk
  :header: Header
  :body: "There are previous versions because you haven't made changes to this entry
    yet. As soon as you publish changes, you'll be able to compare different versions.\n\n"
- :id: 6HASpa0o3SkyMK4gYgIq8W
  :quoteBody: This is another quote
  :quoteOriginName: Sebastian Graz
  :quoteDate: !ruby/object:DateTime 2019-01-19 00:00:00.000000000 Z
- :id: 3D6Gx2qphK6qAI2qyYGcwK
  :caseImage:
    :title: king-fisher
    :description: With King Fisher we managed to create an unique experience using
      Spindrift Site Builder
    :url: "//images.contentful.com/d65nu5c5ibm8/5kCjQiyCv6m00m0m46UWi4/ce4ab4cdc8ade1fd63f3e564def42e45/king-fisher.png"
  :caseImageName: King Fisher case image name
  :caseImageCaption: King fisher image caption

Im using .erb to print the data like so:

  <% data.site.container.each do | _, project| %>
    <%= project['caseModules']%>
  <% end %>

Which gives me this on the front-end.

[# quoteOriginName="Ghandi">, # caseImageCaption="This test image is a nice example of how we want to showcase images on the page" caseImageName="Test image title" id="2qpSXdyiqg8iQucGoQweio">, #, # quoteOriginName="Sebastian Graz">, # caseImageCaption="King fisher image caption" caseImageName="King Fisher case image name" id="3D6Gx2qphK6qAI2qyYGcwK">]

I was hoping I could do something like:

  <% data.site.container.each do | _, project| %>
    <% project['caseModules'].each do | image | %>
       <%= image['caseImage']['url']%>
    <% end %>
  <% end %>

But I get the error: undefined method `[]' for nil:NilClass

So I'm wondering if anyone can give me some pointer on what I'm doing wrong. Should I be using a switch case to render the different block? Am I doing something wrong in the <%= image['caseImage']['url']%> Syntax that it doesn't work?

Any help is appreciated

EDIT:

I ended up using has_key? and it worked.

  <% data.site.container.each do | id, container| %>
    <% container.caseModules.each do | caseModules | %>

       <% if caseModules.has_key?("caseImage") && caseModules["caseImage"]%>
          <img src="<%= caseModules.caseImage['url'] %>" alt="" />
       <% end %>

       <% if caseModules.has_key?("quoteBody") && caseModules["quoteBody"]%>
          <%= caseModules.quoteBody %>
       <% end %>

       <% if caseModules.has_key?("body") && caseModules["body"]%>
          <%= caseModules.body %>
       <% end %>


    <% end %>
  <% end %>
4

1 回答 1

1

问题是 YAML 文件中的键是符号而不是字符串,将其替换为两者中的任何一个project[:caseModules]project.caseModules任何一个都将按预期工作。

于 2017-03-08T12:13:01.307 回答