PHPainfree2 Docs

BaseView - Documentation | PHPainfree2

PHPainfree2 BaseView

A detailed overview of the PHPainfree2 BaseView.

BaseView: templates/app.php

The provided app.php BaseView is a good structural starting point for any project you intend to build. BaseView serves as the foundation for all templates output to the user in a normal HTTP web request. Think of the BaseView as a conductor for your templates.

In most PHPainfree2 projects, the BaseView will also contain the HTML structure of your output and contain the initial HTML elements that define your website design and structure.

BaseView
What Where How
Defined PainfreeConfig.php $PainfreeConfig['BaseView'];
Called Painfree.php $Painfree->view();
PHPainfree/
|-- htdocs/
|-- includes/
|   |-- PainfreeConfig.php
|   |-- Painfree.php
|   |-- App.php
|   `-- Controllers/
`-- templates/
    |-- app.php
	`-- views/
	    `-- main.php

Rendering a Template BaseView

After completing the route() method, execution returns to $Painfree and $Painfree->view(); is called. This function loads the script defined in your PainfreeConfig.php BaseView option, which is the script located in templates/. In the PainfreeConfig provided with this project, the initial BaseView template is defined as templates/app.php.

Your initial BaseView template is often going to be the most complicated template in your project, performing double-duty as an HTML skeleton for the rest of your views as well as dynamically loading view templates based on the URL. In PHPainfree2, this template also has code to handle htmx partial templates.

PHPainfree/
|-- htdocs/
|-- includes/
`-- templates/
    |-- app.php
	`-- views/
	    `-- main.php
templates/app.php

<?php
if ( 
	$App->htmx && 
	! $App->htmx_boosted && 
	file_exists("{$App->BASE_PATH}/templates/views/{$App->view}.php") 
	) {
	// If we are an htmx request and the "view" variable exists in the top-level
	// templates folder, render that as an HTMX snippet.
	//
	// If we are an htmx request and there is a "sub-view" defined that lives
	// inside a folder, render _THAT_ instead of the full top-level snippet.
	//
	// In _this_ application, we're overriding $App->id to act as our default
	// "sub-view" route, but you should feel free to write whatever type of 
	// routing architecture that you want.
	//
	// This example requires that a top-level /templates/views/{$view}.php file 
	// exists **AND** a top-level /templates/views/{$view}/{$id}.php file to
	// exist for this magic to occur. 
	//
	// Each application built with PHPainfree should design their routing and
	// template relationships however best suits that product.
	$file_path = "{$App->BASE_PATH}/templates/views/{$App->view}/{$App->id}.php";
	if (file_exists($file_path)) {
		include_once "views/{$App->view}/{$App->id}.php";
	} else {
		include_once "views/{$App->view}.php";
	}
} else { 
?>
<!DOCTYPE html>
<html lang="en">
	<head>
		<title><?= $Painfree->safe($App->title()); ?></title>

		<link rel="icon" type="image/x-icon" href="/images/favicon.ico" />

Dynamic View Template Loading

Inside of templates/app.php in the meat of our web template, we'll load a template file in the template/views/ directory if one exists with the same name as the value stored in $App->view.

In PainfreeConfig, we have our DefaultRoute parameter defined as "main", so our application will serve templates/views/main.php for any request to either http://hostname.com/ (no path) as well as explictly called like http://hostname.com/main. The value of that view, "main" automatically loads:

  • includes/Controllers/main.php
  • templates/views/main.php

This is one of the designs that makes developing projects with PHPainfree2 so quick. Adding new pages is as simple as dropping a file in the includes/Controllers/ folder and templates/views/ folder. And as you develop more complicated applications, allowing all of your Controller code to serve as the business logic for your REST JSON API, you're able to do a lot more with a lot less duplication.

templates/app.php

	<body id="app-body" class="bg-dark text-light">
<?php
		include 'header.php';	
?>

<?php
	if ( file_exists("{$App->BASE_PATH}/templates/views/{$App->view}.php") ) {
		include "views/{$App->view}.php";
	} else {
		include "views/404.php";
	}
?>

<?php
		include 'footer.php';	
?>