I
I
Ivan Karabadzhak2014-01-07 01:52:02
Yii
Ivan Karabadzhak, 2014-01-07 01:52:02

Is it possible to write the result of the render method for a view into a variable (php, Yii framework)?

I need to send notification emails to users (letters are not text/plain, but text/html). I would like to have for each template letter, its own view (view) and do something like:

$body = $this->renderToString('hello_mail_view');
mail($email, $subject, $body, $headers);

Is there such a possibility?
Also an additional question. To send letters, I created a class in the components folder and in the controllers, where a certain event occurs, using this component, I send a letter to the user. To what extent is this solution optimal from the point of view of the MVC concept? Perhaps there are other solutions, tell me.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
A
Alexander Gubarev, 2014-01-07
@Jakeroid

I need to send notification emails to users (letters are not text/plain, but text/html). I would like to have for each template letter, its own view (view) and do something like:
Is there such a possibility?
Yes. To do this, you can use the renderPartial method with the return(true) parameter. The code will look like this:
$body = Yii::app()->controller->renderPartial('hello_mail_view', array('param'=>'value'), true);
mail($email, $subject, $body, $headers);

I recommend that you read the official documentation on the topic:
It's a perfectly normal solution. You can issue it as an extension.
I would recommend using existing extensions for sending mail than inventing your own "bicycle".

E
Eugene, 2014-01-07
@Nc_Soft

Allows you to create views for
yii-mail messages

M
Mike, 2014-01-07
@Mike77

you can try ob_get_contents

M
Mikhail Osher, 2014-01-07
@miraage

www.yiiframework.com/doc/api/1.1/CController#rende...
See what the third parameter does...
RenderPartial will work for you though.
www.yiiframework.com/doc/api/1.1/CController#rende...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question