In looking over the docs on template rendering I am not seeing a way to pass in an Entry model. It seems more efficient to pass in an existing model rather than have the template do another lookup. Something like:

$html = craft()->templates->render('path/to/template', $myEntry); 

Here is my use case. I want to use the craft template rendering engine to compose email messages.


You can just pass in the EntryModel straight to the view.

$html = craft()->templates->render('path/to/template', [ 'entry' => $entryModel ]); 
  • Okay, exactly what I was looking form. Can you tell me where you locate this info in the docs? I could not find it. Sep 23 '14 at 18:09
  • I didn't. I was looking for the same thing, and after a bit of fumbling about, looking at the rendering process etc., I simply tried passing it, and it worked. All the hard lifting is done by the models, I think. The render layer just show whatever is passed to it, so it's as simple as that. Sep 23 '14 at 18:11
  • This answer is spot on... It's not documented, but this is exactly the right way to do it. Twig templates will handle Model objects just as easily as the PHP does, basically treating it as an associative array.
    – Lindsey D
    Sep 23 '14 at 19:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.