Include vs. Import in the Template Engine
In our template engine, you have two distinct ways to integrate content from one block into another: include and import. Understanding the difference between these two is crucial for effectively managing the scope of variables and calculations.
Using `include`
The include statement is used to incorporate the content of one block into another without affecting the scope of variables.
When a block is included, any calculations or variable modifications in that block do not impact the variables in the block where it’s included.
This ensures that each block maintains its own set of variables, preventing unintended side effects.
Example include
{calculate}
{$a += 1}
{$a} // Output is 2
{/calculate}
{layout}
{$a = 1}
include{calculate}
{$a} // Output remains 1, as {calculate} is included without affecting scope
{/layout}
Using `import`
The import statement, in contrast, extends the scope of the imported block to the block where it’s used.
This means that any variable changes or calculations in the imported block directly affect the variables in the importing block.
Use import when you want the actions within one block to have a direct impact on another.
Example import
{calculate}
{$a += 1}
{$a} // Output is 2
{/calculate}
{layout}
{$a = 1}
import{calculate}
{$a} // Output changes to 2, as {calculate} affects {layout} through import
{/layout}
In summary, while include preserves the isolation of variable scopes between blocks, import merges these scopes, allowing for the propagation of variable states and calculations. This feature provides a high degree of flexibility and control over the behavior of your templates, enabling complex interactions while maintaining clarity and manageability.
- 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