I'm passing a $emailtemplate variable into a view.
This $emailtemplate is a model with the following properties
(string) 'from_name', (string) 'from_email', (string) 'subject', (string) 'body', (string) 'email_type', (integer) 'status'
User's can create new instances of the $emailtemplate via a FORM on the site, where they can populate each of the fields above.
An example of the body as populated by users into the FORM would be exactly the following line (yes users will be writing HTML code into the form to be stored in the body property of the $emailtemplate):
<strong>Dear {{$user->first_name}} </strong>
I have created a view which will allow users to 'Preview' the email. The variables $emailtemplate and $user are both passed to the view (for the case of the preview the $user = Auth::user())
resources/views/emails/preview.blade.php:
{!! $emailtemplate->body !}}
This 'preview' view correctly renders the strong styling however does not identify the blade variable reference as it is stored within a literal string.
i.e. the HTML that is rendered is exactly
Dear {{$user->first_name}}
What is going wrong here? I want to allow Users to develop their own email templates via a form. I am passing both the $emailtemplate and $user into the view however because the $emailtemplate->body is a string datatype Laravel does not recognise the use of the blade variable between the parenthesis.
How can I fix this?
Thanks for your help
You can't do this since Laravel wont Re-render the blade output, to see and replace any variables that is injected by another variable!
Take a look at https://github.com/TerrePorter/StringBladeCompiler
So, you should evaluate the user defined template first, e.g.
<strong>Dear {{$user->first_name}} </strong>
will become:
<strong>Dear Foo </strong>
This evaluated <strong>Dear Foo </strong> will be stored in $evaluatedTemplate
Then you can use this in your main blade template:
{!! $evaluatedTemplate !!}
Also see:
https://github.com/Flynsarmy/laravel-db-blade-compiler
Related
Here the scenario is I want to pass a variable which will be send from one page to another and in next page it's gonna store through a form. So I have passed the variable from first page to second page through the URL. But I want to hide the parameter in the URL. How do I do it?
Here is my route :
Route::get('/registration/{course_id}',[
'uses'=>'AppController#getregistration',
'as'=>'registration'
]);
And Controller :
public function getregistration($course_id)
{
return view('index')->with('course_id',$course_id);
}
And first page this is how I send the value to first page:
<li> A </li>
Post Method
Route
Route::post('/registration',['uses'=>'AppController#getregistration','as'=>'registration']);
View
{!!Form::open(array('url' => '/registration')) !!}
{!! Form::hidden('course_id', '1') !!}
{!! Form::submit('registration') !!}
{!! Form::close() !!}
Controller
public function getregistration(Request $request)
{
$course_id = $request->input('course_id');
return view('index')->with('course_id',$course_id);
}
Get method
use encryption method, it will show encrypted id in url
View
<li> A </li>
Controller
public function getregistration($course_id)
{
$course_id = Crypt::decrypt($course_id);
return view('index')->with('course_id',$course_id);
}
here is no way you hide parameter in url, rather then you convert parameter value encrypt or hash is up to you,
other-way is save value in session first, then call the value from session without define parameter in url.
because laravel route only working to pattern of url /string /id, post get. dynamic value you must be writing / getting using pattern method.
Thanks.
You cannot hide a parameter in URL. If you don't want to show the ID then try using SLUG. I hope you understand what is a SLUG. If you don't then here it is. If you course title is My new Course Title then its slug would be my-new-course-title. And make sure it is unique like an ID in the table. It is also good for SEO, readable and looks good.
I have an array that I am setting up as checkboxes
<?php $buslist = array('Brooklyn','Lakewood'); ?>
#foreach ($buslist as $buses)
{{Form::label('brooklyn_1',$buses)}}
{{Form::checkbox('BusList2[]', $buses,false, ['id'=> $buses]) }}
#endforeach
Then I switch it to an string with a , using implode
But when I try to edit my information, none of the checkboxes are selected and the information gets lost on update.
What code can i put into the blade checkboxes, if that bus is in my string list?
Checkboxes are finicky creatures. In your form you actually have to do something like this
{!! Form::hidden('new-group-user-member', 0) !!}
{!! Form::checkbox('new-group-user-member', true, NULL) !!} Member
When a checkbox is not checked nothing gets sent to the server so you never know when to change the value in the backend. If you add a hidden form field with the same name and a value of 0 it will get sent with the form even though the checkbox is not checked.
In third parameter use in_array($buses, $buslist) instead of false. Your blade code will look like this:
{{Form::checkbox('BusList2[]', $buses,in_array($buses, $buslist), ['id'=> $buses]) }}
up until this point I have essentially been using resource routing. One of my routes is for projects. If I create a project and then SHOW it, I see a URL in the form of
myUrl/projects/1
On the show page for a project, I want to be able to add a document. I have set up the relationships so a project can have one document and a document belongs to a project. I then set up the following route to handle the saving of the documents data
Route::post('projects/{id}/docOne', 'DocOneController#store');
So I add an a form in projects/show.blade.php, which opens like so
{!!
Form::model(new App\DocOne, [
'class'=>'form-horizontal',
'route' => ['docOne.store']
])
!!}
I then have my form fields and a save data button. Because of this new form within my projects show page, when I now show a project, it complains that the route for this new form is not defined.
How can I get this route to work within the projects show page?
Thanks
First of all you need to define a route name to your route, if you want to call it by his name.
So your route would be like:
Route::post('projects/{id}/docOne', [ //you need an array to set a route name
'as' => 'docOne.store', //here define the route name
'uses' => 'DocOneController#store' //here the callback
]);
Second you need to change your laravel form to use your route name and set the id
{!! Form::model(new App\DocOne, [
'route' => ['docOne.store', $project], //if you have setted the id variable like $id blade it gonna retturn it automatically only by passing the object, else, you can set $project->id
'method' => 'POST']
) !!}
EDIT:
You can't get an instance of a model on your view.
So the part:
{!! Form::model(new App\DocOne,
gonna fails every time you trye, also, the form:model needs an instance of a class that should have your vars filled with the info that the inputs should have (when you edit it).
You have two solutions:
If it's a new Doc and never before exist on your dataBase
I recomend to change your
Form::model
to:
Form::open
if it's a Doc thath already exist on your DB, like an edit, so in your controller you need to pass your existing Docas $docand remplace the: {!! Form::model(new App\DocOne, to:
{!! Form::model($doc,
and it works.
Form model was created to fill the input values with the data existing in your object instance, like when you edit someting.
So you need to have a correct instance.
Another think it's the MVC scope, a view shouldn't have acces to models, except if are passed by the controller.
Ok that's all.
I've seen this asked a few times but have never seen an answer. I have a Django ModelForm with a hidden input field. I need to use info from my template for the hidden field - the 'send_to' field in this case.
So I have the form where users can write a subject and a message, and I want to autopopulate the "to" field.
This is my modelform:
class MessageForm(ModelForm):
class Meta:
model = Message
fields = ['subject', 'message', 'send_to']
widgets = {'send_to': forms.HiddenInput()}
The 'send_to' part needs to be populated with info from my template.
<form method="post" action="{% url 'send' %}" id="commiserator" name="{{ c.id }}" class="write-to-user" enctype="multipart/form-data">
<h3>To: {{ c.username }}</h3>
{% csrf_token %}
{{message.as_p}}
<input id="commiserator" class="write-to-user" type="submit" value="Send">
Right now {{c.username}} is just displayed as html. I want it to be used as form input for the hidden field, but I have no idea how to do that.
The form is being sent to the view with Ajax so I would like to make it so that the hidden information is populated before Ajax sends the data. Hope that makes sense.
You can't do it directly from the template. But you have a couple of options.
In your view where you instantiate your message ModelForm to pass to the template, you could pre-populate the value of the send_to field. This should then render it in the template with that same value. This is probably what you want.
Your other option is to exclude send_to from the ModelForm, and to add it hard-coded as a hidden field in the form in your template. {{message.as_p}} will then only render the visible fields.
Im making a edit form for my app and i was wondering if someone could tell me how to get the data from the database into my text field.
I can locate the record i need to edit based on the users click, and i can display the information if i do the following:
value="{{ $letter->subject }}"
BUT, the problem im having is that when i run it through the validation and there is an error, it comes back with the database information instead of the OLD data.
So my questions is. Is there a way to serve up the database information first and then when it goes through the validatior, validate the information the user has edited?
Currently to validate the text field and bring the data back incase of error, im using
Input::old('subject')
Is there a parameter for that old bit that allows me to put in the DB data?
Cheers,
Hey you could validate and return ->withInput() and then in your actual form, check if there is Input::old() and display it, otherwise display from the db.
example:
<input type="text" name="subject"
value="{{ (Input::old('subject')) ? Input::old('subject') : $letter->subject }}">
Or you could go the other way and define the variable and do a regular if statement, instead of the ternary one! Up to you to decide what you want to use!
All you need is form model binding http://laravel.com/docs/html#form-model-binding:
{{ Form::model($letter, ['route' => ['letters.update', $letter->id], 'method' => 'put']) }}
// your fields like:
{{ Form::text('someName', null, ['class' => 'someHTMLclass' ...]) }}
// no default values like Input::old or $letter->something!
{{ Form::close() }}
This way you form will be populated by the $letter data (passed from the controller for example).
Now, if you have on your countroller:
// in case of invalid data
return Redirect::back()->withInput();
then on the redirect your form will be repopulated with input values first, not the original model data.
Make it more simple and clean
<input type="text" name="subject" value="{{ (Input::old('subject')) ?: $letter->subject }}">
I'm not sure for Laravel 4 but in Laravel 5, function old takes second param, default value if no old data in session.
Please check this answer Best practice to show old value