The Template Engine¶
At a basic level, Templates contain almost everything you want displayed on the front-end of the site. When ExpressionEngine serves up each Template, the system must go through a rendering process to parse out the Tags, Variables, Snippets, and Embeds being used in the Template.
Note
The information in this article applies only to rendering Templates. It doesn’t apply to rendering theme files such as those used for Member Profiles, the Wiki module, or the Discussion Forum module.
ExpressionEngine goes through several stages to fully process each Template, and this article exposes the order of those rendering stages. Understanding how the system renders a template can help immensely when building pages and troubleshooting problems.
The use of Conditional Variables provides a great example here. Simple conditionals are parsed before module tags, but advanced conditionals are parsed after module tags. If a simple conditional evaluates false, ExpressionEngine simply ignores a module tag within that conditional, neither rendering it nor displaying it. But if an advanced conditional evaluates false, ExpressionEngine can only hide the output of the already fully rendered module tag. That amounts to a big difference in performance.
Rendering Stages¶
The Template Engine processes the selected template fully from top to bottom through each rendering stage.
- Determine template to process based on request URI
- Get template from database, check template access permissions, and increment the hit counter
- If it exists, get template from file
- If template type is static, return template and end parsing
- Parse (as a group, so order is irrelevant):
- {freelancer_version}
- Snippets
- MSM variables: {site_id}, {site_label}, {site_shortname}
- {last_segment}
- Parse segment variables
- Parse embed variables
- Parse date formatting string constants
- Parse {template_edit_date}
- Parse {current_time}
- If present, get cached template, then skip to the advanced conditionals parsing stage
- Parse PHP on Input
- Parse simple conditionals: segment, embed, global variables
- Assign and parse preload_replace variables
- Parse module and plugin tags
- See notes on how nested plugins are parsed.
- If any module’s {if no_results} tag pair evaluates true, a {redirect} variable within the tag pair will be processed immediately.
- Parse PHP on Output
- Write template to cache file
- Parse advanced conditionals
- Process embedded templates
- Process redirect variable
- Parse user-defined global variables
- Parse some standard global variables (separately, in order given):
- {hits}
- {ip_address}
- {ip_hostname}
- {homepage}
- {cp_url}
- {site_name}
- {site_url}
- {site_index}
- {webmaster_email}
- {stylesheet}
- {encode}
- {debug_mode}
- {gzip_mode}
- {app_version}
- {version}
- {app_build}
- {build}
- {charset}
- {lang}
- {doc_url}
- {theme_folder_url}
- {member_profile_link}
- {captcha}
- Add security hashes to forms and parse {XID_HASH}
- Parse remaining standard global variables (separately, in order given):
- {member_id}
- {group_id}
- {group_description}
- {group_title}
- {member_group}
- {username}
- {screen_name}
- {email}
- {ip_address}
- {location}
- {total_entries}
- {total_comments}
- {private_messages}
- {total_forum_posts}
- {total_forum_topics}
- {total_forum_replies}
- Parse alternative syntax forms of the member variables above
- Parse path variables