ViewFish PHP Templating Engine

Methods Reference

A quick reference for all public methods on the ViewFish object.

Core Methods

Method Description
$t->set_template_path($path) Set the directory where templates are stored
$t->load_template($name) Load a template file (returns content string or false)
$t->render($template, $data) Render a template string with a data array
$t->get_discovered() Get variables found during the last render pass

Function Management

Method Description
$t->extend(['fn1', 'fn2']) Add functions to the allowed list
$t->unextend() Reset to default allowed functions only
$t->load_supported_functions() Load all known safe PHP string functions
// Add custom functions
$t->extend(['custom_format', 'my_helper']);

// Use in templates: {{name|custom_format}}

// Reset to defaults
$t->unextend();

// Load everything safe
$t->load_supported_functions();

Cache Methods

Method Description
$t->enable_cache($mc, $ttl) Enable Memcache/Memcached caching
$t->uncache($tmpl) Remove an uncompiled template from cache
$t->cache_compiled(true/false) Toggle compiled template caching
$t->cache_create($tmpl, $text, $ttl) Manually store a compiled template
$t->cache_read($tmpl) Retrieve a compiled template from cache
$t->cache_destroy($tmpl) Delete a compiled template from cache

Configuration

Method Description
$t->replace_empty(true/false) Toggle stripping of unmatched placeholders

Typical Workflow

// 1. Create the object
$t = new ViewFish\viewfish();

// 2. Point to your templates
$t->set_template_path('/path/to/templates/');

// 3. Optionally extend with custom functions
function my_currency_format(string $price): string {
    return "$" . number_format((float) $price, 2);
}
$t->extend(['my_currency_format']);

// 4. Load a template
$template = $t->load_template('invoice');

// 5. Render with data
$html = $t->render($template, [
    'customer' => 'Acme Corp',
    'total'    => '1250',
]);

echo $html;