I wish to display a variable that is stored in a session at the top of each page throughout my website. At the minute, on every single page, in the controller index() I have;
$data['credits'] = $this->session->userdata('credits');
I havve created a seperate view for the navigation bar (where the variable will be displayed). I have called it vNav.php. In vNav.php I then do echo $credits.
For every new view, I have to include the vNav.php, but that also means in the other view's controllers, I have to set the $data['credits'] variable in the index() function.
Is there a way in CI to do this automatically for me? So I don't have to have the same line of code in all my controllers?
Thanks
Okay here's a better design for your views.
First, create a folder called include/, in this folder create a header.php, footer.php, template.php.
header.php
<html>
<head>JS - CSS - META</head>
<body>
<div>COMMON MESSAGE</div>
footer.php
<footer>FOOTER GOES HERE</footer>
</body>
</html>
template.php
$this->load->view('include/header.php');
$this->load->view($main_page);
$this->load->view('include/footer.php');
any controller:
$data['main_page'] = "hello_world"; // view/hello_world.php
$this->load->view('include/template',$data);
So in that way, you can create a common section that will be displayed on all pages by adding it to the header or the footer. If you want, you can also create another file in the include folder and then include it in the template so that it will be loaded automatically every where by only modified one file
Related
I am creating a custom CMS for learning. I have the plan to have the following pages,
Login page
All Posts page
Edit Post page
index page
header.php (the website's header)
footer.php (the website's footer)
sidebar.php (the website's sidebar)
I am confused how would the index page link header, footer and sidebar. Please guide me how can I link these php files in index.php.
You can simply add an Array of files you want to include:
$array = ('header.php', 'footer.php', 'sidebar.php');
Then add some HTML Code structure...
and then you can access the Array and load files.
include_once($array[0]);
.. to include the header.php
include_once($array[1]);
.. to include the footer.php
....
you can use the require_once function to make your site not loading other content if the file does not exists.
if you want to add these files automaticly just add a loop.
foreach($array as $file){
if(file_exists($file)){
require_once($file);
}
else{
die($file.' does not exist!');
}
}
You can use either require_once or include.
I personally use require once option cause it only needs to be include once and not again on later stage.
Inside your body tags:
require_once('/path/to/header.php');
require_once('/path/to/breadcrumb.php');
require_once('/path/to/content.php');
require_once('/path/to/footer.php');
require_once('/path/to/copyright.php');
i have to header like.
header-first.php
header-second.php
i create two template like first and second. when the first template is load then i want to load header-first.php and this is working. then second like first.
i have problem with when i load second template then the header-second.php is load is working but not when i go to any page then i want to load header-second.php but not it load header.php.
when i load a second template then the header-second.php is loaded working. now i going any page and view post then the header.php file is load i want to load header-second.php file. please help how can i set globally.
i try to set globally in function.php like.
global $header;
if(is_page_template('page-second.php')){
header = 'second';
}else if(is_page_template('page-first.php')){
header = 'first';
}
it working it return only template page is load.
when i try to load any other like about page it return '' value. it not return any value.
i want to second whenever the first template is not visit.
thank you.
Each template file such as page.php, single.php, archive.php, etc, will have get_header() at the top of it.
This function loads the header (header.php).
If you pass in a string as the first argument it will try to load header-{argument}.php and if not found it will load header.php.
Anywhere you want to load header-second.php you need to change get_header(); to get_header( 'second' );
There is an action that fires when this function runs but there's no filter so you can't override it globally like you're attempting to do. Instead you need to update the template files individually with the function I showed you above.
To summarise change:
<?php get_header(); ?>
To:
<?php get_header( 'second' ); ?>
Further reading: http://codex.wordpress.org/Function_Reference/get_header
I was working on a site in which on right side I am displaying some fixed type of dynamic content like event calendar, login, register etc. right side bar name is right_view.php
So first I was doing like this that I was sending parameters in every function of controller's and then in my view I was accessing right side parameters by calling right view like this
<?php $this->load->view('right_view');?>
then after login I can get my username that is stored in session.
After that I thought it is not a good approach to send parameters in every functions I just make a controller named right.php and in this controller I am passing parameters to right_view.php and after that in my view I changed my code for callig righr_view like this
<?php include(base_url().'right');?>
It display right content as I do above but one changed happen that I cannot access any of session stored variable in right side bar.
Is session does not work after including controller in view?
You're basically wanting to do a template system but going about it the wrong way. And for the record no I don't think you can (or at least should) be loading controllers into views like that.
What you want is a template file something like this:
<?php
$this->load->view('templates/header', $title);
$this->load->view('templates/sidebar',$sidebar_content);
$this->load->view('pages/'.$main_content);
$this->load->view('templates/footer');
?>
Call that template.php
Then in your controller you'd do something like this:
public function welcome()
{
$data['main_content'] = 'welcome';
$data['title']='Welcome to REfficient.com!';
$data['sidebar_content'] = 'sidebar/no_sidebar';
$data['additionalHeadInfo'] ='';
$this->load->view('templates/template',$data);
}
So if you look at the template file the first line is loading the header and including the title variable to insert into the page (header, sidebar, maincontent and footer are all their own separate PHP pages) and so on.
Now what I did (since my layout was very similar to yours) is my main sidebar file has an if statement that says if logged in show x, if not show login form.
Note - the additionalHeadInfo variable is so I can have includes like jQueryUI or something on an individual page without loading it on pages that don't need it.
I am a newbie in Yii Framework and creating a CRM which is module based.
Using different tutorials I am able to create my own theme, but now I am stucked at one point.
In my theme, the upper <nav> and left <nav> remains the same throughout the app, until user is logged in. That's why I made it a part of my main.php, but in the login page there are no buttons to show, just simple login form with 2 textfields.
How can I implement this form in my application using custom themes?
I have tried to define a layout in that particular action but not succeeded. Any help would be appreciated.
Using a custom layout for your view is the right way to go.
You can either set the layout in the controller action or in the view.
$this->layout = "//layouts/mylayout";
Note that the default layouts column1.php and column2.php also use the main.php layout file.
Try this step by step :
Create New theme
You can create a new theme and add this to the directory
Application_Root/themes.
Look at the themes/classic directory to get an an idea of the structure of the directory.
The important file (at this stage) is :-
Application_Root/themes/views/layouts/main.php
Customise your theme contents
Copy the css, image, js files etc to the correct directory and change the main.php file to your liking. For example, if your main.php says
<link href="css/mystyle.css" rel="stylesheet">
Then you will have a file
Application_Root/css/mystyle.css
Create the content placeholder.
Somewhere in your main.php, there will be a placeholder for dynamic text, which is specified by.
<?php echo $content; ?>
Tell yii to use the theme.
Change the file Application_Root/protected/config/main.php by adding the following line just before the last line (containing the closing bracket).
'theme'=>'surveyhub'
Create the layout placeholders.
Create an HTML segment that will be written into the $contents portion of main.php. Call it for example one_column.php. The file path will therefore be Application_Root/themes/views/layouts/one_column.php In that file, where you want the dynamic text to be placed, create a placeholder.
<?php echo $content; ?>
Tell Yii to use the layout.
In the file Application_Root/protected/components/Controller.php, add or modify the layout variable to read :
public $layout='//layouts/one_column.php';
Refresh the page
i have a little problem that i dont know what to do with.
i have a template with
header
content
sidebar
footer
on all my page the only thing that need to change is the content, so how can i pass data to my sidebar/footer
do i need to make a controller? or do i need to make a library and load it in my template.php file?
i use this template system
http://williamsconcepts.com/ci/codeigniter/libraries/template/index.html
I am not sure about your specific Template Library, but I do know that generally this is done by nesting views inside other views, and as long as the data is loaded into the initial view, it propagates to nested views as well.
Example without Template library
Controller function
function index() {
$data['some_var'] = "some value";
$data['another_var'] = "another value";
$this->load->view('first_view',$data);
}
first_view
<? $this->load->view('header') ?>
<h1>Content</h1>
<? $this->load->view('sidebar') ?>
<? $this->load->view('footer') ?>
In this instance, the $data that is loaded into first_view propagates to header,sidebar,and footer.
So you can use $some_var or $another_var in any of these views.
UPDATE
Another way you can load data in to your views globally is with this function
$this->load-vars($data);
Where $data is your view data, this statement just before you load your template should allow all of this data to be accessed in any view loaded by the template. While this is a shotgun approach, it is the suggested way to do this by your chosen template library.
If you will only ever change content, then there is no need to set up regions for your header, sidebar or footer - just add their contents to your main template file.
However if you will infrequently need to change the content of these regions, I would create "default" views for these regions, and load in each controller constructor, like thus:
$this->template->write_view('header', 'default/header');
$this->template->write_view('sidebar', 'default/sidebar');
$this->template->write_view('footer', 'default/footer');
You can then either extend these default region views or overwrite them on a per method basis (refer to the documentation of your library to find out how).