fixed twig and functions

This commit is contained in:
Nick Yeoman 2025-12-05 18:04:17 -08:00
parent 466d34c39f
commit 208534b5fb
2 changed files with 19 additions and 14 deletions

View File

@ -7,6 +7,9 @@ if (file_exists(\BASEPATH . '/App/config.php')) {
require_once \FRAMEWORKPATH . '/skeleton/novaconium/App/config.php';
}
require_once \FRAMEWORKPATH . '/src/functions.php';
require_once \FRAMEWORKPATH . '/src/twig.php';
// --- Logging ---
use Novaconium\Logger;
$log = new Logger(\BASEPATH . $config['logfile'], $config['loglevel']);
@ -47,4 +50,4 @@ $redirect = new Redirect();
// --- Router ---
use Novaconium\Router;
$router = new Router();
require_once \BASEPATH . $router->controllerPath;
require_once $router->controllerPath;

View File

@ -17,12 +17,10 @@ function view(string $name = '', array $moreData = []): bool
{
global $config, $data;
// Ensure $data is always an array
if (!is_array($data)) {
$data = [];
}
// Merge additional data
if (!empty($moreData)) {
$data = array_merge($data, $moreData);
}
@ -31,23 +29,29 @@ function view(string $name = '', array $moreData = []): bool
// Setup Twig
// ----------------------------------------
$loader = new FilesystemLoader([
BASEPATH . '/App/views/',
FRAMEWORKPATH . '/twig',
FRAMEWORKPATH . '/views',
BASEPATH . '/App/templates/override'
]);
$loader = new FilesystemLoader(BASEPATH . '/App/views/');
// Add namespace paths
if (is_dir(FRAMEWORKPATH . '/twig')) {
$loader->addPath(FRAMEWORKPATH . '/twig', 'novaconium');
}
if (is_dir(FRAMEWORKPATH . '/views')) {
$loader->addPath(FRAMEWORKPATH . '/views', 'novacore');
}
if (is_dir(BASEPATH . '/App/templates')) {
$loader->addPath(BASEPATH . '/App/templates', 'override');
}
$twig = new Environment($loader);
// Add global config
$twig->addGlobal('config', $config);
// ----------------------------------------
// Render template
// ----------------------------------------
// Direct App override template
$appTemplatePath = BASEPATH . '/App/views/' . $name . '.html.twig';
if (file_exists($appTemplatePath)) {
@ -55,13 +59,11 @@ function view(string $name = '', array $moreData = []): bool
return true;
}
// Namespaced Twig templates like @novaconium/whatever
if (str_starts_with($name, '@')) {
echo $twig->render($name . '.html.twig', $data);
return true;
}
// Fallback
echo "Error: Twig Template ($name) Not Found.";
return false;
}
}