| Class | TemplateController |
| In: |
app/controllers/template_controller.rb
|
| Parent: | ApplicationController |
Controller for interaction with the template data model.
| content | [RW] | Accessor methods. |
| host | [RW] | Accessor methods. |
| host_last_modified | [RW] | Accessor methods. |
| index_html | [RW] | Accessor methods. |
| output | [RW] | Accessor methods. |
| output_cache | [RW] | Accessor methods. |
| page | [RW] | Accessor methods. |
| parse_depth | [RW] | Accessor methods. |
| variables | [RW] | Accessor methods. |
Ersatz constructor.
# File app/controllers/template_controller.rb, line 196
196: def self.create
197: template = self.new
198:
199: # Construct a content object.
200: template.content = ContentController.create
201:
202: # String containing URL content index mapping HTML.
203: template.index_html = ''
204:
205: # The final, fully parsed page frame.
206: template.output = nil
207:
208: # Project page object, containing all data required to generate the
209: # @output instance variable.
210: template.page = nil
211:
212: # The number of times the method parse() has recursed into itself.
213: template.parse_depth = 0
214:
215: # 'template_variables' database table values, indexed by name.
216: template.variables = {}
217:
218: template
219: end
@output processing.
Retrieve a parsed project page.
# File app/controllers/template_controller.rb, line 22
22: def get (
23: host,
24: build_start,
25: printer_friendly
26: )
27: @host = host
28: @page = host.page
29: populate_template_variables
30:
31:
32: # All error pages display the same result.
33: if (DEFAULT_ERROR_PAGE_ID == @page.id)
34: cache_id = @host.name + ':DEFAULT_ERROR_PAGE'
35: # Construct a unique cache identifier for this page based on the current
36: # URL path.
37: #
38: # Page cache data ids are created using the authoritative host name for a
39: # project host, @host.name, as opposed to using the requested host name
40: # from request.env{}, in order to ensure that the same cache data ids
41: # can be used across all development tiers - i.e. cache data generated
42: # using the host name qa.host.com could not be used for www.host.com.
43: else
44: cache_id = @host.name + @page.path_info
45: cache_id << '?print' if (printer_friendly)
46: end
47:
48:
49: # Check the cache for the page specified by the cache id.
50: cache_data = ApplicationCache::get(cache_id)
51:
52:
53: # If this page is a content index:
54: # * extract and compose content index information using the requested URL
55: # * use the content index information to construct a content index cache id
56: # * check if the requested page is cached using this content index cache id
57: if (@page.content_index)
58: # Content index pages containing multiple items will be cached using
59: # the URL path, so if this page is a content index it may already be
60: # cached using a path based cache id.
61: if (!cache_data)
62: # Populate content index data.
63: @content.url_parse_reference(@page.project_id,
64: @page.host_id,
65: @page.path_info,
66: @page.content_type_name)
67:
68: # When viewing single item content index pages, multiple URL formats
69: # may be used to access the exact same page. For example, the following
70: # URLs are equivalent:
71: # * http://risto.net/blog/title/Mea-Culpa
72: # * http://risto.net/blog/title/Mea-Culpa/168
73: # * http://risto.net/blog/date/2008/3/13/Mea-Culpa
74: #
75: # In order to ensure that multiple cache pages are not created for one
76: # content item (one per URL content mapping), content index pages for
77: # single items must use a cache id that is based on the content item
78: # type and content item id.
79: if (1 == @content.index['map'].size)
80: cache_id = CONTENT_INDEX_CACHE_ID.parse_data(@host.name,
81: @content.index['type'].id.to_s,
82: @content.index['type'].items.keys.to_s)
83: cache_id << '?print' if (printer_friendly)
84: cache_data = ApplicationCache::get(cache_id)
85: end
86: end
87:
88: # Use the default content index template for this host.
89: template_id = o('content:index:page_template_id')
90: else
91: # Use the template for this page.
92: template_id = @page.template_id
93: end
94:
95:
96: # Check the memory cache (global variable) for this page, and if it is
97: # found then use it to initialize the page and the page cache.
98: if (cache_data)
99: @output = cache_data
100: @output_cache = cache_data
101: # The page was not in the memory cache, so obtain it from the database, and
102: # initialize the page cache.
103: else
104: @output = Template.get(@page.host_id, template_id)
105: @output_cache = FALSE
106: end
107:
108:
109: # Parse the page if:
110: # * the page contains references
111: # * the page is not cached
112: if (reference_exists(REFERENCE, @output) || !@output_cache)
113: # Load template utilities that are specific to this project.
114: load_project_library(@page.host_id, TEMPLATE_UTILITY_LIB)
115:
116: # Actions to take for non-cached pages.
117: if (!@output_cache)
118: # In the case that the current page is using the master template for
119: # this project host, then parse the current template into it's
120: # master template, which in turn will be parsed and returned as the
121: # current page.
122: parse_page_into_master(printer_friendly)
123:
124: # Initialize the cached version of this page.
125: @output_cache = @output.to_s
126: end
127:
128: # Parse the template.
129: parse
130:
131: # Remove any remaining boundary tags.
132: purge_boundary_tags
133:
134: # Remove unnecessary space and newline characters.
135: purge_whitespace
136:
137: # Cache this page if:
138: # * it is not already cached
139: # * page level caching is enabled in the application
140: # * the requested page itself has caching enabled
141: if (!cache_data && o('application:cache:page') &&
142: 'TRUE' == @page.allow_caching)
143: ApplicationCache::set(cache_id, @output_cache)
144: end
145: end
146:
147:
148: # Insert the server page comment. This operation is performed
149: # last in order to obtain the most accurate build time information.
150: parse_server_comment(build_start)
151: end
Retrieve and construct template variables for this page.
Note that additional template variables may be populated elsewhere.
# File app/controllers/template_controller.rb, line 159
159: def populate_template_variables
160: @variables = TemplateVariable.get(@page.project_id,
161: @page.host_id,
162: @page.id,
163: @page.path_info)
164:
165: # If a CSS file exists for this page, then populate a template variable
166: # containing a CSS import directive for that file.
167: project_page_css = PROJECT_PAGE_CSS.parse_data(@page.id)
168: if File.readable?(PROJECT_WWW.parse_data(@page.host_id) + project_page_css)
169: TemplateVariable.set('css:project_page',
170: v('css:import_css').parse_data(project_page_css))
171: end
172:
173: # The current year.
174: TemplateVariable.set('text:year', Time.now.strftime('%Y'))
175: end