how to load view into another view codeigniter 2.1? - codeigniter

Ive been working with CI and I saw on the website of CI you can load a view as a variable part of the data you send to the "main" view, so, according the site (that says a lot of things, and many are not like they say ...ej pagination and others) i did something like this
$data['menu'] = $this->load->view('menu');
$this->load->view ('home',data);
the result of this is that I get an echo of the menu in the top of the site (before starts my body and all) and where should be its nothing, like if were printed before everything... I have no idea honestly of this problem, did anybody had the same problem before?

Two ways of doing this:
Load it in advance (like you're doing) and pass to the other view
<?php
// the "TRUE" argument tells it to return the content, rather than display it immediately
$data['menu'] = $this->load->view('menu', NULL, TRUE);
$this->load->view ('home', $data);
Load a view "from within" a view:
<?php
// put this in the controller
$this->load->view('home');
// put this in /application/views/home.php
$this->view('menu');
echo 'Other home content';

Create a helper function
function loadView($view,$data = null){
$CI = get_instance();
return $CI->load->view($view,$data);
}
Load the helper in the controller, then use the function in your view to load another one.
<?php
...
echo loadView('secondView',$data); // $data array
...
?>

Related

How do i call a view from within a view in codeigniter?

hi this is my controller :
public function index()
{
$data['menu'] = $this->load->view('frontend/menu/main_menu_view');
$this->load->view('frontend/header/header_view',$data);
$this->load->view('frontend/home/index_view');
}
and this is the code from the index_view view :
echo $menu;
but is not working, what should I do ? thx
The third parameter of $this->load->view() doesn't load the view but returns it as 'data', to do with it what you please, muwahhahah! Sorry.
$data['menu'] = $this->load->view('frontend/menu/main_menu_view' , '', true);
You can still pass $data into that view.
So from inside of
$this->load->view('frontend/header/header_view',$data);
you will echo $menu
Scroll to the bottom of the docs
You can call
$this->load->view('frontend/menu/main_menu_view');
from inside any view file and it will load and display for you.
In the view, you have to write just this :
<?php
$this->view('frontend/menu/main_menu_view');
?>
Don't know if you copied this from your code
or made a typo while typing your question, here, but shouldn't it be
echo $menu;
instead of
echo menu;
?
edit: Also, haven't worked with Codeigniter in a while,
but why pass $data to the header_view and call it from the index_view instead ?

CodeIngiter: Load a view inside of another view

I'm using CodeIgniter. I want to load views inside of other views. How can I do this?
Example:
Let's say I have a "view" called "CommentWall". In CommentWall, I want a bunch of "Comment" views. I use the view for "comment" all over my site!
How can I do this? It seems CodeIgniter only allows me to load views sequentially, which is sort of strange considering I use reusable views INSIDE of other views!
Can I do a $this->load->view('comment'); inside of my view for CommentWall? Or is there some other way to have reusuable views contained inside a view?
You can do it easily, just load the main view, for example CommentWall from the controller
$this->load->view('CommentWall');
To add child views in CommentWall view you can add following line inside your CommentWall view
$this->view('Comment');
For example, if you load CommentWall view from your controller like this
$data['comments'][] = 'Comment one';
$data['comments'][] = 'Comment two';
// load the parrent view
$this->load->view('CommentWall', $data);
Now in the CommentWall (parent view), if you put this
foreach ($comments as $comment) {
$this->view('Comment', array('comment' => $comment));
}
And in your Comment (child view) if you have this
echo $comment . '<br />';
Then you should get output something like this
Comment one
Comment two
Update : Alos, check this answer.
Try
class Main extends CI_Controller {
function __construct()
{
parent::__construct();
$data->comments =$this->load->view('comment');
$this->load->vars($data);
}
And in every view try
echo $comments;
Just load the "Comment" vies as string in controller and pass it to "CommentWall" view.
You can do it like this:
//Controller:
public function load_comment_wall($param) {
$comments_view = ""; //String that holds comment views
//here load the comments for this wall as follows:
//assuming $comment_ids is array of id's of comment to be put in this wall...
foreach($comment_ids as $comment_id) {
$temp = $this->load->view("comment",array('comment_id'=>$comment_id),TRUE); //Setting last parameter to TRUE will returns the view as String
$comments_view = $comment_views.$temp;
}
$data['comments'] = $comments_view;
//load comment wall
$this->load->view('comment_wall',$data);
}
//In Comment wall View, add the following line
echo $comments;

JToolbar::save() redirection

I'm going through the Joomla 2.5 tutorial to build a custom component. Now I'm facing an issue on the redirection after using JToolbar::save() or JToolBarHelper::cancel for that matter. By default Joomla wants to redirect to the default layout (from the edit layout). However I don't want it to do that. I want it to redirect back to another view. In Joomla 1.5 I would have done this through adding the function into the controller - something like
function cancel()
{
//redirects user back to blog homepage with Cancellation Message
$msg = JText::_( 'COM_BLOG_POST_CANCELLED' );
$this->setRedirect( 'index.php?option=com_jjblog&view=jjblog', $msg );
}
Now that works beautifully for the cancel function, however for save this is a much more complex thing. If I want to overwrite the url do I have to redirect the controller to the model and then write in all the code for the model interaction? Because that seems slightly excessive just for a url redirection like you would in Joomla 1.5?
Hope you have added the save toolbar code with the proper controller name like this
JToolBarHelper::save('controllerName.save');
Create a save function in appropriate controller.
Add the task in the form
Finnally make sure you have added form action withthe corresponding component name.
You can try this-
In the controller firstly you call the parent save function than redirect to url.
function save(){
parent::save();
$this->setredirect('index.php?option=com_mycomponent');
}
OK it didn't need to $this->setRedirect at all. Just needed me to change the value to
protected $view_list = 'jjBlog';
which then sets the redirects of everything back to that list view.
Source link for this is here.
Thanks for all the responses though!!
view.html.php
protected function addToolbar ()
{
JRequest::setVar ('hidemainmenu', false);
JToolBarHelper::title (JText::_ ('Configuration'), 'configuration.gif');
JToolBarHelper::save($task = 'save', $alt = 'JTOOLBAR_SAVE');
}
controller.php
public function save()
{
$mainframe = JFactory::getApplication();
$mainframe->enqueueMessage (JText::_ ('COM_SOCIALLOGIN_SETTING_SAVED'));
$this->setRedirect (JRoute::_ ('index.php', false));
}
I think you can use
global $mainframe;
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
If you are overriding joomla's default save function in your custom component like
function save( $task = 'CustomSave', $alt = 'Save' ) // or even same name Save
Inside your controller you can use the CustomSave as the task and use $mainframe for redirect.
or
$mainframe = &JFactory::getApplication();
$mainframe->redirect("index.php?option=com_user&task=activate&activation=".$activation);
Hope this may help you..

codeigniter : using flash data but no new page load?

Usually, I just set a $feedback var or array and then check for that to display in my views.
However, it occurred to me I should perhaps use flashdata instead.
The problem is sometimes - for say an edit record form, I may simply want to reload the form and display feedback - not redirect. when i use flashdata, it shows but then it shows on the next request as well.
What would be the best practice to use here?
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
u use hidden field for that
I would use the validation errors from the Form validation class and load those directly to the view in its 2nd argument.
$this->form_validation->set_error_delimiters('<p>', '</p>');
$content_data = array();
if (!$this->form_validation->run()) {
$content_data['errors'] = validation_errors();
}
$this->load->view('output_page', $content_data);
Then check in your view whether $errors isset.
Controller:
$data['message'] = 'some message you want to see on the form';
$this->load->view('yourView', $data);
View:
if (isset ($message)) : echo $message; endif;
...

CodeIgniter Controller not Loading view with Parameters

Real basic CI question here, which I cant find anything on in the documentation. I think I may need some further configuration?? I have a function which loads a view and it works correctly, but when I send it parameters its doesn't load the view, any ideas??
Heres code with params (view does not load)
function grid($height,$width)
{
echo $height."x".$width;
$this->load->view("grid");
}
and here's without (view does load)
function grid()
{
//echo $height."x".$width;
$this->load->view("grid");
}
So Height and width is the only thing that echos in the first example, in the second the view is loaded.
Thanks ahead of time!
You are supposed to have your controller pass parameters to the view as an array:
function grid($height,$width)
{
$data = array();
$data['height'] = $height;
$data['width'] = $width;
$this->load->view("grid", $data);
}
Then your view can render them:
echo $height."x".$width;
This allows for a clean separation of concerns between the Controller and View objects.
For more information see the section Adding Dynamic Data to the View in the CI User Guide.

Resources