Class: Wytch::Page

Inherits:
Object
  • Object
show all
Defined in:
lib/wytch/page.rb

Overview

Represents a single page in a Wytch site.

Pages are defined by Ruby content files in the content directory. Each content file is evaluated in the context of a Page instance, allowing it to set metadata and configure the view class.

Examples:

A typical content file (content/about.rb)

view MySite::AboutView
add MySite::PageHelpers

[:title] = "About Us"
[:description] = "Learn more about our company"

Accessing page data in a view

class MySite::AboutView < Phlex::HTML
  def initialize(page)
    @page = page
  end

  def view_template
    h1 { @page.[:title] }
  end
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file_path:) ⇒ Page

Creates a new page by loading and evaluating a content file.

Parameters:

  • file_path (String)

    path to the content file, relative to the content directory



31
32
33
34
35
36
37
38
39
# File 'lib/wytch/page.rb', line 31

def initialize(file_path:)
  @file_path = file_path
   = {}
  @view_class = nil

  source_path = File.join(Wytch.site.content_dir, @file_path)

  instance_eval File.read(source_path), source_path
end

Instance Attribute Details

#metadataHash (readonly)

Returns arbitrary metadata set by the content file.

Returns:

  • (Hash)

    arbitrary metadata set by the content file



42
43
44
# File 'lib/wytch/page.rb', line 42

def 
  
end

Instance Method Details

#add(helper_module) ⇒ void

This method returns an undefined value.

Extends this page instance with a helper module. Called from content files to add helper methods.

Examples:

add MySite::PostHelpers

Parameters:

  • helper_module (Module)

    the module to extend this page with



88
89
90
# File 'lib/wytch/page.rb', line 88

def add(helper_module)
  extend helper_module
end

#build_pathString

Returns the output file path for the built page.

Returns:

  • (String)

    the build path (e.g., "about/index.html")



72
73
74
75
76
77
78
# File 'lib/wytch/page.rb', line 72

def build_path
  if virtual_path == "index"
    "index.html"
  else
    "#{virtual_path}/index.html"
  end
end

#change_frequencyString?

The change frequency hint for sitemap generation. Override in subclasses to provide hints like "daily", "weekly", etc.

Returns:

  • (String, nil)

    the change frequency, or nil if unknown



124
125
126
# File 'lib/wytch/page.rb', line 124

def change_frequency
  nil
end

#include_in_sitemap?Boolean

Whether this page should be included in the sitemap. Override in subclasses to exclude pages.

Returns:

  • (Boolean)

    true by default



108
109
110
# File 'lib/wytch/page.rb', line 108

def include_in_sitemap?
  true
end

#last_modifiedDate, ...

The last modification date for sitemap generation. Override in subclasses to provide actual dates.

Returns:

  • (Date, Time, nil)

    the last modified date, or nil if unknown



116
117
118
# File 'lib/wytch/page.rb', line 116

def last_modified
  nil
end

#pathString

Returns the URL path for this page.

Returns:

  • (String)

    the URL path (e.g., "/" for index, "/about" for about.rb)



54
55
56
57
58
59
60
# File 'lib/wytch/page.rb', line 54

def path
  if virtual_path == "index"
    "/"
  else
    "/#{virtual_path}"
  end
end

#priorityFloat?

The priority hint for sitemap generation. Override in subclasses to provide a value between 0.0 and 1.0.

Returns:

  • (Float, nil)

    the priority, or nil if unknown



132
133
134
# File 'lib/wytch/page.rb', line 132

def priority
  nil
end

#renderString

Renders the page using its configured view class.

Returns:

  • (String)

    the rendered HTML



47
48
49
# File 'lib/wytch/page.rb', line 47

def render
  @view_class.new(self).call
end

#view(view_class) ⇒ void

This method returns an undefined value.

Sets the view class for rendering this page. Called from content files to specify which Phlex view to use.

Examples:

view MySite::PostView

Parameters:

  • view_class (Class)

    a Phlex view class that accepts the page in its initializer



100
101
102
# File 'lib/wytch/page.rb', line 100

def view(view_class)
  @view_class = view_class
end

#virtual_pathString

Returns the virtual path derived from the file path.

Returns:

  • (String)

    the path without directory prefix or .rb suffix



65
66
67
# File 'lib/wytch/page.rb', line 65

def virtual_path
  @file_path.delete_prefix("#{Wytch.site.content_dir}/").delete_suffix(".rb")
end