PHP Developer's Guide to Customizing Form Templates in the Template Engine
For PHP developers looking to create custom form templates in a more native PHP approach, the template engine offers a flexible method to redefine form elements. This customization allows for greater control and integration with the backend logic.
Implementing PHP-based Customization:
-
Remove the HTML Form Template:
If you have an existing `forms.html` with predefined templates for form elements (`Input`, `Textarea`, etc.), you'll need to remove or bypass this file. The template engine first checks for the existence of an HTML template file, then looks for a PHP method, and finally falls back to the default template if neither is found.
-
Define PHP Functions for Form Elements:
Use the template engine's methods (`Input`, `Textarea`, `Checkbox`, `Select`, `Radio`) to define custom rendering functions for each form element. These functions should return the HTML string for the respective form element.
Example Implementations:
Customizing the Input Element:
$templateEngine->Input(function ($attr) {
$html = '';
if ($attr['isHidden']) {
$html .= '<input type="hidden" ' . $attr['chain'] . '>';
} else {
$html .= '<div class="form-control form-input' . ($attr['isInvalid'] ? ' invalid' : '') . '">';
$html .= '<label>' . ($attr['label'] ?? '') . ($attr['isRequired'] ? '<span class="asterisk">*</span>' : '') . '</label>';
$html .= '<input ' . $attr['chain'] . ' ' . ($attr['isRequired'] ? 'required' : '') . ' ' . ($attr['isReadOnly'] ? 'readonly' : '') . ' >';
if ($attr['isInvalid'] && !empty($attr['error'])) {
$html .= '<span class="error">' . $attr['error'] . '</span>';
}
if ($attr['info'] ?? false) {
$html .= '<span class="info">' . $attr['info'] . '</span>';
}
$html .= '</div>';
}
return $html;
});
Customizing the Textarea Element:
$templateEngine->Textarea(function ($attr) {
$html = '<div class="form-control form-textarea' . ($attr['isInvalid'] ? ' invalid' : '') . '">';
$html .= '<label>' . ($attr['label'] ?? '') . ($attr['isRequired'] ? '<span class="asterisk">*</span>' : '') . '</label>';
$html .= '<textarea ' . $attr['chain'] . ' ' . ($attr['isRequired'] ? 'required' : '') . ' ' . ($attr['isReadOnly'] ? 'readonly' : '') . ' >' . $attr['value'] . '</textarea>';
if ($attr['isInvalid'] && !empty($attr['error'])) {
$html .= '<span class="error">' . $attr['error'] . '</span>';
}
if ($attr['info'] ?? false) {
$html .= '<span class="info">' . $attr['info'] . '</span>';
}
$html .= '</div>';
return $html;
});
Customizing the Checkbox Element:
$templateEngine->Checkbox(function ($attr) {
$html = '<div class="form-control form-checkbox' . ($attr['isInvalid'] ? ' invalid' : '') . '">';
$html .= '<input type="checkbox" ' . $attr['chain'] . ' ' . ($attr['isRequired'] ? 'required' : '') . ' ' . ($attr['isReadOnly'] ? 'readonly' : '') . ' ' . ($attr['isChecked'] ? 'checked' : '') . ' />';
$html .= '<label>' . ($attr['label'] ?? '') . ($attr['isRequired'] ? '<span class="asterisk">*</span>' : '') . '</label>';
if ($attr['isInvalid'] && !empty($attr['error'])) {
$html .= '<span class="error">' . $attr['error'] . '</span>';
}
if ($attr['info'] ?? false) {
$html .= '<span class="info">' . $attr['info'] . '</span>';
}
$html .= '</div>';
return $html;
});
Customizing the Select Element:
$templateEngine->Select(function ($attr) {
$html = '<div class="form-control form-select' . ($attr['isInvalid'] ? ' invalid' : '') . '">';
$html .= '<label>' . ($attr['label'] ?? '') . ($attr['isRequired'] ? '<span class="asterisk">*</span>' : '') . '</label>';
$html .= '<select type="checkbox" ' . $attr['chain'] . ' ' . ($attr['isRequired'] ? 'required' : '') . ' ' . ($attr['isReadOnly'] ? 'readonly' : '') . ' />';
if (!empty($attr['data'])) {
foreach ($attr['data'] as $value => $title) {
$html .= '<option value="' . $value . '" ' . ((!empty($attr['selected']) && $attr['selected'] == $value) ? 'selected' : '') . '>' . $title . '</option>';
}
}
$html .= '</select>';
if ($attr['isInvalid'] && !empty($attr['error'])) {
$html .= '<span class="error">' . $attr['error'] . '</span>';
}
if ($attr['info'] ?? false) {
$html .= '<span class="info">' . $attr['info'] . '</span>';
}
$html .= '</div>';
return $html;
});
Customizing the Radio Element:
$templateEngine->Radio(function ($attr) {
$html = '<div class="form-control' . ($attr['isInvalid'] ? ' invalid' : '') . '">';
$html .= '<label>' . ($attr['label'] ?? '') . ($attr['isRequired'] ? '<span class="asterisk">*</span>' : '') . '</label>';
if (!empty($attr['data'])) {
foreach ($attr['data'] as $value => $title) {
$html .= '<div class="form-radio">';
$html .= '<input name="' . $attr['name'] . '" type="radio" value="' . $value . '" ' . (($attr['selected'] == $value) ? 'checked' : '') . ' />' . $title;
$html .= '</div>';
}
}
if ($attr['isInvalid'] && !empty($attr['error'])) {
$html .= '<span class="error">' . $attr['error'] . '</span>';
}
if ($attr['info'] ?? false) {
$html .= '<span class="info">' . $attr['info'] . '</span>';
}
$html .= '</div>';
return $html;
});
Customization Process:
- Define custom rendering logic for each form element within the provided PHP functions.
- Utilize the attributes passed in the $attr parameter to dynamically generate the form element's HTML structure.
- Implement custom logic based on the specific needs of your application, like validation, layout, styling, etc.
- Installation - Installing Aurora Template
- Usage - Basic Usage of the PHP NLG Template Engine
- Storage-Setting Directories for Templates and Cache
- Templates - Setting Up Input Templates
- Variables - Setting Variables in the System
- Language Constants- Setting Language Constants
- Cache - Managing Cache Creation
- Router - Standalone Mode with Routing System
- Using the PHP Template Engine
- Blocks - Block Based template engine
- Using variables - How to use variables in templates
- Calculations - Isolation and Sharing Variables
- Include and Import - include vs. import in the Template Engine
- Import - Leveraging `import{}` for Standalone Applications with a Routing System
- Resources - Adding Resources to Templates
- Control Structures - `if`, `else if`, `else`
- Ternary Operator - How to approach to ternary operator
- Macros - Macros in Template Engines - Understanding the Concept
- Custom Macros - Creating Custom Macros in Template Engines
- Forms - Creating Forms in the Template Engine
- Custom forms - Customizing Forms in the Template Engine
- Custom PHP Forms - PHP Developer's Guide to Customizing Form Templates in the Template Engine
- Curly Brackets - Handling Curly Brackets in Text Content
- Error Handling - Error Handling in PHP Template Engine
- Tests