Created
July 23, 2026 17:27
-
-
Save colemanw/2306860feaae1bf8502542f33488176f to your computer and use it in GitHub Desktop.
Script to add `nofilter` to html smarty blocks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env php | |
| <?php | |
| /** | |
| * Recursively updates *.tpl files to add 'nofilter' to any {ts} block containing markup. | |
| * | |
| * Usage: | |
| * php add_nofilter.php /path/to/directory | |
| */ | |
| $targetDir = $argv[1] ?? '.'; | |
| if (!is_dir($targetDir)) { | |
| fwrite(STDERR, "Error: Directory '{$targetDir}' does not exist.\n"); | |
| exit(1); | |
| } | |
| $dirIterator = new RecursiveDirectoryIterator($targetDir, RecursiveDirectoryIterator::SKIP_DOTS); | |
| $iterator = new RecursiveIteratorIterator($dirIterator); | |
| $scanned = 0; | |
| $updated = 0; | |
| // Regex matching Smarty {ts ...} ... {/ts} tags across single or multiple lines | |
| $pattern = '/\{ts\b([^}]*)\}(.*?)\{\/ts\}/s'; | |
| foreach ($iterator as $file) { | |
| if ($file->isFile() && $file->getExtension() === 'tpl') { | |
| $scanned++; | |
| $filePath = $file->getPathname(); | |
| $content = file_get_contents($filePath); | |
| if ($content === false) { | |
| continue; | |
| } | |
| $newContent = preg_replace_callback($pattern, function ($matches) { | |
| $attributes = $matches[1]; // Opening tag parameters (e.g., " 1=$foo") | |
| $body = $matches[2]; // Content between opening and closing tags | |
| // 1. Check if markup (<) is present in the block body | |
| if (strpos($body, '<') === false) { | |
| return $matches[0]; // No markup, leave unchanged | |
| } | |
| // 2. Check if 'nofilter' is already present in the opening tag parameters | |
| if (preg_match('/\bnofilter\b/', $attributes)) { | |
| return $matches[0]; // Already present, leave unchanged | |
| } | |
| // 3. Prepend 'nofilter' inside the opening {ts ...} tag | |
| return '{ts nofilter' . $attributes . '}' . $body . '{/ts}'; | |
| }, $content); | |
| // Save changes if content was modified | |
| if ($newContent !== null && $newContent !== $content) { | |
| file_put_contents($filePath, $newContent); | |
| echo "Updated: {$filePath}\n"; | |
| $updated++; | |
| } | |
| } | |
| } | |
| echo "\nDone. Scanned {$scanned} .tpl file(s), updated {$updated} file(s).\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment