Documentation

On this page

Download full book (text)

# **Chapter 8: Master Files - Creating Page Layouts** > **File Extension Reference:** > - Gate Files: `*.gate.php` (e.g., `Index.gate.php`) > - Front Files: `*.front` (e.g., `index_main.front`) > - Master Files: `*.mast.php` (e.g., `default/master.mast.php`) > - Front Places: `*.place.front` or `*.place.php` > **Try it:** Tested masters in this project: > `masters/default/master.mast.php` (used by `http://localhost/SartajPhpDoc/index.html`) > and the custom `masters/examples/ex05.mast.php` with its Front Place > (`http://localhost/SartajPhpDoc/ex05master.html`). --- ## **8.1 What are Master Files?** Master Files (`.mast.php`) define the overall page layout - the skeleton that wraps your FrontFile content. They contain: - HTML `<head>` section with meta tags and CSS - Page header (logo, navigation) - Main content container - Page footer - JavaScript includes The Master File receives your FrontFile's output and wraps it with the common layout. ## **8.2 Master File Location** Master files are stored in the `masters/` directory: ``` masters/ ├── default/ │ └── master.mast.php # Default layout ├── admin/ │ └── admin.mast.php # Admin area layout └── minimal/ └── simple.mast.php # Minimal layout ``` ## **8.3 Basic Master File Structure** ```php <?php // masters/default/master.mast.php // First: Register and run any Front Places addFrontPlace("mainMenu", __DIR__ . "/menu.main.place.front"); runFrontPlace("mainMenu", "header"); addFrontPlace("footer", __DIR__ . "/footer.place.front"); runFrontPlace("footer", "footer"); ?><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>My Website</title> <?php // Framework adds Bootstrap and other CSS/JS SphpBase::SphpJsM()::addBootStrap(); echo SphpBase::sphpApi()->getHeaderHTML(); ?> </head> <body> <!-- Header with Menu --> <header class="bg-light"> <div class="container"> <?php renderFrontPlace("mainMenu", "header"); ?> </div> </header> <!-- Main Content - Where FrontFile Goes --> <main class="container my-4"> <?php SphpBase::getAppOutput(); ?> </main> <!-- Footer --> <footer class="bg-dark text-white py-4"> <div class="container"> <?php renderFrontPlace("footer", "footer"); ?> </div> </footer> <?php // Framework additional JS echo SphpBase::sphpApi()->getFooterHTML(); ?> </body> </html> ``` ## **8.4 Key Master File Functions** ### **addFrontPlace() - Register a Place** ```php addFrontPlace("name", "path/to/file.place.front", "position"); ``` Parameters: - **name** - Identifier for the place - **path** - Full path to the Front Place file - **position** - Optional position within the place ### **runFrontPlace() - Execute Logic** ```php runFrontPlace("name", "position"); ``` Must be called AFTER `addFrontPlace()` but BEFORE `<!DOCTYPE>` ### **renderFrontPlace() - Output Content** ```php <?php renderFrontPlace("name", "position"); ?> ``` Called where you want the content to appear in the HTML. ### **getAppOutput() - FrontFile Content** ```php <?php SphpBase::getAppOutput(); ?> ``` This is where your FrontFile's rendered HTML appears! ## **8.5 Creating a Menu Front Place** Create `masters/default/menu.main.place.front`: ```html <!-- menu.main.place.front - Main Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-primary"> <div class="container-fluid"> <a class="navbar-brand" href="##{getGateUrl('index')}#">MyApp</a> <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarNav"> <ul class="navbar-nav me-auto"> <li class="nav-item"> <a class="nav-link" href="##{getGateUrl('index')}#">Home</a> </li> <li class="nav-item"> <a class="nav-link" href="##{getGateUrl('product')}#">Products</a> </li> <li class="nav-item"> <a class="nav-link" href="##{getGateUrl('index','about')}#">About</a> </li> <li class="nav-item"> <a class="nav-link" href="##{getGateUrl('index','contact')}#">Contact</a> </li> </ul> <!-- Right side - conditional based on login --> <ul class="navbar-nav"> #{if SphpBase::page()->getAuthenticateType() != 'GUEST'}# <li class="nav-item"> <a class="nav-link" href="##{getEventUrl('logout','','signin')}#">Logout</a> </li> #{else}# <li class="nav-item"> <a class="nav-link" href="##{getGateUrl('signin')}#">Login</a> </li> #{endif}# </ul> </div> </div> </nav> ``` > **Tested:** auth checks use `SphpBase::page()->getAuthenticateType()` > (`'GUEST'`, `'MEMBER'`, `'ADMIN'`) — there is no `isLoggedIn()` / > `getUserName()` on the Gate. Inside `.place.front` files, expression tags and > `getGateUrl` / `getEventUrl` work the same as in normal FrontFiles. For > permission-driven menus use the `BootstrapMenu` class instead (Chapter 6, > `masters/default/menu.place.php`). ## **8.6 Creating a Footer Front Place** Create `masters/default/footer.place.front`: ```html <!-- footer.place.front - Page Footer --> <div class="row"> <div class="col-md-4"> <h5>About Us</h5> <p>We provide quality products and excellent service.</p> </div> <div class="col-md-4"> <h5>Quick Links</h5> <ul class="list-unstyled"> <li><a href="##{getGateUrl('index')}#">Home</a></li> <li><a href="##{getGateUrl('product')}#">Products</a></li> <li><a href="##{getGateUrl('index','contact')}#">Contact</a></li> </ul> </div> <div class="col-md-4"> <h5>Contact</h5> <p>Email: info@example.com<br> Phone: (123) 456-7890</p> </div> </div> <hr> <div class="text-center"> <small>&copy; ##{date('Y')}# MyApp. All rights reserved.</small> </div> ``` ## **8.7 Adding Custom CSS/JS to Master File** ### **Adding Custom CSS** ```php <?php // In onrender() of your Gate addFileLink("path/to/your-style.css"); ``` Or directly in Master File: ```html <link rel="stylesheet" href="custom.css"> ``` ### **Adding Custom JavaScript** ```php <?php // In onrender() of your Gate — signature: (name, code, renderonce) addHeaderJsCode("app1","console.log('Page loaded!');",true); ``` Or add to Master File directly: ```html <script src="custom.js"></script> ``` ## **8.8 Multiple Master Files** You can have different layouts for different sections. The default master is set in `comp.php` (tested line): ```php <?php // comp.php $masterf = "masters/default/master.mast.php"; ``` Gates pick it up via `global $masterf` + `setMasterFile()`, or choose a different master directly: ```php <?php // In a Gate for the admin area public function onstart() { // Use different master for admin — path relative to project root $this->setMasterFile('masters/admin/admin.mast.php'); } ``` > **Tested:** `setMasterFile()` is the API — there is no `$this->masterf` property > assignment. See `apps/examples/Ex05master.gate.php`: > `$this->setMasterFile('masters/examples/ex05.mast.php');` (no `global $masterf` > needed when you hardcode the path). ## **8.9 Complete Master File Example** Here's a production-ready Master File: ```php <?php // masters/default/master.mast.php // ===== Front Places Setup ===== // Main navigation menu addFrontPlace("mainMenu", __DIR__ . "/menu.main.place.front", "main"); runFrontPlace("mainMenu", "main"); // Sidebar (for certain pages) addFrontPlace("sidebar", __DIR__ . "/sidebar.place.front", "left"); runFrontPlace("sidebar", "left"); // Footer addFrontPlace("footer", __DIR__ . "/footer.place.front", "footer"); runFrontPlace("footer", "footer"); // Breadcrumb (optional) addFrontPlace("breadcrumb", __DIR__ . "/breadcrumb.place.front", "nav"); runFrontPlace("breadcrumb", "nav"); // Check if we should show sidebar $showSidebar = SphpBase::sphpApi()->getProp("showSidebar") ?? false; ?><!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <?php // Load framework assets SphpBase::SphpJsM()::addBootStrap(); // Output framework header (jQuery, Bootstrap, meta/SEO from the Title // component, plus anything added by addHeaderCss/addFileLink/…). // The <title> and description meta come from getHeaderHTML() — the Title // component in the FrontFile feeds SphpBase::sphpSettings()->title. echo SphpBase::sphpApi()->getHeaderHTML(); ?> </head> <body> <!-- ===== Navigation ===== --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark"> <div class="container"> <a class="navbar-brand" href="<?php echo SphpBase::sphpSettings()->projectUrl; ?>"> <strong>MyApp</strong> </a> <?php renderFrontPlace("mainMenu", "main"); ?> </div> </nav> <!-- ===== Breadcrumb ===== --> <?php if (SphpBase::sphpApi()->getProp("showBreadcrumb")): ?> <nav class="breadcrumb-nav"> <div class="container"> <?php renderFrontPlace("breadcrumb", "nav"); ?> </div> </nav> <?php endif; ?> <!-- ===== Main Content ===== --> <div class="container my-4"> <div class="row"> <!-- Sidebar (if enabled) --> <?php if ($showSidebar): ?> <div class="col-md-3"> <?php renderFrontPlace("sidebar", "left"); ?> </div> <?php endif; ?> <!-- Main Content Area - FrontFile Output Here! --> <div class="<?php echo $showSidebar ? 'col-md-9' : 'col-12'; ?>"> <?php SphpBase::getAppOutput(); ?> </div> </div> </div> <!-- ===== Footer ===== --> <footer class="bg-light py-4 mt-auto"> <div class="container"> <?php renderFrontPlace("footer", "footer"); ?> </div> </footer> <!-- ===== Framework Footer ===== --> <?php echo SphpBase::sphpApi()->getFooterHTML(); echo SphpBase::sphpApi()->traceError(true); // Show errors in debug mode ?> </body> </html> ``` ## **8.10 How Master File Gets Selected** The Master File to use is configured in `comp.php`: ```php <?php // comp.php — tested default $masterf = "masters/default/master.mast.php"; $mebmasterf = "masters/default/master.mast.php"; // member-area master ``` Gates read it with `global $masterf` and pass it to `setMasterFile()`: ```php public function onstart() { global $masterf; $this->frtMain = new FrontFile("main"); $this->setMasterFile($masterf); } ``` Or override per Gate: ```php public function onstart() { $this->frtMain = new FrontFile("main"); $this->setMasterFile("masters/custom/master.mast.php"); } ``` ## **8.11 Chapter Summary** 1. **Master Files** (`.mast.php`) provide page layout 2. **Front Places** (`.place.front`) provide reusable sections (menu, footer) 3. **addFrontPlace()** - Register a place 4. **runFrontPlace()** - Execute place logic 5. **renderFrontPlace()** - Output place content 6. **getAppOutput()** - Where FrontFile content goes 7. Configure master in `comp.php` or in Gate's `onstart()` ## **8.12 What's Next?** Next, learn about the `runas="holder"` attribute for dynamic content injection in FrontFiles. --- *Next: Chapter 9 - The runas="holder" Attribute*