Implement dynamic language system with automatic detection
- Add getAvailableLanguages() method to scan lang directory automatically
- Add getNativeLanguageName() method for proper language display names
- Enhance SimpleTemplate engine to support array iteration with {{#array}} syntax
- Update header template to use dynamic language dropdown with native names
- Add German (de.php) and French (fr.php) language files as examples
- Fix search input text color to use black text for better visibility
- Languages now appear automatically when added to engine/lang/ without code changes
This commit is contained in:
@@ -39,8 +39,42 @@ class SimpleTemplate {
|
||||
|
||||
// Handle conditional blocks
|
||||
foreach ($this->data as $key => $value) {
|
||||
if (is_array($value) || (is_string($value) && !empty($value))) {
|
||||
// Handle {{#key}}...{{/key}} blocks
|
||||
if (is_array($value)) {
|
||||
// Handle {{#key}}...{{/key}} blocks for arrays (iteration)
|
||||
$pattern = '/{{#' . preg_quote($key, '/') . '}}(.*?){{\/' . preg_quote($key, '/') . '}}/s';
|
||||
if (preg_match($pattern, $template, $matches)) {
|
||||
$blockTemplate = $matches[1];
|
||||
$replacement = '';
|
||||
|
||||
foreach ($value as $item) {
|
||||
if (is_array($item)) {
|
||||
// Create a temporary template instance for nested data
|
||||
$tempTemplate = new self();
|
||||
$tempTemplate->data = $item;
|
||||
$replacement .= $tempTemplate->renderTemplate($blockTemplate);
|
||||
} else {
|
||||
// Simple array, replace {{.}} with the item value
|
||||
$itemBlock = str_replace('{{.}}', htmlspecialchars($item, ENT_QUOTES, 'UTF-8'), $blockTemplate);
|
||||
$replacement .= $itemBlock;
|
||||
}
|
||||
}
|
||||
|
||||
$template = preg_replace($pattern, $replacement, $template);
|
||||
}
|
||||
|
||||
// Handle {{^key}}...{{/key}} blocks (negative condition for empty arrays)
|
||||
$pattern = '/{{\^' . preg_quote($key, '/') . '}}(.*?){{\/' . preg_quote($key, '/') . '}}/s';
|
||||
if (empty($value)) {
|
||||
// Show the content if array is empty
|
||||
if (preg_match($pattern, $template, $matches)) {
|
||||
$template = preg_replace($pattern, $matches[1], $template);
|
||||
}
|
||||
} else {
|
||||
// Remove the content if array is not empty
|
||||
$template = preg_replace($pattern, '', $template);
|
||||
}
|
||||
} elseif ((is_string($value) && !empty($value)) || (is_bool($value) && $value === true)) {
|
||||
// Handle {{#key}}...{{/key}} blocks for truthy values
|
||||
$pattern = '/{{#' . preg_quote($key, '/') . '}}(.*?){{\/' . preg_quote($key, '/') . '}}/s';
|
||||
if (preg_match($pattern, $template, $matches)) {
|
||||
$replacement = $matches[1];
|
||||
|
||||
Reference in New Issue
Block a user