This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
I'm trying to use October with as little use of the database that I can (I prefer to keep the overhead low), and I've got the Mail class pulling a template from the filesystem instead of the database (located in the app/views
directory).
That's fine and well, but I'm confused how I can access all of the data and pass it into a twig for
loop. The reason I'm attempting this is because I have a lot of forms that would be using this template, but they don't all have the same fields.
Basically, I just want to do a loop through all the submitted info in a form, and create table rows for each item.
Any ideas, or am I completely missing something?
By access 'all of the data', I presume you mean 'everything that gets passed into the template'?
This stack over flow question says that this isn't possible.
You might be able to use dump()
to get that info, but it'd be hard to get it in a format that you can use to create a table from.
Have you considered passing in an extra variable with all of the other variables in it? (e.g. for the inputs user = ..
, activation_code = ..
, url = ..
, you can pass an extra variable vars = ['user','activation_code','url']
.) I think this might be the most pragmatic way of doing it, even if it isn't as DRY. You'd still have to do some kind of magic with the attribute(..)
function for it to work though.
I don't really think this is a good idea. If you need it for debugging or recoding things, just stick a dump()
in the template with some formatting, and you'll have a listing of all of the variables, otherwise set up a proper template for each form.
Thanks for the answer brokenpixels2325, I ultimately ended up passing an extra variable containing all the data, which isn't as efficient because I end up calling Input::all()
twice, but we only get a few handfuls of these form submissions per day anyways. Below is how I managed to answer my own question.
By 'all of the data', I meant all the data that was posted to the template from a form. When calling Mail::send()
, I pass the template and Input::all()
, as well as the callback function. Using Input:all()
, I was trying to find a way to get that information in an array for the mail template. Unfortunately, I couldn't find a way, so I went with the following workaround:
Mail::send('email.template', Input::all(), function($message){
$message->data = Input::all();
});
In the callback function (the third parameter to Mail::send()
), I ran $message->data = Input::all()
(use Input::except()
to not pass in the token or session key, as well as any other variables you don't need).
I was then able to access the $message->data
variable in the mail template using the following twig code:
{% if message.data %}
<table>
{% for field, value in message.data %}
<tr>
<td>{{ field }}</td>
<td>{{ value }}</td>
</tr>
{% endfor %}
</table>
{% endif %}
This workaround makes the second parameter of the Mail::send()
call useless unless you wanted to easily call one or two variables passed in (maybe an email or name field for addressing the email in a personal manner).
Hope this helps anyone looking to loop through all the data passed into a twig email template in October!
Last updated
1-3 of 3