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 . '/skeleton/novaconium/App/config.php';
} }
require_once \FRAMEWORKPATH . '/src/functions.php';
require_once \FRAMEWORKPATH . '/src/twig.php';
// --- Logging --- // --- Logging ---
use Novaconium\Logger; use Novaconium\Logger;
$log = new Logger(\BASEPATH . $config['logfile'], $config['loglevel']); $log = new Logger(\BASEPATH . $config['logfile'], $config['loglevel']);
@ -47,4 +50,4 @@ $redirect = new Redirect();
// --- Router --- // --- Router ---
use Novaconium\Router; use Novaconium\Router;
$router = new 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; global $config, $data;
// Ensure $data is always an array
if (!is_array($data)) { if (!is_array($data)) {
$data = []; $data = [];
} }
// Merge additional data
if (!empty($moreData)) { if (!empty($moreData)) {
$data = array_merge($data, $moreData); $data = array_merge($data, $moreData);
} }
@ -31,23 +29,29 @@ function view(string $name = '', array $moreData = []): bool
// Setup Twig // Setup Twig
// ---------------------------------------- // ----------------------------------------
$loader = new FilesystemLoader([ $loader = new FilesystemLoader(BASEPATH . '/App/views/');
BASEPATH . '/App/views/',
FRAMEWORKPATH . '/twig', // Add namespace paths
FRAMEWORKPATH . '/views', if (is_dir(FRAMEWORKPATH . '/twig')) {
BASEPATH . '/App/templates/override' $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); $twig = new Environment($loader);
// Add global config
$twig->addGlobal('config', $config); $twig->addGlobal('config', $config);
// ---------------------------------------- // ----------------------------------------
// Render template // Render template
// ---------------------------------------- // ----------------------------------------
// Direct App override template
$appTemplatePath = BASEPATH . '/App/views/' . $name . '.html.twig'; $appTemplatePath = BASEPATH . '/App/views/' . $name . '.html.twig';
if (file_exists($appTemplatePath)) { if (file_exists($appTemplatePath)) {
@ -55,13 +59,11 @@ function view(string $name = '', array $moreData = []): bool
return true; return true;
} }
// Namespaced Twig templates like @novaconium/whatever
if (str_starts_with($name, '@')) { if (str_starts_with($name, '@')) {
echo $twig->render($name . '.html.twig', $data); echo $twig->render($name . '.html.twig', $data);
return true; return true;
} }
// Fallback
echo "Error: Twig Template ($name) Not Found."; echo "Error: Twig Template ($name) Not Found.";
return false; return false;
} }