Class: Geb::Template

Inherits:
Object
  • Object
show all
Defined in:
lib/geb/template.rb

Defined Under Namespace

Classes: TemplateFileNotFound, TemplateFileReadFailure

Constant Summary collapse

TEMPLATE_PATTERN =

template, section and insert tag match patterns

/<% template: (.*?) %>/
SECTION_PATTERN =
/<% start: (.*?) %>(.*?)<% end: \1 %>/m
INSERT_PATTERN =
/<%= insert: (.*?) %>/
@@loaded_templates =

define a class level cache for loaded template objects

{}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(template_path) ⇒ Geb::Template

Template class constructor

Parameters:

  • template_path (String)

    the path to the template file

Raises:



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/geb/template.rb', line 118

def initialize(template_path)

  # set the specified template path and initialise the content
  @path = template_path
  @content = nil

  # check if the template file exists
  raise TemplateFileNotFound.new(template_path) unless template_file_exists?()

  Geb.log " - loading template: #{@path}"

  # read the template file, raise an error if the file could not be read
  begin
    @content = File.read(template_path)
  rescue => e
    raise TemplateFileReadFailure.new(e.message)
  end # begin

end

Instance Attribute Details

#contentString (readonly)

Returns the content of the template file.

Returns:

  • (String)

    the content of the template file



113
114
115
# File 'lib/geb/template.rb', line 113

def content
  @content
end

#pathString (readonly)

Returns the path to the template file.

Returns:

  • (String)

    the path to the template file



109
110
111
# File 'lib/geb/template.rb', line 109

def path
  @path
end

Class Method Details

.expire_cacheObject

class method to expire the template cache



40
41
42
# File 'lib/geb/template.rb', line 40

def self.expire_cache
  @@loaded_templates = {}
end

.extract_sections_for_template(page_content) ⇒ Hash

Note:

the function looks for tags like this <% start: header %> … <% end: header %> in the page content and returns whats in between

extract the sections for the template from the page content

Parameters:

  • page_content (String)

    the page content

Returns:

  • (Hash)

    the sections for the template, key is the section name, value is the section content



92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/geb/template.rb', line 92

def self.extract_sections_for_template(page_content)

  # initialise the sections for the template hash
  sections_for_template = {}

  # scan the page content for sections and add them to the hash
  page_content.scan(SECTION_PATTERN).each do |section|
    sections_for_template[section[0].strip] = section[1].strip
  end # scan

  # return the sections for the template
  return sections_for_template

end

.extract_template_path(page_content) ⇒ String

Note:

the function looks for tags like this <% template: shared/templates/_site.html %> in the page content

extract the template path from the page content

Parameters:

  • page_content (String)

    the page content

Returns:

  • (String)

    the template path, or nil if no template path is found



78
79
80
81
82
83
84
85
86
# File 'lib/geb/template.rb', line 78

def self.extract_template_path(page_content)

  # match the template pattern and return the template path
  match = page_content.match(TEMPLATE_PATTERN)

  # return the template path or nil if no match
  return match ? match[1].strip : nil

end

.load(template_path) ⇒ Geb::Template

class method to initialise a template if it is not already loaded, otherwise, return the cached template

Parameters:

  • template_path (String)

    the path to the template file

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/geb/template.rb', line 47

def self.load(template_path)

  # initialise a return template object
  return_template = nil

  # check if the template is already loaded
  if @@loaded_templates.key?(template_path)

    # return the cached template
    Geb.log " - using cached template: #{template_path}"
    return_template = @@loaded_templates[template_path]

  else

    # create a new template object
    return_template = Template.new(template_path)

    # add the template to the cache
    @@loaded_templates[template_path] = return_template

  end # if else

  # return the new template object
  return return_template

end

Instance Method Details

#parse(page_content_sections) ⇒ String

Note:

the function looks for tags like this <%= insert: header %> in the template content

parse the page content sections and replace the sections in the template with the page section content

Parameters:

  • page_content_sections (Hash)

    the page content sections, key is the section name, value is the section content

Returns:

  • (String)

    the parsed template content



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/geb/template.rb', line 142

def parse(page_content_sections)

  # create a duplicate of the template content
  return_content = @content.dup

  # step through the page content sections and replace the insert sections in the template with the page content
  return_content.gsub!(INSERT_PATTERN) do |match|
    section_name = match.match(INSERT_PATTERN)[1].strip
    page_content_sections[section_name] || match
  end # return_content.gsub!

  # return the content
  return return_content

end