Documentation

On this page

Download full book (text)

# **Chapter 9: The runas="holder" Attribute - Dynamic Content Injection** > **File Extension Reference:** > - Gate Files: `*.gate.php` (e.g., `Product.gate.php`) > - Front Files: `*.front` (e.g., `product_page.front`) > - Master Files: `*.mast.php` (e.g., `default/master.mast.php`) > - Front Places: `*.place.front` or `*.place.php` > **Try it:** Every holder form in this chapter is tested on the runnable page > `http://localhost/SartajPhpDoc/product-page.html` (gate `product`, > `apps/examples/fronts/product_page.front`). Add a few products first so pagination > has more than one page. --- ## **9.1 What is runas="holder"?** The `runas="holder"` attribute creates a **placeholder** in your FrontFile that gets filled with dynamic content at render time. Unlike `runat="server"` which creates a Component, `runas="holder"` just marks a spot for dynamic content without creating a Component. ## **9.2 Resolution Rules (tested)** A holder is resolved by its helper attributes, in this order: | Used Attributes | Runtime Behavior | Tested? | |-----------------|------------------|---------| | `sphp-comp` + `sphp-comp-prop` | Display **Component property value** | Yes | | `sphp-comp` + `sphp-comp-fun` | Display **Component method return** | Yes | | `sphp-comp` + `dfield` | Display **Component row field** (Pagination/Grid rows) | Yes | | `sphp-comp` only | Trigger **Component `onholder` event**, pass NodeTag | Yes | | none (bare holder) | Trigger **Gate `onholder` event**, pass NodeTag | Yes | | `sphp-app-prop` / `sphp-app-fun` | **Not resolved in current build** — falls through to Gate `onholder` | Yes (see 9.7) | ## **9.3 Bare Holder → Gate onholder()** The simplest form: any tag with `runas="holder"` and no helper attributes is passed to your Gate's `onholder($element)` method. FrontFile: ```html <!-- apps/examples/fronts/product_page.front --> <div id="holdBare" class="alert alert-secondary" runas="holder">placeholder</div> ``` Gate: ```php // apps/examples/Product.gate.php public function onholder($element) { // $element is the NodeTag with runas="holder" $element->setInnerHtml('filled by Gate onholder()'); } ``` Output: `<div ...>filled by Gate onholder()</div>` **Pitfall (tested):** a bare holder without an `onholder()` method in the Gate throws `Call to undefined method ...::onholder()`. ## **9.4 One onholder() Serving Many Placeholders** Branch on your own data attribute to serve several placeholders from one method: FrontFile: ```html <div class="alert alert-info" runas="holder" data-widget-type="stats">counting...</div> ``` Gate: ```php // apps/examples/Product.gate.php public function onholder($element) { $type = $element->getAttribute('data-widget-type'); if ($type === 'stats') { $db = $this->dbEngine; $db->connect(); $result = $db->executeQueryQuick('SELECT COUNT(*) AS c FROM book2_product'); $row = $db->rowFetchAssoc($result); $db->disconnect(); $element->setInnerHtml('Products in DB: ' . (isset($row['c']) ? $row['c'] : '?')); } else { $element->setInnerHtml('filled by Gate onholder()'); } } ``` Output: `Products in DB: 7` ## **9.5 Holder from Component (Pagination rows)** Inside a Pagination/Grid row template, `sphp-comp` + `dfield` prints the current row's field (this is how every list in this book is built): ```html <tbody id="showall" runat="server" path="uikit/data/Pagination.php" fun-setFieldNames="id,txtname" dtable="book2_product" fun-setPerPageRows="3"> <tr> <td runas="holder" sphp-comp="showall" dfield="id"></td> <td runas="holder" sphp-comp="showall" dfield="txtname"></td> </tr> </tbody> ``` ### **Holder from Component Property** ```html <!-- Read the page_links property of the Pagination Component --> <div runas="holder" sphp-comp="showall" sphp-comp-prop="page_links"></div> ``` Renders the page-number links: `<a href="...product-show.html?page=1">1</a><a ...>2</a><a ...>3</a>` **Tested caveat:** `page_links` is empty until the query actually has more than one page — set `fun-setPerPageRows` small while testing. ### **Holder from Component Method** ```html <!-- Call getPageBar() on the Pagination Component --> <div runas="holder" sphp-comp="showall" sphp-comp-fun="getPageBar"></div> ``` Same output as the property form above (`getPageBar()` returns `$this->page_links`). ## **9.6 Gate Properties and Methods → Expressions (recommended)** For Gate → FrontFile content, the tested mechanism is the **expression tag**, not a holder attribute: ```php // apps/examples/Product.gate.php /** Gate property */ public $sidebarNote = '<strong>Sidebar</strong> from Gate property'; /** Gate method */ public function getCategoryList() { $cats = array('Electronics', 'Clothing', 'Books', 'Home'); $html = '<ul class="list-group">'; foreach ($cats as $cat) { $html .= '<li class="list-group-item">' . $cat . '</li>'; } return $html . '</ul>'; } ``` ```html <!-- Property (HTML-escaped) --> <p>##{$parentGate->sidebarNote}#</p> <!-- Method return as raw HTML --> ##{raw:$parentGate->getCategoryList()}# ``` - `##{$parentGate->prop}#` — reads a Gate property (escaped). - `##{raw:$parentGate->method()}#` — prints a Gate method's return as raw HTML. - Inside expressions you can also use `$this` for the parent Gate in some contexts; `$parentGate` is the tested, explicit form. ## **9.7 About sphp-app-prop / sphp-app-fun** Older documentation (and some RAG rules) describe: ```html <div runas="holder" sphp-app-prop="sidebarNote"></div> <div runas="holder" sphp-app-fun="getCategoryList"></div> ``` **Tested result (current framework build):** these two attributes are **not resolved** — the holder falls through to the Gate `onholder()` event exactly like a bare holder (both divs above printed `filled by Gate onholder()`). Until they are supported, use: | Instead of | Use | |------------|-----| | `sphp-app-prop="sidebarNote"` | `##{$parentGate->sidebarNote}#` | | `sphp-app-fun="getCategoryList"` | `##{raw:$parentGate->getCategoryList()}#` | Keep an `onholder()` method in any Gate whose FrontFiles contain holders, since unresolved holders route to it. ## **9.8 Practical Example - Dynamic Sidebar** ```php <?php // apps/examples/Product.gate.php (sidebar parts) /** URL: product-page.html — holder demo */ public function page_event_page($evtp) { $this->pageName = 'Holder Demo'; $this->setFrontFile($this->pageFront); } ``` ```html <!-- apps/examples/fronts/product_page.front (sidebar column) --> <div class="col-md-4"> <div class="card mb-3"> <div class="card-header">Categories</div> <div class="card-body"> ##{raw:$parentGate->getCategoryList()}# </div> </div> <div class="card mb-3"> <div class="card-header">Note</div> <div class="card-body"> ##{$parentGate->sidebarNote}# </div> </div> <div class="card"> <div class="card-header">Stats</div> <div class="card-body"> <div runas="holder" data-widget-type="stats">counting...</div> </div> </div> </div> ``` ## **9.9 Holder vs Component** | Aspect | runat="server" (Component) | runas="holder" | |--------|---------------------------|----------------| | Creates | Component object | No Component | | Use Case | Interactive elements, row templates | Static content slots | | Lifecycle | Has events (on-init, on-startrender, …) | Resolved once at render | | Database | Can use dtable/dfield | `sphp-comp` + `dfield` reads a row field | | Best For | Forms, inputs, interactive | Sidebars, lists, page bars | ## **9.10 Holder with Conditional Logic** Use expression tags for conditional content around holders: ```html #{if $parentGate->getAuthenticateType() != 'GUEST'}# <div runas="holder" data-widget-type="stats"></div> #{else}# <p class="text-muted">Login to see stats.</p> #{endif}# ``` Or conditional class based on a Gate property: ```html <div class="alert ##{$parentGate->hasError ? 'alert-danger' : 'alert-info'}#"> ##{$parentGate->statusMessage}# </div> ``` ## **9.10a Expression Tag Rules — What Belongs Inside `##{ }#`** Expression tags are a **controlled subset** of PHP for presentation only. The framework executes them with restricted scope (no project globals — only the component/Gate objects in the template context), and the philosophy is: *simple output, never complex logic.* **✅ Do inside expression tags:** ```html <p>##{$parentGate->userName}#</p> <!-- variable --> <p>##{$parentGate->price * $parentGate->qty}#</p> <!-- arithmetic --> <div class="##{$parentGate->isActive ? 'on' : 'off'}#"> <!-- ternary --> <p>##{'Hello ' . $parentGate->firstName}#</p> <!-- concatenation --> ##{raw:$parentGate->getCategoryList()}# <!-- Gate method --> ``` **❌ Don't:** - Define functions or classes inside a tag - Write loops that build HTML — use a Pagination/loop Component or a Gate method returning the HTML string (the `getCategoryList()` pattern above) - Put business logic or DB access in a tag — prepare data in the Gate, print it in the template - Reach for `$_GET`/`$_POST`/globals — they are not in scope; read `$parentGate->prop` instead The rule of thumb: if an expression needs more than one line to explain, it belongs in a Gate method called via `##{raw:$parentGate->method()}#`. Silent tags `#{ }#` (no output) cover control flow around markup — the `#{if}#`/`#{else}#`/`#{endif}` pattern from 9.10 is the tested form. ## **9.11 Chapter Summary** 1. **runas="holder"** creates placeholder for dynamic content 2. **Bare holder** → Gate `onholder($element)` (required, or exception) 3. **data-widget-type** + `onholder()` switch → many widgets, one handler 4. **sphp-comp + dfield** → Pagination/Grid row field 5. **sphp-comp + sphp-comp-prop / sphp-comp-fun** → Component property / method 6. **Gate properties and methods** → use expressions `##{$parentGate->prop}#` and `##{raw:$parentGate->method()}#` (sphp-app-prop/fun are not resolved in this build) 7. Use holders for sidebars, lists, static content; Components for interactive elements ## **9.12 What's Next?** Next, learn about Code Blocks - how to enhance FrontFile design with reusable styling and layout transformations. --- *Next: Chapter 10 - Code Blocks: Enhancing FrontFile Design*