Symfony2: Get HTML from another file in controller - php

In my Symfony2 project, I would like to get the HTML of a file and put it in a variable in my controller.
I have a controller function like this:
public function myControllerAction()
{
$htmlOtherFile = '';
return $this->render('#MyBundle/myTargetFile.html.twig', ['htmlOtherFile' => $htmlOtherFile]);
}
The html file I would like to import is in my views folder: htmlOtherFile.html.twig
How can I get the HTML of this file into my $htmlOtherFile variable? I have already tried with file_get_contents, but without success.

all you need to do is call renderView from your controller on the template and put the contents in a variable.
$html = $this->renderView('/path/myTargetFile.html.twig', [/*options in here*/]);
Be sure to call renderView and not render on its own as that will return a Response instance, and not any HTML.
Alternatively, you can call:
$this->render('/path/myTargetFile.html.twig', [/*options in here*/])->getContent();
to return the html from the response.

If it is twig file - you could try this:
$this->renderView('/path/to/template.html.twig', []);

You can call a controller directly from you twig file if you want.
Use the syntax {{ render(controller('MyBundle:functionName')) }}

Related

How to include HTML instead of a blade template for a Laravel Mailable?

For a Mailable, when using the ->view() method, instead of providing a Blade Template, like this
return $this->subject($this->subject)
->view('mybladetemplate');
I would like to provide the HTML inline. For example
return $this->subject($this->subject)
->view('<!DOCTYPE html... etc ');
Is this possible? Is there any other method to do this?

Passing data from blade to controller Laravel

I want to pass a object from the blade file to the controller file. The purpose is when the user click an edit button the user will get a form which is filled with the previous input data. I am using this code in the blade file:
Edit
But When I want to get the passed object from the controller's edit method I get a null. My Controller code is like this now:
public function edit(FeesType $feesType)
{
//
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
Here I have dump the $feesType object but I get a null. Please help me how can I solve this problem.
Thanks in advance
Route model binding works a bit different here is the documentation
What you need to do is have your route like this:
Route::get('feestype/{feesType}/edit', 'YourController#edit')->name('feestype.edit');
then in your view
Edit
-- EDIT
using a resource file:
Route::resource('feestype', 'YourController')
the link will be built the same as above:
{{ route('feestype.edit', $feesType) }}
you should change your Route to :
Route::put('feestype/{id}/edit', 'YourController#edit');
For update and edit you should use put not get.
Note that for this code:
Edit
first you should compact $feestype in YourController then use your code in blade.
Now the code in the blade file is
Edit
The controller file contains this code:
public function edit(FeesType $feesType)
{
//
$feesType = FeesType::find($feesType->id);
dump($feesType->name);
return view('feestype.edit',['feesType'=>$feesType]);
}
And here is my Route definition:
Route::resource('feestype','FeesTypesController');
And the browser shows this message:

issue with passing variable from controller to view in codeigniter

This is my Code Of Controller
$datacc['user_id'] = $this->user_id;
$this->load->view("Templates/header",$this->data);
$this->load->view("Tax/Tax_Search",$datacc);
$this->load->view("Tax/Tax",$datacc);
in this, i want to pass "$datacc" to my both view files (Tax.php And Tax_Search)
but $datacc can't pass to Tax_Search.php file
can anyone help?
Thank You
You cannot call multiple view in one controller function.
This can be done in view file.
I suggest you that you should first create a template file and in that template call your views like this
template.php
$this->load->view("Tax/Tax");
$this->load->view("Tax/Tax_search");
<?php echo $content; ?>
$this->load->view("Tax/footer");
Let me know if you have queries

Return html from controller action in Zend

i have a structure like so:
modules -> Controllers -> Amodule -> IndexController
there i have an action which is called:
public function getTemplateAction(){
... //1) load html file
//2) return html
}
this function is called from javascript with get via the url: baseUrl+'/Amodule/index/get-template/viewid/3.
I would like based on the viewid to return an html template, where should i put the .html file and how can i load it in Zend? i tried
this->partial('thetemplate.html');
and putting the html file in Amodule folder but didnt work.
Thanks in advance!
Added another folder at the same level as controller called it views/scripts and in there i created the partial. Then from the view i load the partial as:
<?php echo $this->partial('_partialName.phtml', array(
var1 => value1,
var2 => value2
/*... vars to pass in the partial*/
));?>
A simple solution is to have your html files in a dedicated folder at the root of your project and output the file with readfile :
public function getTemplateAction() {
readfile(APPLICATION_PATH . '/../templates/thetemplate.html');
exit;
}

Calling function in Yii controller with renderPartial

Code from controller:
public function actionSomeName($param){
do something here...
$this->renderPartial('_formCalculations', array(
'modelX'=>$modelX,
'modelY'=>$modelY,
));
}
I want to call that function in my view, but it won't work.
Is $this->renderPartial correct ? I think not, because it's only for views? But which function do I have to use then?
$this->renderPartial('Controllername/Somename',array("param"=>"value"));
You can't call the controller/action directly in the view.
You can user render/renderPartial. Render/renderPartial reference
a view file, not a controller action
You could put in your logic in the code as well (in your example, the "do something here..." part) but that is not good MVC!
The other alternative is to use a widget
Surly you can`t use renserPartial individually, but you can do it so:
$body = $this->renderPartial('Controllername/Somename',array("param"=>"value"),true);
In this code you have that view file`s scripts and you can use them anywhere you need.

Categories