![]() ![]() To build the site, run: bundle exec middleman build resources ) end endīe aware that if talk.rb changes, Middleman won’t pick up on it by default unless you restart the server. Move the class into a file called lib/talk.rb, and use require to load it into config.rb: require './lib/talk' helpers do def talks Talk. We can also throw away initialize, because SimpleDelegator does that for us.įinally, extract the class from the helpers block and move it into its own file. We don’t need to explicitly define the url or data methods any more. new ( resource ) end end endĪny method on the resource object becomes available for use. start_with? ( '/talks' ) end filtered_resources. all ( resources ) filtered_resources = resources. It turns out there’s a class in the standard library which does this for us, so we can simplify our code: class Talk < SimpleDelegator def title data. In initialize, we’re passing in an object that we’re going to delegate other methods to. Great! But there’s still a little room for refactoring. We’ve added the methods we’d like to have available – in this case, url and title, that simply delegate to the resource object underneath. We’ve created a Decorator for the resource object. Let’s change that to returning an array of Talk instances: class Talk def initialize ( resource ) = resource end def url. We’re still returning a simple filtered array of resources. We pass in sitemap.resources because it allows us to decouple the Talk class from having to know about the sitemap, and make it easier to test later if we need to. start_with? ( '/talks' ) end end end def talks Talk. start_with? ( '/talks' ) end end endįirst, we can pull the implementation of talks into the talk class: helpers do class Talk def self. helpers do class Talk end def talks sitemap. For convenience, we’ll continue to keep everying inside the helpers block inside config.rb for now. ![]() Let’s extract talks into their own class. It would be much nicer if we could just use talk.title. We can now change our source/ to use new the method: īut can we do better? The still feels awkward. start_with? ( '/talks' ) end end endĪny method defined inside the helpers block becomes available to every page. Inside config.rb, add the following method definition to the helpers block: helpers do def talks sitemap. Let’s extract the awkward code in the example above to something nicer. We can write our own helper methods, too. Link_to is a helper method provided to us by Middleman for generating links. We access the the frontmatter through the data attribute of the resource. We can filter these resources by URL, for instance, to select only those that are inside the source/talks directory. The simplest way we can do this is through Middleman’s sitemap.resources, which contains a list of all resources in the site. Now that we have our talks, we want to create a list of them to display on the home page. We can use these attributes elsewhere in the site. Any valid YAML is fair game for the frontmatter. The title and author are attributes we simply made up. If no layout is given, it will fall back to the default layout (typically source/layouts/layout.erb). When given, it tells Middleman to use a particular layout inside source/layouts (in this case, we gave talk as the value, which corresponds to source/layouts/talk.erb). The layout attribute has a special meaning in Middleman. Understanding frontmatterįor this page, we have three pieces of frontmatter: the layout of the page, the title and the author of the talk. Everything following the frontmatter is the content of the page (in this case, written in Markdown). The metadata, also known as the frontmatter, consists of YAML. The two sets of three dashes ( -) delineate the page’s metadata. Create a source/talks directory, and add a new file called middleman.md.erb: - title : Middleman layout : talk author : Pete Nicholls. Your site will be served up from Adding a talk Start a local server by cd-ing into the newly created talks directory and running: bundle exec middleman server Generate a new site: middleman init talks You can see the finished project on GitHub. In this example, we’re going to build a site to host notes on talks. Static site generators let you break up your website into templates and pages that are compiled together to create a static HTML website. Middleman is a static site generator, similar to Jekyll (which powers GitHub Pages) or Stasis. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |