carrot / roots-records

Parse JSON conveniently as roots locals to be used with any template engine.
Other
24 stars 11 forks source link

YAML #34

Open calebeby opened 8 years ago

calebeby commented 8 years ago

Hi guys, I was wondering if I could use the templating/out feature of this with YAML data. For example, in middleman, I can make proxies based on YAML data files. For example, in Middleman, I can do:

data.people.each do |name|
  proxy "/about/#{name}.html", "/about/template.html", :locals => { :person_name => name }
end

This would create a new page for each person in the YAML file. Is there a way to do this with roots-records without using JSON?

Thanks!

jescalan commented 7 years ago

I think this would probably be a more appropriate feature for roots-yaml. It could definitely be added. Are you interested in submitting a PR?

calebeby commented 7 years ago

I think that proxies should be separate from records and yaml so that the functionality is not duplicated and that it can be used by any extension.

This is how I think it should go:

load/run roots-yaml extension      <---\
load/run roots-records extension   <----|-- these are in any order
load/run any other extension       <---/
load/run roots-proxies extension - yaml() and records() functions are available.

The code to run roots-proxies would be like this:

proxies(
  hook: (proxy) ->
    for user in ['Caleb', 'Bob', 'Joe']
      proxy "users/#{user}", '_user.jade', locals: {user: user}
    for flight in records('flights')
      proxy "flights/#{flight}", '_flight.jade', locals: {flight: flight}
)

The proxy extension would call the hook function, pass proxy. In proxy, each time it was called, it would add the page information to a new pages array. After the config is done, the new pages would be created.

This is based on Middleman Dynamic Pages.

I have gotten the basic functionality of adding new pages, but I cannot use records or yaml in extension configs.

Do you plan on making view functions available in extension configs?

calebeby commented 7 years ago

If you can make view functions available in extension configs, or have another idea to make this possible, this weekend I will start implementing this extension.

calebeby commented 7 years ago

@jescalan I will have some time to work on this tomorrow. Do you want to discuss view functions in extension configs?

jescalan commented 7 years ago

Hey @calebeby, sorry again for the late response! So extension ordering is fairly complex, see here. That being said, the plan you have here should work fine. Have you had a chance to start on this? Feel free to ping me on gitter as well, I should be available this week!

calebeby commented 7 years ago

Thanks for looking at this.

I have got it working, it is kind of messy, I am not sure if I am misusing the system...

https://gitlab.com/classroom/classroom.gitlab.io/blob/master/dynamic.coffee

Basically, I set up a fs category of all files, and then after all pages in that category are done (all pages have been compiled, which means that records and yaml should be done by now. I found that roots.config.locals has all the locals, and that is passed into the configuration function.

calebeby commented 7 years ago

Then the config function returns an array of new pages with information about them. Each new page is then rendered.

calebeby commented 7 years ago

If you get a chance to clone it and fiddle with it, please note that when you click from one page to another, it displays a white page; that is because browsersync and turbolinks don't work well together. This is fixed in jescalan/roots#733

calebeby commented 7 years ago

The config for dynamic looks like this, as of now:

dynamic(
  hook: (locals) ->
    records = locals.records
    new_pages = []
    if records?
      for period in records['classes']
        new_pages.push
          path: "/classes/#{period.about.name.slugify()}"
          template: 'views/_period.jade'
          locals: period
      # print 'Records exists'
      for subject of records['assignments']
        for assignment, data of records['assignments'][subject]
          new_pages.push
            path: "assignments/#{subject}/#{assignment}"
            template: 'views/_assignment.jade'
            locals:
              subject: subject
              assignment: assignment
              assignment_data: data
      # print JSON.stringify new_pages
      new_pages
    else
      console.error 'Records doesn\'t exist'
)
calebeby commented 7 years ago

Also, when I run it, it gets run a whole bunch of times (6 times per file).

Is there an easier way for an extension to run after all the other ones?

jescalan commented 7 years ago

Hey @calebeby -- this looks good, nice work! It might be getting run extra times because other extensions create their own categories and the after_category hook runs for all categories. You can potentially fix this by checking the category name in that hook before running the logic 😁

calebeby commented 7 years ago

OK. I'll look at this this evening. Should I just add a test with browsersync configuration with a different port, and then request that port/page and make sure the request goes through?

On Thu, Oct 13, 2016, 7:24 AM Jeff Escalante notifications@github.com wrote:

Hey @calebeby https://github.com/calebeby -- this looks good, nice work! It might be getting run extra times because other extensions create their own categories and the after_category hook runs for all categories. You can potentially fix this by checking the category name in that hook before running the logic 😁

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/carrot/roots-records/issues/34#issuecomment-253528268, or mute the thread https://github.com/notifications/unsubscribe-auth/AMmFoXxcIs_hLaWrEbVc8JbOafwiOxXKks5qzj8LgaJpZM4J_vYb .

calebeby commented 7 years ago

Oh whoops. Wrong issue.

On Thu, Oct 13, 2016, 8:12 AM Caleb E caleb.eby01@gmail.com wrote:

OK. I'll look at this this evening. Should I just add a test with browsersync configuration with a different port, and then request that port/page and make sure the request goes through?

On Thu, Oct 13, 2016, 7:24 AM Jeff Escalante notifications@github.com wrote:

Hey @calebeby https://github.com/calebeby -- this looks good, nice work! It might be getting run extra times because other extensions create their own categories and the after_category hook runs for all categories. You can potentially fix this by checking the category name in that hook before running the logic 😁

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/carrot/roots-records/issues/34#issuecomment-253528268, or mute the thread https://github.com/notifications/unsubscribe-auth/AMmFoXxcIs_hLaWrEbVc8JbOafwiOxXKks5qzj8LgaJpZM4J_vYb .

calebeby commented 7 years ago

If other extensions have an after_category hook for all categories, won't it keep bouncing back and forth between the extensions? I will try disabling all of the other extensions this evening.

On Thu, Oct 13, 2016, 8:13 AM Caleb E caleb.eby01@gmail.com wrote:

Oh whoops. Wrong issue.

On Thu, Oct 13, 2016, 8:12 AM Caleb E caleb.eby01@gmail.com wrote:

OK. I'll look at this this evening. Should I just add a test with browsersync configuration with a different port, and then request that port/page and make sure the request goes through?

On Thu, Oct 13, 2016, 7:24 AM Jeff Escalante notifications@github.com wrote:

Hey @calebeby https://github.com/calebeby -- this looks good, nice work! It might be getting run extra times because other extensions create their own categories and the after_category hook runs for all categories. You can potentially fix this by checking the category name in that hook before running the logic 😁

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/carrot/roots-records/issues/34#issuecomment-253528268, or mute the thread https://github.com/notifications/unsubscribe-auth/AMmFoXxcIs_hLaWrEbVc8JbOafwiOxXKks5qzj8LgaJpZM4J_vYb .

jescalan commented 7 years ago

one of the params to the hook is the name of the category, so you can use this to run actions just for the category you want