LAPS Posted March 5, 2020 Share Posted March 5, 2020 Hi there, I'm working on an email message template and would like to render a custom one to be sent. I tried to use $files->render() this way // rendering file e.g. a template file or a hook statement in ready.php $body = $files->render('emails/myfile', array( 'myVar1' => $var1, 'myVar2' => $var2, ... )); but $vars seems to be not accessible from within the rendered file. That is, $vars seems to be not set: // rendered file emails/myfile.php $myVar1 = $vars['myVar1']; // is not set $myVar2 = $vars['myVar2']; // is not set ... // is not set How can I get the $vars so to properly render the custom message template for sending the email? Notes: By using $files->include() the $vars is accessible but I cannot use this method because I should retrieve the output $body as a return value for sending the email later. So, I'm still looking to render the custom message using $files->render(). This thread is someway related to this one. Link to comment Share on other sites More sharing options...
Robin S Posted March 5, 2020 Share Posted March 5, 2020 When you pass in variables to $files->render() as ['myVar1' => $var1, 'myVar2' => $var2] then they are available in the rendered file as $myVar1 and $myVar2. Link to comment Share on other sites More sharing options...
LAPS Posted March 5, 2020 Author Share Posted March 5, 2020 @Robin S OK for $myVar1 and $myVar2 (string variables) 🙂 ... but it seems do not work when I pass a PW Page 😕 // rendering file $myPage = $pages->get(12345); $body = $files->render('emails/myfile', array( 'myPageVar' => $myPage )); // rendered file emails/myfile.php isset($myPageVar); // returns false Strange 🤔 Link to comment Share on other sites More sharing options...
Robin S Posted March 5, 2020 Share Posted March 5, 2020 2 minutes ago, LAPS said: but it seems do not work when I pass a PW Page Perhaps a typo or other error in your code. I use $files->render() regularly and can pass in objects without any problem. Link to comment Share on other sites More sharing options...
LAPS Posted March 5, 2020 Author Share Posted March 5, 2020 4 minutes ago, Robin S said: Perhaps a typo or other error in your code. You are right. Thanks Link to comment Share on other sites More sharing options...
psy Posted August 21, 2020 Share Posted August 21, 2020 Had the same issue as @LAPS, ie $vars not accessible in the rendered template. Fixed by including an empty array for $options, ie: <?php $vars = [ 'items' => $items, 'homepage' => pages(1), 'category' => $category ]; echo $files->render('inc/item-detail',$vars,[]); Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now