Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions core/View/View.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ public static function init()
{
$viewsPath = __DIR__ . '/../../app/Views';
$loader = new FilesystemLoader($viewsPath);

$twigConfig = config('view.twig', []);

// Configure cache properly
$cacheEnv = $twigConfig['cache'] ?? false;
$cachePath = false; // Default to no cache
Expand All @@ -27,7 +27,7 @@ public static function init()
mkdir($cachePath, 0777, true);
}
}

self::$twig = new Environment($loader, [
'cache' => $cachePath,
'debug' => $twigConfig['debug'] ?? true,
Expand All @@ -43,11 +43,12 @@ public static function init()
// Register helpers for Twig: auto-register all from helpers.php, merge with config('view.twig_helpers') if set.
self::registerExplicitHelpers();
}

/**
* Register helpers for Twig: auto-register all from helpers.php, merge with config('view.twig_helpers') if set.
*/
private static function registerExplicitHelpers() {
private static function registerExplicitHelpers()
{
// 1. Get all user-defined functions (auto-discover from helpers.php and any loaded helpers)
$userFunctions = get_defined_functions()['user'];

Expand All @@ -60,7 +61,9 @@ private static function registerExplicitHelpers() {
// 4. Register each helper if it exists
foreach ($allHelpers as $helper) {
if (function_exists($helper)) {
self::$twig->addFunction(new \Twig\TwigFunction($helper, $helper));
self::$twig->addFunction(new TwigFunction($helper, $helper));
} else {
log_error("[SproutPHP] Warning: Twig helper function '$helper' does not exist and was not registered.");
}
}
}
Expand All @@ -75,13 +78,13 @@ public static function render($template, $data = [], $return = false)
if (Debugbar::isAjaxRequest() && config('app.debug', false)) {
// Reset debugbar for this request
Debugbar::resetForRequest();

// Render the template first
$content = self::$twig->render($template . '.twig', $data);

// Append debugbar to the response
$debugbar = Debugbar::render();

if ($return) {
return $content . $debugbar;
}
Expand Down