Block Based template engine
Before diving into specifics, it's essential to understand that this template engine operates on a "block-based" architecture. Unlike traditional file-based template systems, where each template corresponds to a complete file, a block-based system segments the templates into various blocks within the same file. These blocks can be independently assembled and reused, offering greater flexibility and modularity. This approach is particularly advantageous for creating complex layouts and components that can be easily managed and integrated into different parts of a web application. The following examples will illustrate how this block-based system works in practice.
Components
In this scenario, the template engine differs from traditional file-based systems by adopting a "block-based" structure. In this structure, HTML or tpl files consist of various blocks that can be independently combined. For instance:
In `index.html`:
{header}
<h1>This is the header</h1>
{/header}
{content}
<p>Hello World</p>
{/content}
{layout}
<html>
<header>
...
</header>
<body>
include{header}
This is the layout
include{content}
include{content}
include{content}
</body>
</html>
{/layout}
The output block is chosen as 'layout' because it is called in the function:
print $templateEngine->render("layout");
However, any block can be invoked like this, potentially to return a snippet for AJAX applications. This flexible system allows for more modular and reusable code, particularly useful for web coders who later hand off templates to developers.
Multiple templates
When the template engine processes multiple HTML or template files, it's important to be aware of how it handles blocks with the same name. If different files contain blocks with identical names, these blocks will be merged into a single unit in the output. For example, consider the following scenario with two different files:
articles.html contains:
{content}
<h2>Articles</h2>
{/content}
products.html contains:
{content}
<h2>Products</h2>
{/content}
In this case, the `content block` from both files will be combined into one, which might lead to unexpected results.
To prevent such issues and maintain control over the layout, it's crucial to:
- Use unique names for blocks across different template files to avoid unintended merging.
- Carefully manage the files you loadinto the template engine, ensuring only the necessary templates for a particular context or page are included.
This way, you can maintain a clean, organized template structure, allowing for precise control over the rendered output on your web pages.
- Installation - Installing Aurora Template
- Usage - Basic Usage of the PHP NLG Template Engine
- Storage-Setting Directories for Templates and Cache
- Templates - Setting Up Input Templates
- Variables - Setting Variables in the System
- Language Constants- Setting Language Constants
- Cache - Managing Cache Creation
- Router - Standalone Mode with Routing System
- Using the PHP Template Engine
- Blocks - Block Based template engine
- Using variables - How to use variables in templates
- Calculations - Isolation and Sharing Variables
- Include and Import - include vs. import in the Template Engine
- Import - Leveraging `import{}` for Standalone Applications with a Routing System
- Resources - Adding Resources to Templates
- Control Structures - `if`, `else if`, `else`
- Ternary Operator - How to approach to ternary operator
- Macros - Macros in Template Engines - Understanding the Concept
- Custom Macros - Creating Custom Macros in Template Engines
- Forms - Creating Forms in the Template Engine
- Custom forms - Customizing Forms in the Template Engine
- Custom PHP Forms - PHP Developer's Guide to Customizing Form Templates in the Template Engine
- Curly Brackets - Handling Curly Brackets in Text Content
- Error Handling - Error Handling in PHP Template Engine
- Tests