Kotchasan Framework Documentation
View Class - Presentation Layer Management
View Class - Presentation Layer Management
The View class serves as the base for the application's presentation layer. It orchestrates the rendering process by managing templates, HTTP headers, meta tags, and asset injection (CSS/JS).
Namespace
Kotchasan\ViewOverview
This class integrates with the Template system to inject data into HTML views and manages the document's <head> section and HTTP response headers.
Key Features
- Asset Management: Dynamic injection of CSS files, JavaScript files, and inline scripts into the HTML
<head>. - Template Rendering: Replaces placeholders in templates with dynamic content.
- Response Control: Manages HTTP headers and final content output.
Public Methods
create()
Factory method to create a View instance.
public static function create(): staticrenderHTML()
Process and render the final HTML from a template.
public function renderHTML(string|null $template = null): stringProcess:
- Injects system variables:
{WEBTITLE},{WEBDESCRIPTION},{WEBURL},{SKIN}. - Injects accumulated Meta tags, CSS, and JS into the
<head>section. - Performs variable replacement using data set via
setContents.
setContents()
Set key-value pairs for template replacement.
public function setContents(array $array): voidExample:
$view->setContents([
'/{CONTENT}/' => 'Page Content',
'/{FOOTER}/' => 'Footer Info'
]);setMetas() / setHeaders()
Set HTML Meta tags and HTTP Headers.
public function setMetas(array $array): void
public function setHeaders(array $array): voidaddCSS() / addJavascript() / addScript()
Inject resources into the page.
public function addCSS(string $url): void
public function addJavascript(string $url): void
public function addScript(string $script): voidData Access Helpers
Safe accessors for arrays and objects with default fallback.
public static function array_value(array $array, string $key, mixed $default = ''): mixed
public static function object_value(object $source, string $key, mixed $default = ''): mixedback()
Generate a return URL, intelligently handling query strings (e.g., stripping login/logout actions).
public static function back(array|string $f): stringoutput()
Send headers and echo the content.
public function output(string $content): voidUsage Example
// Initialize View
$view = Kotchasan\View::create();
// Add Assets
$view->addCSS('style.css');
$view->addScript('console.log("Hello");');
// Set Data
$view->setContents([
'/{TOPIC}/' => 'My Topic',
'/{DETAIL}/' => 'Details here...'
]);
// Render using 'main' template
echo $view->renderHTML(Template::load('', '', 'main'));File Size: 263 lines