Class: Lifer::Builder::HTML

Inherits:
Lifer::Builder show all
Defined in:
lib/lifer/builder/html.rb,
lib/lifer/builder/html/from_any.rb,
lib/lifer/builder/html/from_erb.rb,
lib/lifer/builder/html/from_liquid.rb

Overview

This builder makes HTML documents out of any entry type that responds to ‘#to_html` and writes them to the configured Lifer output directory.

The HTML builder depends on the collection’s layout file. Layout files can be ERB or Liquid template files. The layout file yields entry contents via a ‘content` call that is parsed by ERB or Liquid.

It is normal to have templates that render other templates. So normal, in fact, that Liquid provides a built-in ‘render` function that allows us to render partials from other Liquid files:

{% render "path/to/my/partial.html.liquid" with local_variable: "value" %}

And we’ve ensured a similar ‘render` function exists for our ERB HTML builder:

<%= render "path/to/my/partial.html.erb", local_variable: "value" %>

The entry being built will include contextual data about the current project, so that the user can build out normal website features like navigation links, indexes, and so on. Note that with the current Liquid implementation, you may need to explicitly pass context from layouts to the partials that require it via ‘with`. Meanwhile, the ERB implementation provides a bunch of context globally. Context includes:

- `my_collection_name`: Or, any collection by name.

  For example, you can iterate over the entries of any named collection by
  accessing the collection like this:

      my_collection.entries

- `settings`: Serialized Lifer settings from the configuration file.

- `collections`: A list of collections.

- `content`: The content of the current entry.

The ‘:content` variable is especially powerful, as it also parses any given entry that’s an ERB file with the same local variables in context.

[1]: docs.ruby-lang.org/en/3.3/ERB.html [2]: shopify.github.io/liquid/

Defined Under Namespace

Classes: FromAny, FromERB, FromLiquid

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Lifer::Builder

build!, prebuild!

Class Method Details

.execute(root:) ⇒ void

This method returns an undefined value.

Traverses and renders each entry for each collection in the configured output directory for the Lifer project.

Parameters:

  • root (String)

    The Lifer root.



59
60
61
62
63
# File 'lib/lifer/builder/html.rb', line 59

def execute(root:)
  Dir.chdir Lifer.output_directory do
    new(root: root).execute
  end
end

Instance Method Details

#executevoid

This method returns an undefined value.

Traverses and renders each entry for each collection.



69
70
71
72
73
74
# File 'lib/lifer/builder/html.rb', line 69

def execute
  Lifer.collections(without_selections: true).each do |collection|
    generate_output_directories_for collection
    generate_output_entries_for collection
  end
end