You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
77 lines
2.5 KiB
77 lines
2.5 KiB
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* @author Robert Strutts <Bob_586@Yahoo.com>
|
|
* @copyright (c) 2025, Robert Strutts
|
|
* @license MIT
|
|
*/
|
|
|
|
namespace CodeHydrater\services;
|
|
|
|
use CodeHydrater\bootstrap\registry as Reg;
|
|
use CodeHydrater\php_file_cache as FileCache;
|
|
use Liquid\{Liquid, Template, Context};
|
|
use Liquid\Cache\Local;
|
|
|
|
final class liquid_templates {
|
|
private $liquid;
|
|
private $dir;
|
|
private $extension = 'tpl';
|
|
|
|
public function __construct(private $use_local_cache = false, string $template_extension = 'tpl') {
|
|
$this->extension = $template_extension;
|
|
if (! Reg::get('loader')->is_loaded('Liquid')) {
|
|
Reg::get('loader')->add_namespace('Liquid', CodeHydrater_PROJECT . 'vendor/liquid/liquid/src/Liquid');
|
|
}
|
|
|
|
Liquid::set('INCLUDE_SUFFIX', $template_extension);
|
|
Liquid::set('INCLUDE_PREFIX', '');
|
|
|
|
$this->dir = CodeHydrater_PROJECT . 'views/liquid';
|
|
$this->liquid = new Template($this->dir);
|
|
}
|
|
|
|
public function whitespace_control() {
|
|
/* Force whitespace control to be used by switching tags from {%- to {%
|
|
* Also, will error out if you try {%-
|
|
* Need to figure out how to turn back on whitespaces with {%@ errors! not important...
|
|
*/
|
|
Liquid::set('TAG_START', '(?:{%@)|\s*{%');
|
|
Liquid::set('TAG_END', '(?:@%}|%}\s*)');
|
|
}
|
|
|
|
public function parse(string $source) {
|
|
return $this->liquid->parse($source);
|
|
}
|
|
|
|
public function parse_file(string $file) {
|
|
$safe_file = \CodeHydrater\security::filter_uri($file);
|
|
if ($this->use_local_cache) {
|
|
$cache = new FileCache(BaseDir . "/protected/runtime/liquid_cache");
|
|
|
|
$templateName = $safe_file . "." . $this->extension;
|
|
$templatePath = $this->dir . "/". $safe_file . "." . $this->extension;
|
|
|
|
$templateSource = file_get_contents($templatePath);
|
|
$cached = $cache->get($templateName);
|
|
if (!$cached) {
|
|
$this->liquid->parseFile($safe_file);
|
|
$cache->set($templateName, $this->liquid);
|
|
} else {
|
|
$this->liquid = $cached;
|
|
}
|
|
} else {
|
|
$this->liquid->parseFile($safe_file);
|
|
}
|
|
}
|
|
|
|
public function render(array $assigns = array(), $filters = null, array $registers = array()): string {
|
|
return $this->liquid->render($assigns, $filters, $registers);
|
|
}
|
|
|
|
public function get_engine() {
|
|
return $this->liquid;
|
|
}
|
|
} |