codeigniter : pass data to a view included in a view - php

I have a controller and including two views from one function as below
$this->load->view('includes/header',$data);
$this->load->view('view_destinations',$data);
The view file view_destinations.php including a php menu file as follows
<? $this->load->view('includes/top_menu'); ?>
My question is, how can I pass data that is fetched from the controller to this included top_menu.php ?
Thank you guys

Inside your controller, have
$data['nestedView']['otherData'] = 'testing';
before your view includes.
When you call
$this->load->view('view_destinations',$data);
the view_destinations file is going to have
$nestedView['otherData'];
Which you can at that point, pass into the nested view file.
<? $this->load->view('includes/top_menu', $nestedView); ?>
And inside your top_menu file you should have $otherData containing 'testing'.

castis's solution works
however if you want to do this on a more finely grained level you can use:
//in your controller
$data['whatever'] = 'someValue';
.
//In your view
echo $whatever //outputs 'someValue';
//pass $whatever on
$this->load->view('some/view', Array('whatever' => $whatever));

This Codeigniter forum post should help you ;)
You can either make your $data (example) global in the controller, or pass just like #castis mentioned from within your view (variables only in your view)
More details here:
http://codeigniter.com/forums/viewthread/88335/

I have seen in my view files, if I'm passing data from controller to view and from that view to included nested view files. there is no need to transfer
$data
for your nested view it is already available. you can directly access it, within your inner view.

Also try this to if you want every single CodeIgniter view data in a subview:
echo $this->view('subview', get_defined_vars()['_ci_data']['_ci_vars'])

Related

get the html of .ctp file in CAKEPHP

