# **Chapter 10: Code Blocks - Enhancing FrontFile Design**
> **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:** All code in this chapter is tested code from the runnable example
> gate `ex08codeblock` (`apps/examples/Ex08codeblock.gate.php` +
> `apps/examples/fronts/ex08codeblock_main.front`). Open
> `http://localhost/SartajPhpDoc/ex08codeblock.html` — every block below appears
> on that page.
---
## **10.1 What are Code Blocks?**
Code Blocks (`runcb="true"`) allow you to apply pre-defined styling and layout
transformations to any HTML element in your FrontFile without writing custom PHP
code. They work similar to CSS utility classes but with server-side processing.
Think of code blocks as **reusable design patterns** that transform simple HTML
into styled, structured output.
| | Code Block | Component |
|---|---|---|
| Attribute | `runcb="true"` + `sphp-cb-name="..."` | `runat="server"` |
| Creates PHP object | No | Yes |
| Gate access | No (callback only) | `$front->getComponent('id')` |
| Theme changes | Re-register the block — FrontFiles untouched | Edit Gate/Front |
## **10.2 Basic Syntax (tested)**
```html
<!-- Apply a code block -->
<div id="demoCard" runcb="true" sphp-cb-card="Card title">
Body from FrontFile — structure from Code Block card.
</div>
<!-- Compose several blocks on one tag (each attribute = one block) -->
<div id="demoBorder" class="mt-3 p-2" runcb="true"
sphp-cb-border="3" sphp-cb-color="primary">
sphp-cb-border="3" + sphp-cb-color="primary"
</div>
```
- `runcb="true"` — enable code block processing for this tag
- `sphp-cb-[blockname]` — apply the named block
- **Multiple blocks:** put several `sphp-cb-*` attributes on the same tag
> **Tested argument rule:** in this framework build the **whole attribute value
> arrives as `$args[0]`** — commas are NOT auto-split. `sphp-cb-card="Header,Footer"`
> gives the callback ONE argument `"Header,Footer"`. Either use one value per
> attribute (and compose attributes), or split commas yourself inside your own
> callback (Section 10.5).
## **10.3 Where Blocks Live**
Two layers register blocks with the same registry:
1. **UI kit (shared):** `vendor/sartajphp/sartajphp/res/components/uikitdef/bootstrap/sphpcodeblock.php`
2. **Project (theme):** `masters/sphpcodeblock.php` — included by the UI-kit file
at its end, so the project file can **add new blocks** or **re-register an
existing name to override it** (theme change without editing FrontFiles).
## **10.4 Shared UI-Kit Blocks (verified in this install)**
From the vendor `sphpcodeblock.php` — these all work out of the box:
| Block | Value | Effect (verified) |
|-------|-------|-------------------|
| `card` | title **or** image path | Bootstrap card; value with `.` → `<img class="card-img-top">`, else `card-header`; `class` option wraps body in `card-body` |
| `border` | `0-5`, `top`, `end`, `bottom`, `start` | `border` / `border-N` classes |
| `color` | `primary`…`dark` | text/bg color class |
| `shadow` | empty/`1`/`2`/`3` | `shadow`, `shadow-sm`, `shadow-lg`, `shadow-none` |
| `size` | `1-5` | `w-25`…`w-100`, `w-auto` |
| `rounded` | `0-3`, `circle`, `pill`, sides | `rounded-*` |
| `align` | `left/center/right` | `d-flex justify-content-*` |
| `overflow` | `1-3` | `overflow-scroll/hidden/visible` |
| `text` | align/size args | text classes |
| `position` | `left/center/right` | positioning wrapper |
| `label` | label text (+sizes) | Bootstrap horizontal form row around an input |
| `btn-count`, `btn-marker` | color | button classes |
| `bggrad` | colors | gradient background |
| `aos`, `counter` | animation args | scroll animations (not in AJAX mode) |
Usage (from the tested `ex08codeblock_main.front`):
```html
<div runcb="true" sphp-cb-card="Card title">…</div>
<div runcb="true" sphp-cb-border="3" sphp-cb-color="primary">…</div>
<div runcb="true" sphp-cb-shadow="2">Expect class shadow-lg</div>
```
## **10.5 Defining Your Own Blocks (`masters/sphpcodeblock.php`)**
Callback signature: `function ($element, $args, $lstOther_CB)` where `$element` is
the HTML node (methods: `appendAttribute`, `appendPreTag`, `appendPostTag`,
`appendInnerPreTag`, `setInnerPreTag`, …), `$args` is the argument list, and
`$lstOther_CB` carries block options (e.g. `class`).
Tested project blocks:
```php
<?php
// masters/sphpcodeblock.php
/**
* Split one attribute value into arg list.
* Verified: this runtime passes the whole attribute string as $args[0]
* (comma-separated multi-args as in older docs are not auto-split here).
*/
function sphp_ex08_split_args($args)
{
if (!is_array($args) || count($args) === 0) {
return array();
}
if (count($args) === 1 && is_string($args[0]) && strpos($args[0], ',') !== false) {
return array_map('trim', explode(',', $args[0]));
}
return $args;
}
// Project-only: alert-style callout — sphp-cb-ex08callout="Tip"
SphpCodeBlock::addCodeBlock(
'ex08callout',
function ($element, $args, $lstOther_CB) {
$args = sphp_ex08_split_args($args);
$title = isset($args[0]) && $args[0] !== '' ? $args[0] : 'Note';
$element->appendAttribute('class', ' alert alert-info');
$element->appendInnerPreTag('<strong class="me-1">' . htmlspecialchars($title) . ':</strong> ');
},
array('documentation' => 'Ex08 callout. Value = title.')
);
// Project-only panel with comma args split in the callback:
// sphp-cb-ex08panel="Title,Footer"
SphpCodeBlock::addCodeBlock(
'ex08panel',
function ($element, $args, $lstOther_CB) {
$args = sphp_ex08_split_args($args);
$title = isset($args[0]) ? $args[0] : '';
$footer = isset($args[1]) ? $args[1] : '';
$pre = '<div class="card border-info" data-ex08="panel">';
if ($title !== '') {
$pre .= '<div class="card-header">' . htmlspecialchars($title) . '</div>';
}
$pre .= '<div class="card-body">';
$element->appendPreTag($pre);
$post = '</div>';
if ($footer !== '') {
$post .= '<div class="card-footer text-muted">' . htmlspecialchars($footer) . '</div>';
}
$post .= '</div>';
$element->appendPostTag($post);
},
array('documentation' => 'Ex08 panel. Value title,footer (comma-split in project callback).')
);
```
## **10.6 Overriding a Shared Block (Project Theme)**
Re-calling `SphpCodeBlock::addCodeBlock` with an existing name **replaces** that
block. Tested override of the UI-kit `shadow` — FrontFiles keep using
`sphp-cb-shadow`, but the project version adds its own marker:
```php
<?php
// masters/sphpcodeblock.php — project override of shared "shadow"
SphpCodeBlock::addCodeBlock(
'shadow',
function ($element, $args, $lstOther_CB) {
if (isset($args[0]) && $args[0] !== '') {
$ch = array(1 => 'shadow-sm', 2 => 'shadow-lg', 3 => 'shadow-none');
$n = intval($args[0]);
$element->appendAttribute('class', ' ' . (isset($ch[$n]) ? $ch[$n] : 'shadow'));
} else {
$element->appendAttribute('class', ' shadow');
}
$element->setAttribute('data-ex08-shadow', 'project');
},
array('documentation' => 'Project override of UI-kit shadow.')
);
```
Verified output on `ex08codeblock.html`: the demo div gets `class="... shadow-lg"`
**and** `data-ex08-shadow="project"` — proof the project block ran instead of the
UI-kit one.
## **10.7 Practical Example — Product Card Without PHP**
The shared `card` block already covers the classic product-card layout — its first
argument is a **title or an image path** (value containing `.` renders
`card-img-top`), and the `class` option wraps content in `card-body`:
```html
<!-- product_list.front -->
<div class="col-md-4">
<div runcb="true" sphp-cb-card="/img/product1.jpg">
<h5 class="card-title">Product Name</h5>
<p class="card-text">Description goes here.</p>
</div>
</div>
<div class="col-md-4">
<div runcb="true" sphp-cb-card="Simple header">
<p class="card-text">Card with text header.</p>
</div>
</div>
```
For a title **and** footer in one attribute, use a project block with the
comma-split helper (10.5):
```html
<div runcb="true" sphp-cb-ex08panel="Panel title,Panel footer">
Comma args split inside the project callback (title + footer).
</div>
```
## **10.8 Code Blocks vs Components**
| Feature | Code Blocks | Components |
|---------|-------------|-------------|
| Syntax | `runcb="true"` | `runat="server"` |
| Creates Object | No | Yes |
| Interactive | No | Yes |
| Database Binding | No | Yes (dtable/dfield) |
| Gate access | Callback only | `getComponent()` / `comp_*_on_*` hooks |
| Best For | Styling, layout | Forms, data display |
| Complexity | Low | Higher |
## **10.9 Chapter Summary**
1. **Code Blocks** (`runcb="true"` + `sphp-cb-[name]`) transform plain tags server-side
2. **One value per attribute** in this build — `$args[0]` is the whole string; split
commas yourself in your callback when you need multi-args
3. **Compose** multiple blocks by adding multiple `sphp-cb-*` attributes
4. **Define** project blocks in `masters/sphpcodeblock.php`; re-registering a shared
name overrides it (theme without touching FrontFiles)
5. **Shared UI-kit blocks** (`card`, `border`, `color`, `shadow`, `size`, `rounded`,
`align`, `label`, …) work out of the box — verified on `ex08codeblock.html`
6. Use code blocks for **styling/layout**, components for **interactive/data** tags
---
*Next: Chapter 11 - Putting It All Together*