diff --git a/lib/Cache.php b/lib/Cache.php index 60ae734..9a87b54 100644 --- a/lib/Cache.php +++ b/lib/Cache.php @@ -1,8 +1,17 @@ pretransform(); // Replace includes of templates $matches = array(); - preg_match_all("/{{include (.*\.html)(.*)}}/", $contents, $matches); + preg_match_all("/{{(include|includeCached) (.*\.html)(.*)}}/", $contents, $matches); for ($i = 0; $i < count($matches[1]); $i++) { $matchString = $matches[0][$i]; - $match = $matches[1][$i]; - $options = trim($matches[2][$i]); + $method = $matches[1][$i]; + $match = $matches[2][$i]; + $options = trim($matches[3][$i]); + + if ($method === "includeCached") { + $crc = hash("crc32b", $match . $options); + $include = Cache::getIncludeCache($crc); + if (!is_null($include)) { + $contents = str_replace($matchString, $include, $contents); + continue; + } + } $include = new Template(dirname($this->template) . "/" . $match, $this->data); - $contents = str_replace($matchString, $include->render( + $rendered = $include->render( !empty($options) ? json_decode($options, false, 512, JSON_THROW_ON_ERROR) : null - ), $contents); + ); + + if ($method === "includeCached") { + Cache::setIncludeCache($crc, $rendered); + } + + $contents = str_replace($matchString, $rendered, $contents); } $class->transform();