NetLife Guru

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.