-
Kostia
Newbie
-
Posts: 2
Threads: 1
Joined: Dec 2015
Reputation:
0
Hi!
Sorry for my english.
I just started learning framework, and I have a question:
How to insert view into another view?
In documentation:
PHP Code: <?php
class Page extends CI_Controller {
public function index() { $data['page_title'] = 'Your title'; $this->load->view('header'); $this->load->view('menu'); $this->load->view('content', $data); $this->load->view('footer'); }
}
But I wish that there was a basic template, which would connect their content file for each page
Example:
PHP Code: <?php
class Page extends CI_Controller {
//Basic template (header + empty content + footer) public $template = 'views/main_template';
public function index() { //Load only content file for index page }
}
How to do it correctly?
Thank you!
-
edwade3
Newbie
-
Posts: 1
Threads: 0
Joined: Oct 2015
Reputation:
0
They way achieve this is to have a main view that is loaded (placed in the root of the views folder called main_view.php). In this file, I have a line as follows:
PHP Code: $this->load->view($view,$data);
In my controller I then do the following:
PHP Code: $data['view'] = 'users/login'; $this->load->view('main_view',$data);
This then loads your main view and your subview and the $data variables are available to all the views.
-
rakeshshrs
Newbie
-
Posts: 4
Threads: 0
Joined: Nov 2015
Reputation:
0
This should work:
PHP Code: <?php $this->load->view('file_name');?>
-
nasser.man
Member
-
Posts: 98
Threads: 19
Joined: Apr 2015
Reputation:
3
you can get output of view using this command:
$output_of_view1 = $this->load->view('path_to_view1" , $data , true);
then in view2 , you can echo $output_of_view1, remember to send $output_of_view1 to view2,
$data['view1'] = $output_of_view1;
$this->load->view('path_to_main_view' , $data );
i hope this helps,
-
cartalot
You Can't Fax Glitter
-
Posts: 354
Threads: 13
Joined: Nov 2014
Reputation:
17
so for example a controller that shows blog pages
in your controller constructor - define the folder your blog view files are in
and the template name
PHP Code: // the folder your content files are in $this->templatefolder = 'blog' ; // the template name $this->view_template = 'blog_template' ;
in a method when you are ready to call some views
PHP Code: $data['content01'] = 'search_articles'; $data['content02'] = 'main_article'; $data['content03'] = 'suggested_articles'; $this->load->view( $this->view_template, $data );
the template itself
views/blog_template.php
PHP Code: // opening html etc that is generic to website $this->load->view('tmpl_open');
// top nav bar that is generic to website $this->load->view('tmpl_header');
// this is optional but IF the template folder is not set // we have a default folder called 'pages' to look in for the content views
// but in this example the folder is set to be 'blog' // so the blog view files will be in application/views/blog/search_articles.php etc etc if( isset($this->templatefolder)){
$templatefolder = $this->templatefolder . '/' ; }
else { $templatefolder = 'pages/'; }
// header that is specific for the content $this->load->view($templatefolder . 'header');
// so in this specific example its going to load 3 view files, but this part is completely flexible if(isset($content01)) $this->load->view($templatefolder.$content01);
if(isset($content02)) $this->load->view($templatefolder.$content02);
if(isset($content03)) $this->load->view($templatefolder.$content03);
if(isset($content04)) $this->load->view($templatefolder.$content04);
if(isset($content05)) $this->load->view($templatefolder.$content05);
if(isset($content06)) $this->load->view($templatefolder.$content06);
if(isset($content07)) $this->load->view($templatefolder.$content07);
if(isset($content08)) $this->load->view($templatefolder.$content08);
// example of an optional file that you can uncomment for testing // $this->load->view('objecttesting');
// bottom nav bar generic to website $this->load->view('tmpl_footer');
// closing html etc generic to website $this->load->view('tmpl_close');
-
ozzy mandiaz
A traveller from an antique land
-
Posts: 25
Threads: 4
Joined: Nov 2014
Reputation:
2
i am thinking (its a dangerous prospect, I know) that cartalot's solution is the simplest in that you simply load your views in succession. being a relative neophyte to the CI 3, I couldn't imagine doing this any differently in either 2 or three.
that is, unless I have completely missed the intent or goal of Kostia's question.
OM...
-------------------------
And on the pedestal these words appear:
'My name is Ozymandias, king of kings:
Look on my works, ye Mighty, and despair!'
Nothing beside remains. Round the decay
Of that colossal wreck, boundless and bare
The lone and level sands stretch far away.
-
webcomfort
Newbie
-
Posts: 4
Threads: 0
Joined: Oct 2014
Reputation:
0
12-15-2015, 02:01 PM
(This post was last modified: 12-16-2015, 06:41 AM by webcomfort.)
I think, that you need simple helper, like this:
PHP Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/** * View in view * * @access public * @param string * @param array * @param string * @return string */ if ( ! function_exists('view')) { function view($view, $params = array()) { if(is_file(FCPATH.APPPATH.'views/'.$view.'.php')) { $CI =& get_instance(); return $CI->load->view($view, $params, true); } } }
Load this helper or autoload it. And call function from your view:
PHP Code: <?php echo @view('my_view', array('param'=>'pam-pam')); ?>
-
skunkbad
Senior Citizen
-
Posts: 1,300
Threads: 63
Joined: Oct 2014
Reputation:
86
(12-10-2015, 03:13 AM)nasser.man Wrote: you can get output of view using this command:
$output_of_view1 = $this->load->view('path_to_view1" , $data , true);
then in view2 , you can echo $output_of_view1, remember to send $output_of_view1 to view2,
$data['view1'] = $output_of_view1;
$this->load->view('path_to_main_view' , $data );
i hope this helps,
Yes, the third parameter of Load::view() is the answer. I don't see any reason to use anything else. Most websites are going to have a main template, which pages that are generated as content inserted into the main template. Setting the third parameter of view() to TRUE ensures that the view is not output, but returned.
-
Kostia
Newbie
-
Posts: 2
Threads: 1
Joined: Dec 2015
Reputation:
0
Thank you all for answers!
I wrote my code using the third parameter of Load::view('path_to_view1" , $data , true)
It looks like this:
Controller
PHP Code: <?php defined('BASEPATH') OR exit('No direct script access allowed');
class Main extends CI_Controller {
public $template = 'template'; //Default template public $content = ''; //Content view public $data = array( 'title' => 'Site_name', //Default title 'h1' => 'Site_name' //Default h1 // ... Other values ); //Apply settings public function base() { //Content view $this->data['content'] = $this->load->view($this->content, $this->data, true);
//Default template $this->load->view($this->template,$this->data); } //Some page public function index() { //Page data $this->data['title'] = 'Title for this psge'; $this->data['h1'] = 'H1 for this psge'; // ... Other values //Content view $this->content = 'index_view'; //Apply settings $this->base(); } }
View template.php
Code: <!doctype html>
<head>
<title><?= $title ?></title>
</head>
<body>
<!-- Head and menu html -->
...
<!-- /Head and menu html -->
<?= $content ?>
<!-- Footer html -->
...
<!-- /Footer html -->
</body>
</html>
Thank you!
|