Class: Geb::Template
- Inherits:
-
Object
- Object
- Geb::Template
- 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
-
#content ⇒ String
readonly
The content of the template file.
-
#path ⇒ String
readonly
The path to the template file.
Class Method Summary collapse
-
.expire_cache ⇒ Object
class method to expire the template cache.
-
.extract_sections_for_template(page_content) ⇒ Hash
extract the sections for the template from the page content.
-
.extract_template_path(page_content) ⇒ String
extract the template path from the page content.
-
.load(template_path) ⇒ Geb::Template
class method to initialise a template if it is not already loaded, otherwise, return the cached template.
Instance Method Summary collapse
-
#initialize(template_path) ⇒ Geb::Template
constructor
Template class constructor.
-
#parse(page_content_sections) ⇒ String
parse the page content sections and replace the sections in the template with the page section content.
Constructor Details
#initialize(template_path) ⇒ Geb::Template
Template class constructor
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.) end # begin end |
Instance Attribute Details
#content ⇒ String (readonly)
Returns the content of the template file.
113 114 115 |
# File 'lib/geb/template.rb', line 113 def content @content end |
#path ⇒ String (readonly)
Returns the path to the template file.
109 110 111 |
# File 'lib/geb/template.rb', line 109 def path @path end |
Class Method Details
.expire_cache ⇒ Object
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
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
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
the function looks for tags like this <% template: shared/templates/_site.html %> in the page content
extract the template path from the page content
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
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
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
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 |