Welcome Guest, Not a member yet? Register   Sign In
How to insert view into another view?
#1

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!
Reply
#2

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.
Reply
#3

This should work:
PHP Code:
<?php $this->load->view('file_name');?>
Reply
#4

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,
ressan.ir
CI is nice Heart
Reply
#5

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'); 
Reply
#6

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.
Reply
#7

(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$paramstrue);
 
       }
 
   }


Load this helper or autoload it.  And call function from your view:
PHP Code:
<?php echo @view('my_view', array('param'=>'pam-pam')); ?>
Reply
#8

There are some template libraies available for ci. You can try them. Or roll out your own. Search google for how to create template library in ci. Even If you dont need template system just look at code. you will learn use full tricks
          Heart  love codeigniter Heart
          Learning  best  practices
     Rate my post if you found it helpfull
Reply
#9

(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.
Reply
#10

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->datatrue);

        
//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!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB