Chapter 15: Advanced Smarty Techniques & Performance

Introduction: Beyond the Basics

In Chapter 8, we covered the essentials of using Smarty to separate our presentation from our application logic. Now, we will explore the advanced features that transform Smarty from a simple template engine into a powerful tool for building flexible, high-performance, multi-site applications.

This chapter will detail the specific patterns we use to solve complex architectural challenges in our unified application. We will cover our template inheritance strategy that allows for a shared core of templates with site-specific overrides, and the dynamic include pattern that enables database-driven page composition.

These techniques are the key to managing a large and diverse set of views from a single codebase.


Template Inheritance: The Shared Core View

One of the most powerful features of our architecture is the ability to share a core set of templates across all websites, while still allowing any site to have its own unique, custom version of a template. This is achieved through template inheritance.

We accomplish this in the constructor of our custom View class by providing Smarty with an array of template directories. Smarty searches these directories in the order they are provided until it finds the requested file.

<?php
// In our custom View class constructor

// 1. Define the path to the current site's templates.
$siteTemplateDir = $_SERVER['TEMPLATE_FOLDER'] . '/templates/';

// 2. Define the path to the shared core templates.
$coreTemplateDir = CORE_TEMPLATE_DIRECTORY . '/templates/';

// 3. Set both directories. Smarty searches them in this order.
$this->setTemplateDir([
    $siteTemplateDir, // Look here first
    $coreTemplateDir  // Fall back to here
]);

When a model calls for a template like {include file=" header.tpl"}, Smarty first looks for the file in the site-specific directory. If it exists, that version is used. If not, Smarty automatically looks in the core directory.

Database-Driven Views with Dynamic includes

n our architecture, the MainDispatcher uses the httpRequests table to determine which models and views to load for a given URL. The key to making this work in the presentation layer is the dynamic construction of the template's filename from variables, which is then passed to Smarty's {include} function.

In our main body.tpl layout file, the main content area is rendered with this single, powerful line:

Code snippet

{* This include statement builds the filename from variables *}
{include file="`$view``$templateExtension`"}

Code Breakdown Backticks (`): In Smarty, backticks around a variable allow you to use it as part of a string for a parameter.

$view: This variable is set by the MainDispatcher and holds the name of the content template for the current page (e.g., advertsList).

$templateExtension: This is a global variable that holds the file extension, typically .tpl.

The result is that Smarty dynamically constructs the filename and includes it. This pattern is the final link in the chain, allowing our database to have complete control over the composition of any page on any site.

Shopping summary

Item

SubTotal: £

Empty Basket

this is a hidden panel

Completed in 47 milliseconds

Completed in 47 milliseconds

This is the top Panel

This is the bottom Panel