Jump to content

wireRenderFile not returning string output


abdus
 Share

Recommended Posts

Hi,

I'm working on a custom contact form and I want to create an HTML markup for email body. After tedious and long searches I've found wireRenderFile function that does exactly what I need. Pass in template file and variables to use, it returns the output. Except that it doesn't. It simply returns an empty string.

I tested the same template using wireIncludeFile, it echoes the output perfectly.

(I'm using the latest 2.6.16 DEV version of ProcessWire)

Here's what I did:

  1. Got and processed user inputs
  2. Created a simple partial template file at templates/partials/_email.php with contents
    <?php if (isset($vars)): ?>
    <div class="meta">
        From: <b><?= $vars["name"] ?></b><br>
        Email: <?= $vars["email"] ?><br>
        sent this at <?= $vars["date"] ?>
    </div>
    <hr>
    <div class="message">
        <?= $vars["body"] ?>
    </div>
    <?php endif; ?>
    
  3. Prepare mail data and send it

    $mail = wireMail();
    ...
    ...
    $bodyHTML = wireRenderFile("partials/_email.php", array(
            "name" => $name,
            "body" => $body,
            "email" => $email,
            "date" => date("r"),
        ));
    ...
    ...
    ...
    if ($mail->testConnection()) {
        $mail->sendSingle(true)
            ...
            ->bodyHTML($bodyHTML)
            ->send();
    }
    

The $bodyHTML variable gets an empty string from wireRenderFile function. When I replace it with wireIncludeFile

$bodyHTML = wireIncludeFile("partials/_email.php", array(<data>));

The template is rendered correctly, but echoed instead of returned (it returns boolean anyway). I think I can collect the output using ob_start() and ob_get_contents(), but the code above should work correctly and I dont want to use a hack for it, yet.

Thanks in advance for any help.

---

EDIT:

I solved it.

wireRenderFile function imports given data in $vars variable as separate entities as in 

// Second argument is $vars
$bodyHTML = wireRenderFile("partials/_email.php", array(
        "name" => $name,
        "body" => $body,
        "email" => $email,
        "date" => date("r"),
    ));

// $vars variable is included in template file as in
$name = $vars["name"]
...
// etc.

There's no need (can't) to access to $vars variable. I was able to output the data with this modified template file. Notice the if (isset($vars)) is removed since $vars variable doesnt exist in that context.

<div class="meta">
    From: <b><?= $name ?></b><br>
    Email: <?= $email ?><br>
    sent this at <?= $date ?>
</div>
<hr>
<div class="message">
    <?= $body ?>
</div>
Edited by abdus
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...