I have deployed an app using cakephp. Now i have some data which i want to render on some logic. This data should be showed as a page including some inputs and other fields.
One thing is to do this in javascript side but it will not be a suitable option to make a whole html in javascript.
Other solution on which i am working is to pass the data to the .ctp file. Get the html and pas this to my content.
I am trying to do this as
$html = $this->render('myview');
$this->set('html', $view);
but it render the myview as a page not as html to be shown to my view.
Follow the code to achieve the same, (this code in your controller's method/action)
//Variables used in view
$data = $this->Admins->newEntity();
// create a builder
$builder = $this->viewBuilder();
// configure as needed
$builder->setLayout('default');
$builder->setTemplate('/Admins/login'); //Here you can use elements also
$builder->setHelpers(['Html']);
// create a view instance
$view = $builder->build(compact('data')); //Pass the variables to the view
// render to a variable
$output = $view->render();
//Print output
pr($output);
die;
From what I understood from your question and comments, you want to render something in a certain place of your view, but you want to put code for that component in separate .ctp file. CakePHP already has built in tools to achieve this - Elements and Cells.
Elements are reusable pieces of code, which resides in src/Template/Element directory. To create an element, simply create new .ctp file (eg. myelement.ctp) in this directory, and output it in your main view using:
<?= $this->element("myelement") ?>
Elements have access to variables passed to view from which are called, but if needed, one can also pass variables as second argument:
<?= $this->element("myelement", ["somevariable" => "somevalue"]) ?>
On the other hand, you have also Cells, which should be used in need to create an reusable component which depends on some logic which should be separated from controller code. Cell consists of two parts - a Cell class, which is equivalent for controller, and template file. It can be baked by:
bin/cake bake cell Sample
This command will create src/View/Cell/SampleCell.php and src/Template/Cell/Sample/display.ctp files. In first of them, a standard controller logic can be performed. In second, you can put HTML and access to passed variables. Cell can be rendered into a view by using:
<?= $this->cell("Sample") ?>
More info can be found in docs:
Elements
View Cells

Yii render variable not view file

question is: Does Yii have any method(s) to render variable with code in it?
Default:
$this->render('site/index'); where site/index is path to view file.
What I need to do is:
$content = '<div><?php echo "do something here"; ?></div>';
$this->render($content);
Output should be layout + parsed content
I tried to use $this->renderText($content); but this methods returns empty string.
I'm using Smarty extension to render view files, then $this->renderText($content); returns not parsed string: {assign ..}
Any help would be appreciated.
There are some rendering Functions in Yii you could use to get there, but not with a variable. It would be better to use renderFile(). If you want to pass data to this file use
$this->renderFile("renderfile.php",array("var1"=>"xyz","var2"=>"abc");
More render funtions described here

Yii render view in another view

I have a rendered view in my application and when I export to PDF I'd like to use the data that I rendered before. Now I'm using another way to do it, I load all things again to export. How can I do it?
I've used this code but don't had success:
array('label'=>'Export pdf', 'url'=>array($this->renderPartial(
'ViewPDF2', array('sessao' => $GLOBALS['session'],'name_project'=>$model->name_project,'id_project'=>$model->i‌d_project,'dataStart'=>$model->data_start,'dataEnd'=>$model->data_end))))
I'm not sure how you've got that code in your view? You're passing that array as an argument to some function? All we can see right now is you're defining an array.
The general approach for rendering a view within in a view would be something like this (this is the container view file):
<?php //view code here ?>
<!-- some html in your view-->
<div id='included_view' >
<?php $this->renderPartial('viewName', array('argForView'=>$foo)); ?>
</div>
<!-- rest of view -->
Note that the use of $foo there means that you previously passed that variable to the container view (or that you defined $foo in a PHP code block before the renderPartial)
In url parameter you should use an URL Address which refer to an action which that action render the view that exporting to PDF.
The renderPartial method just render a view file in your self layout format.

How to switch layout files in Zend Framework?

I'm sure it's a simple one-liner, but I can't seem to find it.
How can I use a different layout file for a particular action?
Update: This worked for me, thanks!
// Within controller
$this->_helper->_layout->setLayout('other-layout') //other-layout.phtml
//Within view script
<?php $this->layout()->setLayout('other-layout'); ?>
From inside a Controller:
$this->_helper->layout->setLayout('/path/to/your/layout_script');
(via these docs)
EDIT: I should mention that the path is relative to whatever your layout directory is (by default, it's application/layouts/scripts/)
You can also use like this
// Within controller
Zend_Layout::getMvcInstance()->setLayout('layout_name');
//Within view script
<?php $this->layout()->setLayout('layout_name'); ?>
Your layout must be in /layouts/scripts/ folder, otherwise you need to specify the path also. No need to write .phtml, just name of the layout

Zend organization question

So I had a question on general organization of code for the Zend framework with regard to the layout.
My layout is basically this:
(LAYOUT.PHTML)
<div id='header'>
<?= $this->Layout()->header ?>
</div>
<div id='main'>
<?= $this->Layout()->main ?>
</div>
<div id='footer'>
<?= $this->Layout()->footer ?>
</div>
and so on and so forth. Now, in order to keep my code in my header separate from the code of my main and the code of my footer, I've created a folder for my view that holds header.phtml, main.phtml, footer.phtml. I then use this code to assign the content of header.phtml into $this->layout()->header:
(INDEX.PHTML)
$this->Layout()->header = file_get_contents('index/header.phtml');
$this->Layout()->main = file_get_contents('index/main.phtml');
$this->Layout()->footer = file_get_contents('index/footer.phtml');
That was working great, but I've hit a point where I don't want main to be static HTML anymore. I would like to be able to insert some values with PHP. So in my Controller in indexAction, I want to be able to load from my database and put values into index/main.phtml. Is there a way to do this without restructuring my site?
If not is there a way to do it so that I can have:
The ability to put code into different sections of my layout, such as Layout()->header, Layout->footer.
Separate these pieces into different files, so that they're easy to find and organize, like my index/footer.phtml, index/main.phtml etc.
Not have to put that code into quotes unnecessarily to turn it into a string to pass it to Layout()->header etc.
Thank you guys so much for your help.
-Ethan
Here is an idea:
Assign layout()->header the filename instead of the contents.
Put your code in this file
In your layout file, include() or require() the layout->header().
Since your layout headers/footers are now parsed, you can use them just like a view.
The ->header in $this->layout()->header is response segment. You can render parts of response using $this->_helper->viewRenderer->setResponseSegment('header'); in an action.
If you use
$this->layout()->header = $this->render('index/header.phtml');
It will even use the view, therefore keeping all your variables defined when rendering the header.
I would suggest using something like
<?php echo ($header = $this->layout()->header)?
$header : $this->render('headerDefault.phtml'); ?>
in your layout file - it will render a default header from the layout folder if the view script doesn't override it.
Have you tried looking at view helpers. They are a way of structuring view logic into reusable and modular code. In this case you would use a view helper to generate each of your required segments. So your example view script would look like
$this->Layout()->header = $this->header();
$this->Layout()->main = $this->main();
$this->Layout()->footer = $this->footer();
The benefit of using view helpers over include and require statements is that all of the file handling and name resolution is handled by the framework. The manual has more information on how to set up the paths and usage examples etc.
helpers are good. Another option is like the above, putting filenames in header/footer - put the template names and use $this->render($this->layout()->header)), etc etc. This is just like the include/require above, but more consistent.

Categories