NetLife Guru

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:

This way, you can maintain a clean, organized template structure, allowing for precise control over the rendered output on your web pages.