My Laravel app allows users to modify email templates, then save them to database.
Sample:
Customer {{ $customer_name }} has accepted your proposed timing {{ $timing }} for PO {{ $po_no }}
(User can change 'customer' to 'client' etc.. but leave the {{ }} intact )
The app then loads dynamic data below into above template.
$data = array('customer_email'=> "email#mail.com",
'timing'=> "2pm",'po_no' => "PO001"
);
Then email those rendered text to some emails.
How can I do that?
Rephrase:
load text from db
make it a Blade template
render that template with data
I get stuck at #2
add email view to your views/emails folder
ex: mail_template.blade.php
Customer {{ $customer_name }} has accepted your proposed
timing {{ $timing }} for PO {{ $po_no }}
send method
Mail::send('emails.mail_template', array('customer_email'=> "email#mail.com", 'timing'=> "2pm",'po_no' => "PO001" ), function($message)
{
$message->to('sendto#gmail.com')->subject('your email subject');
});
further read Official Doc
Related
I am currently trying to translate a string in Symfony/Twig inside an include Statement for a twig Template.
This is the non-translated Code is use for it currenty:
{{ include('#BluelineUser/user/_form.html.twig', {
'back_link': path('user_index'),
'title' : 'Edit User: ' ~ user.username,
'button_caption': 'Save'
}) }}
And in my form I output it like this:
{{ form_start(form) }}
<h1>{{ title|trans }}</h1>
{{ form_end(form) }}
Now if I use the include with a diffrent title (without a variable in it) it works fine.
Adding this to my translation file just doesn't pick up the 'Edit User: '
<trans-unit id="edit.name2">
<source>Edit User: </source>
<target>Benutzer bearbeiten: </target>
</trans-unit>
Any idea on how to get they 'Edit User: ' in the title translated? I think it's breaking because of the variable.
So far couldn't find any example on how to get this working in an include statement.
Try to translate it before like this:
{{ include('#BluelineUser/user/_form.html.twig', {
'back_link': path('user_index'),
'title' : ('Edit User: '|trans) ~ user.username,
'button_caption': 'Save'
}) }}
I have problem with email template encoding:
This is my template:
mail/user-created.blade.php
#component('mail::message')
# Invoice Paid
Your invoice has been paid!
#component('mail::button', ['url' => '#'])
View Invoice
#endcomponent
Thanks,<br>
{{ config('app.name') }}
#endcomponent
For example in web.php:
Route::get('/test', function () {
$message = (new \App\Notifications\UserCreated(\App\User::first()))->toMail('test#email.com');
$markdown = new \Illuminate\Mail\Markdown(view(), config('mail.markdown'));
return $markdown->render('mail.user-created', $message->toArray());
});
And I get:
Preview in browser
Preview in browser 2
What is wrong?
--
I moved code to the left:
Code left
Code left 2
Your email template has to be left-aligned. Just move everything over to the left.
#component('mail::message')
# Invoice Paid
Your invoice has been paid!
#component('mail::button', ['url' => '#'])
View Invoice
#endcomponent
Thanks,<br>
{{ config('app.name') }}
#endcomponent
I just found solution:
vendor/tijsverkoyen/css-to-inline-styles/src/CssToInlineStyles.php
Line 129:
Replace:
$html = $document->saveHTML($htmlElement);
To:
$html = $document->saveHTML();
But why?
In their new documentation there isn't anything for flash messages.
I installed the flash extension from their github repository (slimphp/Slim-Flash). Everything works fine, I can add messages and can also get these messages.
// Adding a message
$this->flash->addMessage('test', 'This is a message');
// Getting a message
$this->flash->getMessage('test')[0];
But this only works inside routes. Of course I want to have these messages displayed on my view.
But I just don't know how to get this message on the twig view.
I have already tried:
{{ container.flash.message('test')[0] }}
{{ container.flash.getMessage('test')[0] }}
{{ this.flash.message('test')[0] }}
{{ this.flash.getMessage('test')[0] }}
{{ flash.message('test')[0] }}
{{ flash.getMessage('test')[0] }}
{{ app.flash.message('test')[0] }}
{{ app.flash.getMessage('test')[0] }}
{{ container.flash.test }}
Thanks for help!
You can add the flash message inside the data attribute from the render method:
$this->view->render($res, 'path/to/template.twig', [
'flash' => $this->flash
]);
Or you could add a middleware and add your flash instance to the twig parameters
$app->add(function ($request, $response, $next) {
$this->view->offsetSet("flash", $this->flash);
return $next($request, $response);
});
then it should be possible to access the messages inside the twig template with
{{ flash.getMessage('test') }}
Below is my code for a Laravel 4 project.
Going to the authors/create URL and submitting the form gives me a 405 error.
However, if I prepend the routes.php file with Route::post('authors/store', 'AuthorsController#store');, basically doubling what it already should do, everything works like a charm!
Why do I need do prepend said line in my code to work? I can only assume I'm doing something wrong here.
routes.php:
Route::resource('authors', 'AuthorsController');
AuthorsController.php:
public function create() {
$view = View::make('authors.create');
return $view;
}
public function store() {
//
}
authors/create.twig:
{{ form_open({'url':'authors/store'},{"method" : "post"}) }}
<p>
{{ form_label("Name", "name") }}
{{ form_text("name") }}
</p>
<p>
{{ form_submit("Add Author") }}
</p>
{{ form_close() }}
The store action get's trigger when you POST to the resource. So just authors and not authors/store:
{{ form_open({'url':'authors'},{"method" : "post"}) }}
See this table on more information what URL corresponds to what controller action.
Also I think it should be like this:
{{ form_open({'url':'authors', 'method' : 'post'}) }}
And you can pass the route name Laravel automatically generates to make your life a bit easier:
{{ form_open({'route':'authors.store', 'method' : 'post'}) }}
Oh and one more, post is the default method so this should do as well:
{{ form_open({'route':'authors.store'}) }}
I've been working on migrating several of our forms to Laravel, but there's one last step I'm not entirely sure on how to go about. I have a form that does an Insert into a database, but instead of just having 2 pages--the form and the submission page--I have 3: the form, a confirmation and a submission page.
Here is what I have at the moment:
Routes:
Route::any('application/housing-form', array('as'=>'application.form', 'uses'=>'ApplicationController#form'));
Route::post('application/confirmation', array('as'=>'application.confirmation', 'uses'=>'ApplicationController#confirmation'));
Route::post('application/submit', array('as'=>'application.submit', 'uses'=>'ApplicationController#submit'));
ApplicationController:
public function form()
{
$application = new Application;
return View::make('application/form')->with(array('application'=>$application));
}
public function confirmation()
{
$input = Input::all();
//More here?
return View::make('application/confirmation')->with(array('input'=>$input));
}
public function submit() {
$input = Input::all();
DB::table('application')->insert(
array(
<field1> => $input('field1')
...
)
);
return View::make('application/submit');
}
Views:
//form
{{ Form::model($application, array('route'=>'application.confirmation')
//inputs
{{ Form::submit('Continue') }}
{{ Form::close() }}
//confirmation
{{ Form::open(array('route'=>'application.form') }}
{{ Form::submit('Back to my information') }}
{{ Form::close() }}
{{ Form::open(array('route'=>'application.submit') }}
{{ Form::submit('Submit') }}
{{ Form::close() }}
//submission
<p>Thank you for your submission!</p>
What I am unsure about is how to persist the data from the form through the confirmation page and into the submission page. From what I can tell, I can see a few options:
Reflash all of the input
Use a hidden field (or fields) to send the information
Insert the information into the database in the confirmation page and just do an update with an in-between query with the information.
I'm pretty sure it would be the first one: reflashing the data. But if so, I'm not sure where you're actually supposed to call Session::flash or Session::reflash. Or how many times I need to do it to get it through all of the requests. Any suggestions on how to go about that, or how to streamline the rest of the form would be greatly appreciated.
One extra note as well is that this particular form deals with a large number of input fields (around 60). That's part of why I want to avoid having to request each individual field to a minimum.
What I would do is to flash the input to the session in order to repopulate the form. This can be achieved by using the Input::flash() method like so:
public function confirmation(){
Input::flash(); //this will store the input to the session
return View::make('application/confirmation');
}
Then in your view, use the Input::old() method to retrieve input data from the previous request:
{{ Form::text('fieldname', Input::old('fieldname')) }}