Sha256: 921b3abb688c6285e417dd076c8fa3343e5154c461680001de5506ee15173a03

Contents?: true

Size: 1.99 KB

Versions: 9

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

module Nanoc::Helpers
  # @see https://nanoc.ws/doc/reference/helpers/#rendering
  module Rendering
    include Nanoc::Helpers::Capturing

    # @param [String] identifier
    # @param [Hash] other_assigns
    #
    # @raise [Nanoc::Int::Errors::UnknownLayout]
    # @raise [Nanoc::Int::Errors::CannotDetermineFilter]
    # @raise [Nanoc::Filter::UnknownFilter]
    #
    # @return [String, nil]
    def render(identifier, other_assigns = {}, &block)
      # Find layout
      layout_view = @layouts[identifier]
      layout_view ||= @layouts[identifier.__nanoc_cleaned_identifier]
      raise Nanoc::Int::Errors::UnknownLayout.new(identifier) if layout_view.nil?

      layout = layout_view._unwrap

      # Visit
      dependency_tracker = @config._context.dependency_tracker
      dependency_tracker.bounce(layout, raw_content: true)

      # Capture content, if any
      captured_content = block_given? ? capture(&block) : nil

      # Get assigns
      assigns = {
        content: captured_content,
        item: @item,
        item_rep: @item_rep,
        items: @items,
        layout: layout_view,
        layouts: @layouts,
        config: @config,
      }.merge(other_assigns)

      # Get filter name
      filter_name, filter_args = *@config._context.compilation_context.filter_name_and_args_for_layout(layout)
      raise Nanoc::Int::Errors::CannotDetermineFilter.new(layout.identifier) if filter_name.nil?

      # Get filter class
      filter_class = Nanoc::Filter.named!(filter_name)

      # Create filter
      filter = filter_class.new(assigns)

      # Layout
      content = layout.content
      arg = content.binary? ? content.filename : content.string
      result = filter.setup_and_run(arg, filter_args)

      # Append to erbout if we have a block
      if block_given?
        # Append result and return nothing
        erbout = eval('_erbout', block.binding)
        erbout << result
        ''
      else
        # Return result
        result
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
nanoc-4.11.11 lib/nanoc/helpers/rendering.rb
nanoc-4.11.10 lib/nanoc/helpers/rendering.rb
nanoc-4.11.9 lib/nanoc/helpers/rendering.rb
nanoc-4.11.8 lib/nanoc/helpers/rendering.rb
nanoc-4.11.7 lib/nanoc/helpers/rendering.rb
nanoc-4.11.6 lib/nanoc/helpers/rendering.rb
nanoc-4.11.5 lib/nanoc/helpers/rendering.rb
nanoc-4.11.4 lib/nanoc/helpers/rendering.rb
nanoc-4.11.3 lib/nanoc/helpers/rendering.rb