Dynamic views with Codeigniter or templates or partial views - php

How can I load views of my nav menu without reloading the entire main view? something like templates or partial views. Is necesary to do this with ajax? How can it be? I donĀ“t know much about ajax. It will be possible to see an example?
I load my home view in controller like this:
$data = [
'header_view' => 'main/header',
'nav_view' => 'main/nav',
'section_view' => 'main/main_view',
'article_view' => 'main/article',
'aside_view' => 'main/aside',
'footer_view' => 'main/footer',
];
$this->load->view('home_view', $data);
Then I load the vievs from home view:
<section>
<?=$this->load->view($section_view);?>
</section>
...
And in the nav view I wanted to put the links to different sections without reload the nav view, because I will have a tree structure and other stuff.

why don't you use AngularJS ? you can make a single page application using ui-routes or ng-routes. you can make ajax call to your controller method that will render your view, and you can show that view in .

EDIT
In order to do a one page iframe site, you need to set up a controller to return a page without anything else
Controller -> loadpage($name)
then route it however you'd like
then, create an iframe with an id
<iframe name="maincontent"></iframe>
obviously, add in your needed styling / attributes.
On your links, add in targets
Link 1

Related

Is it possible to add custom preview images in og tags for different pages of a website?

I am trying to add og tag on each view page of my website. But it is only rendering the og tag of the layout page(index page).
Is it possible to add different title/images in views for every page?
Thanks in advance
Yes, it's possible. From inside view you can this for example:
$this->registerMetaTag([
'property' => 'og:image',
'content' => 'path/to/file'
]);
where $this is View object you've got access to from view.
Meta property will be added automatically in your layout if you've got there <?php $this->head() ?>

Laravel 4 Won't update views content

For some reason, Laravel 4 won't update the content view that should be displayed in the browser.
For example, if I add one more item to the navigation menu, it is written on the view file, but not shown on the navigation list.
I have checked for any mispells, errors or anything related to this issue however I can't figure out if it's my mistake or it has to do with the cache.
What I've done:
Created the route for the url.
The related controller that will render the view.
Created the actual view with the content.
Added the item to the navigation file.
And I am sure that the view:
Extends with my layout
Included my content inside the #section and #stop'ed it.
And there are no errors displayed in the browser.
Also I've checked the storage folder which contains the view's cache, and I verified that the navigation file and the view file are phased correctly.
Looks like I need your advice guys.
Code as requested:
The route
Route::get('/account/profile', array(
'as' => 'account-profile',
'uses' => 'SettingsController#getProfileSettings'
));
Navigation
<li>Edit profile</li>
Controller
public function getProfileSettings() {
return View::make('account.profile');
}
View
#extends('layout.main')
#section('content')
Edit your profile...
Not yet...
#stop
First things I would do is:
Clear the view's cache folder.
check how you display the navigation, with #yield or #section, which should end with #show in the layout file.
If that didn't work maybe you could provide us with some code from the layout.

Implementing Pjax on a common header footer coding structure

I am developing a project using codeigniter that has common header and footer. By using pjax I am able to dynamically change the content alone without disturbing the header and the footer. Also the url changes with respect to the controller. Below is my concern over the url and SEO analogy.
My default home page controller loads the header, index page and the footer as shown below.
public function index(){
$this->load->model('dbmodel');
$data['about'] = $this->dbmodel->about();
$this->load->view('templates/header',$data);
$this->load->view('includes/index',$data);
$this->load->view('templates/footer');
}
Suppose I click on a menu item, it loads the corresonding controller path in the url (say - http://domain.com/main/bandDirectory) and the pjax content div alone is replaced/updated with the content while header and footer remaining the same.
public function bandDirectory(){
$this->load->model('dbmodel');
$data['content'] = $this->dbmodel->band();
$this->load->view('includes/bandDirectory',$data);
}
This works fine when the menu items are navigated from the home page as it loads the header and footer initially. But what if we directly hit the url (say http://domain.com/main/bandDirectory). This controller does not contain header and footer and it loads only the content which breaks the page apart! This would become a serious issue if search engines indexes these urls. How to overcome this issue?
P.S : Since I am implementing a player in the header, I do not want to include header and footer in all the controllers as this would stop the player from playing when header refreshes.
What I did in our project was that to look for pjax header in the request, if the pjax header is present then load the content template only else load the full template, this is my corresponding code in perl, hope it helps
sub tour {
my $self = shift;
return $self->render(
template => 'static/tour',
layout => $self->req->headers->header('X-PJAX') ? 'content_header' : 'full_width',
);}
you can implement the same in php

Composite views with Codeigniter?

For my Codeigniter site, I started by making a view for each controller situation. This was impractical, as it would require going back to the code for each to make a change. So I changed approach and operated on a 'default' controller with optional fields. I then thought I could load special views as needed into it.
I put together this view with optional fields with fields for $title, $search_bar on/off etc. However, now came the content area. I was able to load more views into this default view using:
$data['content_views'][]='blocks/login';
$this->load->view('default/a', $data);
and in the 'default'view:
if(isset($content_views)&& (is_array($content_views)))
{
foreach($content_views as $content_view)
{
$this->load->view(&$content_view);
}
}
(and that works fine)
Two questions:
Am I making things to complex? Is this an accepted way of doing this? Or have I misunderstood the functioning of a view and how they are intended to work?
I want a way to mix the $content_view, i.e. a block of text, then a view. I'm not quite sure as to how to proceed. Say I want a message first, then a view, then more text. This method will only accept views.
Can anybody help me create this flexible approach?
Yeah I would say you're making things a little complex. While I may not be following your description well enough to know precisely how to respond, I can tell you how I do it:
First the whole site is run through a template so the header and footer are the container file and all views needed within the site are rendered as page type views - like an article page, a gallery page, etc. Components are loaded and passed to the views as strings:
$content['sidebar'] = $this->load->view('components/sidebar', $data, true);
That last true says to render as string.
Essentially, this means the page views are pretty much html with php echoing out the necessary elements. No views calling other views, which is how I read your example.
CI loads views progressively, so your controller can output like so:
$this->load->view('header', $header_data);
$view_data['sidebar'] = $this->load->view('components/sidebar', $sidebar_data, true);
$this->load->view('content', $view_data);
$this->load->view('footer', $footer_data);
and in content view, handle the sidebar like so:
<?php if(isset($sidebar)): ?>
<nav>
<?php echo $sidebar; ?>
</nav>
<?php endif; ?>
And, assuming you populate those arrays for each view it will render header, then content, then footer. And also render sidebar if it is present.
So combining everything, I'm basically saying you can load in sections in your controllers progressively, passing sub-views as strings to whichever section makes sense. That keeps your view controlling in the controller where it belongs and not in the view files themselves. In my experience, I have not had to write a site that was so complex that this construct wasn't perfectly suitable if the site is planned well.

Detecting the order of stacked views with CodeIgniter

I am creating multiple CodeIgniter views that could possibly be placed in any order by a controller. One of these views is a table of contents that I want to display the correct order of the views as they appear in the page. Is it possible to detect which order the views appear in and then change, add or remove elements from the table of contents?
CodeIgniter won't be able to help you with it cause it renders the view instantly when you call $this->load->view(...) (see the relevant code for view loading in the core Loader class).
You'll have to do your own logic for that, there's nothing stopping you from creating an array with information about the views you need to load, and load your table of contents view beforehand:
$views = array('page1.php' => 'Page 1', 'page2.php' => 'Page 2');
$this->load->view('table_of_contents.php', $views);
foreach($views as $view => $title){
$this->load->view($view);
}

Categories