rubymiddlemancontentful

How to get nested loops in config.rb to work in Middleman?


We have been working for several years on Middleman, the static HTML site builder. After a recent upgrade we have run into an issue that has our whole team stumped. In the config.rb all proxy calls work fine until we try to run a nested loop like this:

data.site.datatype.each do | id, c |
         puts c
      data.site.datatype.each do | id, c2 |
         puts c2
      end
    end

[Note that data.site.datatype are .yaml files imported using middleman-contentful.]

The behavior is that the outer loop runs fine but the inner loop refuses to execute.

The output of the outer loop (puts c) shows the middleman import of the .yaml payload as follows:

#<Middleman::Util::EnhancedHash id="id-1" name="comp1" slug="comp1">
#<Middleman::Util::EnhancedHash id="id-2" name="comp2" slug="comp2">

We have been stuck on this issue for a week now so any guidance would be very welcome!

The Gemfile is as follows:

source 'https://rubygems.org'
gem 'middleman', '~> 4.2'
gem 'middleman-autoprefixer', '~> 2.7'
gem 'tzinfo-data', platforms: [:mswin, :mingw, :jruby, :x64_mingw]
gem 'wdm', '~> 0.1', platforms: [:mswin, :mingw, :x64_mingw]
gem 'middleman-dotenv', '~> 2.0'
gem 'contentful_middleman', '~> 4.2.0'

Otherwise the config.rm has activate :dotenv and activate :contentful

We have gone back to https://middlemanapp.com/advanced/dynamic-pages/ and can't find anything there that helps.


Solution

  • Update: While we have not found any real documentation on this, here is the answer.
    While the use of data.site works in the .html.erb templates, in config.rb the call to the data files requires @app.data.site.

    So the example code above should read

    @app.data.site.datatype.each do | id, c |
             puts c
          @app.data.site.datatype.each do | id, c2 |
             puts c2
          end
        end
    

    The hint came from the Middleman repo:

    https://github.com/middleman/middleman/blob/master/middleman-core/lib/middleman-core/core_extensions/data.rb#L15-L19

        # Make the internal `data_store` method available as `app.data`
        expose_to_application data: :data_store
    
        # Exposes `internal_data_store` to templates, to be wrapped by `data` in the context
        expose_to_template internal_data_store: :data_store