| Module | TemplateReference |
| In: |
app/controllers/template_reference.rb
|
Mixin module: Reference component, of the template controller.
Note that the web server must be restarted to recognize changes within this file.
Construct a database field template element open or close tag.
Database field template element open and close tags are used to demarcate the HTML surrounding a database field template element reference that belongs to the database field.
An open tag takes the following format:
A close tag takes the following format:
For example, if a content type database table contains the fields ‘title’ and ‘date’ then it‘s HTML template may look like this:
In this example, if the ‘title’ field contains the data ‘Number 1’ and the ‘date’ field contains the data ‘Nov 11’, then the template would be parsed and returned as follows:
If the ‘date’ field were empty when the database row is parsed into this template then all content encased within the open and close ‘date’ tags will be excluded from the output, thus the following would be returned:
# File app/controllers/template_reference.rb, line 33
33: def reference_assemble_boundary_tag (
34: *arguments
35: )
36: type = arguments.shift
37:
38: if ('open' == type)
39: template = REFERENCE_TAG_OPEN_REGEX
40: elsif ('close' == type)
41: template = REFERENCE_TAG_CLOSE_REGEX
42: else
43: return FALSE
44: end
45:
46: template.parse_data(reference_assemble(arguments.insert(0, DATABASE_FIELD)))
47: end
Determine if a template contains element references.
# File app/controllers/template_reference.rb, line 53
53: def reference_exists (
54: reference_type,
55: template
56: )
57: (template =~ /#{reference_regex(reference_type)}/) ? (TRUE) : (FALSE)
58: end
Obtain all template element references within a template.
# File app/controllers/template_reference.rb, line 64
64: def reference_extract (
65: reference_type,
66: template
67: )
68: references = []
69:
70: # Only process this template if it contains content references.
71: if (reference_exists(reference_type, template))
72: # Obtain all template references within this template, removing
73: # reference markers and storing only bare references in the result.
74: for reference in template.scan(/#{reference_regex(reference_type)}/)
75: reference = reference[0].to_s if ('array'==reference.get_variable_class)
76: references << reference.gsub(/[\{\}]/, '')
77: end
78: end
79:
80: references
81: end
Return the regular expression that defines the structure of a template element reference.
# File app/controllers/template_reference.rb, line 88
88: def reference_regex (
89: type
90: )
91: # The input must be examined using a case statement to inspect it's value
92: # in order to ensure that the input is valid, as opposed to simply
93: # appending '_REFERENCE' to the value of the 'type' input variable; we do
94: # not want to allow execution of this method with invalid input and return
95: # and invalid result. For example, the input 'no' should not return the
96: # result 'no_REFERENCE'.
97: case type
98: when CONSTANT
99: pattern = CONSTANT_REFERENCE
100: when CONTENT_ITEM
101: pattern = CONTENT_ITEM_REFERENCE
102: when CONTENT_GROUP
103: pattern = CONTENT_GROUP_REFERENCE
104: when DATABASE_FIELD
105: pattern = DATABASE_FIELD_REFERENCE
106: when LINK
107: pattern = LINK_REFERENCE
108: when METHOD
109: pattern = METHOD_REFERENCE
110: when PAGE_DATA
111: pattern = PAGE_DATA_REFERENCE
112: when TEMPLATE
113: pattern = TEMPLATE_REFERENCE
114: when VARIABLE
115: pattern = VARIABLE_REFERENCE
116: else
117: pattern = REFERENCE
118: end
119:
120: pattern
121: end
Extract all data from a template reference.
The format of a template reference is as follows:
Template elements are referenced within HTML by encapsulating them inside of brace parentheses, {}, for example:
Type indicates the class of information that will be parsed into a reference.
Available reference types are listed in: config/codeblue.rb
The return value of this method is dependent upon the type of template reference being processed:
The number of data fields used per type varies.
For a full description of all data fields employed by a particular reference type, refer to it‘s parsing method. For example, to learn more about the ‘i’ (content item) reference type refer to the documentation for parse_content_item_fields().
# File app/controllers/template_reference.rb, line 157
157: def reference_split (
158: reference
159: )
160: # Link reference's include string segments that may contain ':' characters.
161: # The ':' character is escaped in the string segments of link references so
162: # that reference_split() will know to ignore these characters, and that
163: # references should be split using only unescaped ':' characters. To
164: # achieve this, the string \: is converted into the text 'ESCAPED_COLON'
165: # prior to splitting a reference.
166: if (reference =~ /#{ESCAPED_COLON}/)
167: reference = reference.gsub(ESCAPED_COLON, 'ESCAPED_COLON')
168: end
169:
170:
171: # Extract all data segments from this template reference.
172: segments = reference.split(/:/)
173:
174:
175: # Extract the reference type from the data segments.
176: type = segments.shift
177:
178:
179: # Convert ':' characters that were escaped into unescaped ':' characters.
180: data = []
181: for s in segments
182: s = s.gsub('ESCAPED_COLON', ':') if (s =~ /ESCAPED_COLON/)
183: data << s
184: end
185:
186:
187: # Proceed according to the reference type.
188: case type
189: # Content references are expected to contain a specific number of
190: # elements; FALSE elements are appended to undersized arrays.
191: when CONTENT_ITEM, CONTENT_GROUP
192: data << FALSE while (data.size < SEGMENTS_IN_CONTENT_REFERENCE)
193:
194: # Link references are expected to contain a specific number of
195: # elements; FALSE elements are appended to undersized arrays.
196: when LINK
197: data << FALSE while (data.size < SEGMENTS_IN_LINK_REFERENCE)
198:
199: # Ensure that integer elements are of data type Fixnum.
200: when TEMPLATE
201: data = data[0].to_i
202:
203: # Construct the (string) index for template variables.
204: when VARIABLE
205: data = data[0] + ':' + data[1]
206: end
207:
208:
209: data
210: end
Extract all data from a database field boundary tag reference.
The format of a database field boundary tag reference is as follows:
<replacement template id> is optional.
In the case that a database field reference yields no value and the <replacement template id> is populated, then all HTML encased within this boundary tag will be replaced with the template specified by <replacement template id>.
These elements are referenced within HTML by encapsulating them inside of angle parentheses, <>, for example:
# File app/controllers/template_reference.rb, line 232
232: def reference_split_boundary_tag (
233: reference
234: )
235: # Remove opening and closing angle brackets, and extract the individual
236: # segments from this boundary tag.
237: subs = []
238: subs << ['^\<', '']
239: subs << ['\>$', '']
240: data = reference.gsubs(subs).split(/:/)
241:
242: # Link references are expected to contain a specific number of
243: # elements; FALSE elements are appended to undersized arrays.
244: data << FALSE if (data.size < SEGMENTS_IN_BOUNDARY_TAG)
245:
246: data
247: end