Skip to content

Instantly share code, notes, and snippets.

@denisdefreyne
Last active December 30, 2015 06:29
Show Gist options
  • Save denisdefreyne/7789295 to your computer and use it in GitHub Desktop.
Save denisdefreyne/7789295 to your computer and use it in GitHub Desktop.
Mustache + nanoc requirements

Goal: excellent Mustache support in nanoc

Need alternatives for:

  • <%= item[:blah] %>{{ item:blah }}
  • <%= item.path %>{{ item.path }}
  • <%= layout[:blah] %>{{ layout:blah }}
  • <%= config[:blah] %>{{ config:blah }}
  • <%= some_helper(…) %> → ???
  • <% some_helper(…) do %>…<% end %> → ???

Maybe these (not sure whether these are necessary in logicless templates):

  • <%= @items['/some/item/'][:blah] %> → ???
  • <%= @items['/some/item/'].path %> (and compiled_content etc) → ???

Conventions used here:

  • {{ item:blah }}: access attribute :blah in current item (totally not sure whether I like the :)
  • {{ item.path }}: call “method” path on current item (not really a method call, because you won’t be able to just call any method)
@denisdefreyne
Copy link
Author

Also <%= @items['/some/item/'].path %> I guess.

@jugglinmike
Copy link

I'm kind of new to Mustache, so I'm still not sure if it allows for nested attributes. Would we be able to access nested item metadata? i.e. given an item like

---
location:
  venue_name: Agganis Arena
  venue_city: Boston, MA

...would we be able to write {{ item.location.venue_city }} ?

@jugglinmike
Copy link

I'm not sure if this is contrary to the design of nanoc, but what about allowing for a "global" context hash that gets mixed in to each expansion? I would like to be able to use data structures that I create in the preprocess block such as sorted category lists, etc.

Then layouts could use

{{#categories.news}}
  <li>{{title}}</li>
{{/categories.news}}

@denisdefreyne
Copy link
Author

I think Mustache only works with key-value pairs, but {{ item.location.venue_city }} could work if you have a key item.location.venue_city and value Boston, MA.

As for globals, I’d suggest using the @config instead of globals, because the latter may break dependency tracking in odd ways.

@jugglinmike
Copy link

Just checked (using defunkt/mustache):

>> require 'mustache'
=> true
>> Mustache.render("Hello {{planet.n}}", :planet => { 'n' => 3 })
=> "Hello 3"

@denisdefreyne
Copy link
Author

Some ideas on accessing “custom” (e.g. :title) vs “built-in” attributes (e.g. path):

  • {{ item.:title }} and {{ item.path }}
  • {{ item.meta.title }} and {{ item.path }}
  • {{ item.attr.title }} and {{ item.path }}

@jugglinmike
Copy link

It looks like defunkt/mustache already performs string -> symbol mapping. Example

require 'mustache'

context = {
    'item' => {
        'string' => 'string key',
        :symbol => 'symbol key',
        'both' => 'string key',
        :both => 'symbol key'
    }
}

puts Mustache.render(".string\t{{item.string}}", context)
puts Mustache.render(".symbol\t{{item.symbol}}", context)
puts Mustache.render(".both\t{{item.both}}", context)

...outputs:

.string string key
.symbol symbol key
.both   symbol key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment