PHP code example of knifelemon / comment-template

1. Go to this page and download the library: Download knifelemon/comment-template library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

knifelemon / comment-template example snippets



KnifeLemon\CommentTemplate\Engine;

// Initialize template engine
$template = new Engine();
$template->setPublicPath(__DIR__);        // Root directory (where index.php is)
$template->setSkinPath('templates');      // Relative to public path
$template->setAssetPath('assets');        // Relative to public path
$template->setFileExtension('.php');
$template->setCacheBusting(true);         // Append ?timestamp to asset URLs

// Render template
$template->render('homepage', [
    'title' => 'Welcome',
    'content' => 'Hello World!'
]);


KnifeLemon\CommentTemplate\Engine;

$app = Flight::app();

$app->register('view', Engine::class, [], function (Engine $engine) {
    $engine->setPublicPath(__DIR__);           // Root directory (where index.php is)
    $engine->setSkinPath('views');             // Relative to public path
    $engine->setAssetPath('assets');           // Relative to public path
    $engine->setFileExtension('.php');
    $engine->setCacheBusting(true);            // Enable cache busting
});

$app->map('render', function(string $template, array $data) use ($app): void {
    echo $app->view()->render($template, $data);
});


KnifeLemon\CommentTemplate\Engine;

$app = Flight::app();

$app->register('view', Engine::class, [
    __DIR__,                // Public path (root directory where index.php is)
    'views',                // Templates path (relative to public path) 
    'assets',               // Asset path (relative to public path)
    '.php',                 // File extension
    true                    // Enable cache busting
]);

$app->map('render', function(string $template, array $data) use ($app): void {
    echo $app->view()->render($template, $data);
});

// Via constructor (5th parameter)
$template = new Engine(__DIR__, 'views', 'assets', '.php', true);

// Via setter
$template->setCacheBusting(true);

// Example: if your index.php is at /var/www/html/myapp/index.php
$template->setPublicPath('/var/www/html/myapp');  // Root directory

// Windows example: if your index.php is at C:\xampp\htdocs\myapp\index.php
$template->setPublicPath('C:\\xampp\\htdocs\\myapp');

$template = new Engine();
$template->setPublicPath('/var/www/html/myapp');  // Root directory (where index.php is)

// Relative paths - automatically combined with public path
$template->setSkinPath('views');           // → /var/www/html/myapp/views/
$template->setSkinPath('templates/pages'); // → /var/www/html/myapp/templates/pages/

// Absolute paths - used as-is (Unix/Linux)
$template->setSkinPath('/var/www/templates');      // → /var/www/templates/
$template->setSkinPath('/full/path/to/templates'); // → /full/path/to/templates/

// Windows absolute paths
$template->setSkinPath('C:\\www\\templates');     // → C:\www\templates\
$template->setSkinPath('D:/projects/templates');  // → D:/projects/templates/

// UNC paths (Windows network shares)
$template->setSkinPath('\\\\server\\share\\templates'); // → \\server\share\templates\

// Relative paths - automatically combined with public path
$template->setAssetPath('assets');        // → /var/www/html/myapp/assets/
$template->setAssetPath('static/files');  // → /var/www/html/myapp/static/files/

// Absolute paths - used as-is (Unix/Linux)
$template->setAssetPath('/var/www/cdn');           // → /var/www/cdn/
$template->setAssetPath('/full/path/to/assets');   // → /full/path/to/assets/

// Windows absolute paths
$template->setAssetPath('C:\\www\\static');       // → C:\www\static\
$template->setAssetPath('D:/projects/assets');    // → D:/projects/assets/

// UNC paths (Windows network shares)
$template->setAssetPath('\\\\server\\share\\assets'); // → \\server\share\assets\

public function __construct(string $publicPath = "", string $skinPath = "", string $assetPath = "", string $fileExtension = "", bool $cacheBusting = false)


use KnifeLemon\CommentTemplate\Engine;
use Tracy\Debugger;

// Enable Tracy (must be called before any output)
Debugger::enable(Debugger::DEVELOPMENT);

// Use CommentTemplate as normal - logging happens automatically
$template = new Engine();
$template->setPublicPath(__DIR__);
$template->setSkinPath('templates');
$template->setAssetPath('assets');

$template->render('homepage', ['title' => 'Hello World']);
html
<!--@layout(layout)-->
<h1>{$title}</h1>
<p>{$content}</p>
html
<!-- Inline small images as data URIs for faster loading -->
<img src="<!--@base64(images/logo.png)-->" alt="Logo">
<div style="background-image: url('<!--@base64(icons/star.svg)-->');">
    Small icon as background
</div>
html
<!-- Execute PHP functions and constants -->
<div>Current time: <!--@echo(date('Y-m-d H:i:s'))--></div>
<div>PHP Version: <!--@echo(PHP_VERSION)--></div>
<div>App Version: <!--@echo(APP_VERSION)--></div>
javascript
// Generate dynamic URLs with Flight framework
location.href = '<!--@echo(Flight::getUrl('login'))-->';
const apiUrl = '<!--@echo(Flight::getUrl('api'))-->';
const dashboardUrl = '<!--@echo(Flight::getUrl('dashboard'))-->';

// Access constants and configuration
const DEBUG_MODE = <!--@echo(DEBUG ? 'true' : 'false')-->;
const MAX_UPLOAD = <!--@echo(MAX_FILE_SIZE)-->;
css
/* Dynamic theme values */
.theme {
    --primary-color: <!--@echo($theme['primary'] ?? '#007bff')-->;
    --font-url: url('<!--@echo(CDN_URL . '/fonts/custom.woff2')-->');
}

.logo {
    background: url('<!--@echo(Flight::getUrl('static') . '/logo.png')-->');
}