What is best practice.
Loading my custom language file in Codeigniter in the view or in the controller and then pass it from the controller to the view?
Controller example:
$data = array( 'registrationSuccess' => $this->lang->line('success'),
'sayHello' => $this->lang->line('hello'));
$this->load->view('view_registration', $data);
You should load the language file in your controller, but you should use it directly in your view.
Related
my controller having the below format. how can i transfer to view in laravel
i tried ->with($inboxMessage) . The browser not even completely loaded. It's loading for a lifetime.
$inboxMessage[] = [
'messageId' => $message_id,
'messageSnippet' => $snippet,
'messageSubject' => $message_subject,
'messageDate' => $message_date,
'messageSender' => $message_sender
];
you can return your view in controller with data like below
return view('viewname', ['inboxMessage' => $inboxMessage]);
now $inboxMessage will available in view.
return view("path to your view", compact('inboxMessage'));
return view("view_name",['data'=>$inboxMessage]);
and in view you can access using foreach loop or use print_r($data)
in view if you are using blade template then
#foreach($data as $val)
{{$val->messageId}}
#endforeach
or else use normal foreach loop
Is it possible to use same _form.php in two different view files with different content in yii framework? If yes then how to achieve it.
Of course you can do it however both models have to have the same attributes.
in views/pleace1/create.php and update.php
<?php $this->renderPartial('/pleace1/_form', array('model'=>$model)); ?>
in views/pleace2/create.php and update.php
<?php $this->renderPartial('/pleace1/_form', array('model'=>$model)); ?>
in views/pleace1/_form.php change form widget start to:
$form = $this->beginWidget('CActiveForm', array(
'id' => 'stl-' . $this->controller_alias . '-form',
in both controllers where you using form set public controller_alias to model name without prefix (if you use it)
In an application I am loading the pages through AJAX calls (instead of the regular browser loading) so I'm wondering if it's possible to add the output from a blade template in a JSON return?
I designed my application around a JSON looking like this:
return Response::json(array('id' => "1",'urlString' => "admin/posts", 'html' => "<p>test</p>"),200);
I need the HTML index in the JSON to be the output from a blade template, is this possible?
Yes, that should be possible by calling the render() method of the view :
$view = View::make('some.view');
$view->somevariable = 'some value';
$html = $view->render();
return Response::json(array('id' => "1",'urlString' => "admin/posts", 'html' => $html);
Can I set a variable in view file?
for exemple:
I have a controller: welcome.php
Its load:
$this->load->view('header');
$this->load->view('main');
$this->load->view('footer');
I need set a variable on file main.php and get on footer.php
Its possible?
You can pass a variable from one view to another just like you would from a controller to a view. You just have to load the view file that sets that variable before the view that uses that variable:
$this->load->view('main'); //load before
$this->load->view('footer'); //load after
Inside of main.php do $this->load->vars(array('your_variable'=>'it's value')); and you will be able to call it in the footer like you would any other variable. The only requirement is that main.php be loaded before footer.php.
It is not possible to set a variable in a view file and access it from another. There is no reason you should be setting variables in your view files anyway. The controller should be handling all of your application logic so you should be setting the variable there. I would recommend reading through the user guide or looking at some articles to get a better grasp of MVC principles.
To access the same variable in multiple views, pass it to each view you load.
// Set your variable
$data['variable'] = 'value';
// Pass variable to multiple views
$this->load->view('main', $data);
$this->load->view('footer', $data);
You can pass both array or object to your view, for example:
$data = new StdClass;
$data->title = "The Title";
$data->content = "The Content";
$this->load->view('main', $data);
or
$data = new SomeClass;
$this->load->view('main', $data);
Also, in each view you are able to pass a different data:
$data1 = array("key" => "val");
$data2 = $this->some_class->some_method($params);
$data3 = $this->another_class->another_method($params);
$this->load->view('navigation', $data1);
$this->load->view('main', $data2);
$this->load->view('footer', $data3);
Yes you can, to pass variable from controller you can type the variable data after the name of view, this is the example :
$data["vehicle"] = $vehicle;
$this->load->view('header',$data);
then in your view just call the name of variable :
<td>
<?php echo $vehicle ?>
</td>
Hope it can help
I have a model, view and controller not interacting correctly, and I do not know where the error lies.
First, the controller. According to the Code Igniter documentation, I'm passing variables correctly here.
function view() {
$html_head = array( 'title' => 'Estimate Management' );
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('html_head', $html_head);
$this->load->view('estimates/view', $estimates);
$this->load->view('html_foot');
}
The model (short and sweet):
function get_estimates() {
$query = $this->db->get('estimates')->result();
return $query;
}
And finally the view, just to print the data for initial development purposes:
<? print_r($estimates); ?>
Now it's undefined when I navigate to this page. However, I know that $query is defined, because it works when I run the model code directly in the view.
$estimates = $this->Estimatemodel->get_estimates();
$this->load->view('estimates/view', $estimates);
You're loading the return of $this->Estimatemodel->get_estimates() as the array of view variables. In other words, all the children of $estimates (assuming it can be treated as an array) are available in your view. But not the parent element.
The key here is when loading a view the second parameter needs to be an array of values, not just a single value.
$this->load->view('estimates/view', array('estimates' => $estimates));
That should get the result you're looking for, in fact, you're already doing that for the html header view. Even though that view only has one variable, it's passed as the single element of an array:
$html_head = array( 'title' => 'Estimate Management' );
$this->load->view('html_head', $html_head);
The documentation shows that the object you pass to the view must be an associative array.
$data = array(
'estimates' => $estimates
);
$this->load->view('estimates/view', $data);
Docs here