Current Location: Home> Latest Articles> Use the init function to combine the template engine for view initialization

Use the init function to combine the template engine for view initialization

gitbox 2025-05-28

In PHP development, the template engine is an important tool for separating views and business logic, which can make the code more concise and convenient to maintain. In order to efficiently initialize the view, a rational design of an init function can greatly simplify the configuration and use of the template engine, avoid duplicate code, and improve overall development efficiency.

This article will use an example to explain how to combine init functions and template engines (taking Smarty as an example) to efficient view initialization.

1. Why do you need an init function?

When using the template engine, we usually need to perform a series of initialization operations, such as:

  • Instantiate template engine objects;

  • Set templates and compile directories;

  • Configure cache and debug options;

  • Pass in global variables or configuration information.

If these operations are repeated in each controller or view processing function, the code will appear redundant and difficult to maintain. An init function can encapsulate these common initialization work in a unified manner. In the future, just call init() to quickly obtain the configured template engine object.

2. Sample code: Initialize Smarty using the init function

 <?php
require_once 'libs/Smarty.class.php';

function init() {
    $smarty = new Smarty();

    // Setting template directory
    $smarty->setTemplateDir('/var/www/gitbox.net/templates/');
    
    // Set the compile directory
    $smarty->setCompileDir('/var/www/gitbox.net/templates_c/');
    
    // Set the cache directory
    $smarty->setCacheDir('/var/www/gitbox.net/cache/');
    
    // Setting the configuration directory
    $smarty->setConfigDir('/var/www/gitbox.net/configs/');
    
    // Turn on debug mode(Available in the development phase,Production environment recommended to close)
    $smarty->debugging = true;
    
    // Cache configuration
    $smarty->caching = Smarty::CACHING_LIFETIME_CURRENT;
    $smarty->cache_lifetime = 120;

    // Assign global variables,For example, site name
    $smarty->assign('site_name', 'GitBox Sample site');

    return $smarty;
}

// Example usage
$smarty = init();

// Assign template variables
$smarty->assign('page_title', 'Welcome GitBox!');
$smarty->assign('content', 'This is a use init Example template for function initialization。');

// Rendering template
$smarty->display('index.tpl');
?>

3. Key points description

  • Directory configuration : Note that the template, compilation, and cache directories must have corresponding read and write permissions, otherwise an error will be reported.

  • Global variables : Variables assigned in the init function (such as site_name ) will take effect in all templates, reducing duplicate allocations.

  • Scalability : You can load different configurations in the init function according to different environments (development, testing, production).

  • Improve efficiency : Once the init function is set, each controller needs to focus on business variables and template rendering in the future, greatly reducing duplicate work.