sebastianbergmann/php-text-template · error · RuntimeException

Writing rendered result to "%s" failed

Error message

Writing rendered result to "%s" failed

What it means

renderTo() renders the template with the currently set variables and writes the result to $target via file_put_contents(). If that call returns false — the OS refused or failed the write — the library wraps it in a RuntimeException with the target path in the message. The @ suppression is applied so the underlying PHP warning does not double-report; this exception is the only signal you get.

Solutions

  1. Verify the target directory exists and is writable by the PHP process user (is_dir + is_writable), creating it with mkdir($dir, 0777, true) if needed
  2. Check that the target itself is not an existing directory and, if a file, is writable
  3. Check filesystem-level causes: disk full (df -h), read-only mount, quota, SELinux/AppArmor, or open_basedir restrictions
  4. As a workaround, render() to a string and write it yourself so you control error handling and can fall back to another location

Example fix

// before
$template->renderTo('/var/lib/app/output/result.php');

// after
$dir = '/var/lib/app/output/result.php';
if (!is_dir(dirname($dir))) {
    mkdir(dirname($dir), 0777, true);
}
if (!is_writable(dirname($dir))) {
    throw new RuntimeException('Output directory not writable: ' . dirname($dir));
}
$template->renderTo($dir);
Defensive patterns

Strategy: try-catch

Validate before calling

if (!is_dir(dirname($target)) || !is_writable(dirname($target)) || (file_exists($target) && !is_writable($target))) {
    throw new RuntimeException('Cannot write to ' . $target);
}

Try / catch

try {
    $template->renderTo($target);
} catch (\RuntimeException $e) {
    error_log($e->getMessage());
    // fall back to an alternate output location or abort
}

Prevention

When it happens

Trigger: Calling $template->renderTo('/path/to/out') where the target path is in a non-writable directory, the file exists but is not writable, an intermediate directory does not exist, the path is a directory, the disk is full, or SELinux/open_basedir blocks the write.

Common situations: Code-generation or report-export scripts run by a deploy user lacking write permission on the output directory; CI containers with read-only filesystems; a hardcoded relative output path resolved against an unexpected CWD; missing parent directory after a config change; full /tmp or disk quota exceeded.

Understand the failure class

Background: "failed to write file", "Could not save figure", "Error saving remote file" — file write failed: causes and fixes across languages and libraries — this error's family across 38 libraries.

Related errors


AI-assisted analysis of sebastianbergmann/php-text-template@1e6083ad3a (2026-09-14). Data as JSON: /api/errors/2d80bd5b1d7980bb. Report an issue: GitHub.

Appendix: source

Thrown at src/Template.php:88

    public function render(): string
    {
        $keys = [];

        foreach (array_keys($this->values) as $key) {
            $keys[] = $this->openDelimiter . $key . $this->closeDelimiter;
        }

        return str_replace($keys, $this->values, $this->template);
    }

    /**
     * @codeCoverageIgnore
     */
    public function renderTo(string $target): void
    {
        if (@file_put_contents($target, $this->render()) === false) {
            throw new RuntimeException(
                sprintf(
                    'Writing rendered result to "%s" failed',
                    $target,
                ),
            );
        }
    }

    /**
     * @param non-empty-string $file
     *
     * @throws InvalidArgumentException
     *
     * @return non-empty-string
     */
    private function loadTemplateFile(string $file): string
    {
        if (is_file($file)) {

View on GitHub (pinned to 1e6083ad3a)