CodeIgniter: Displaying Dynamic Titles issues - php

Can someone help me out with this piece of code.
I have a template set up like this:
system/core/Loader.php :
public function template($view, $vars = array(), $return = FALSE)
{
$template = $this->view('includes/header', array(), $return);
$template = $this->view('includes/navigation', array(), $return);
$template .= $this->view($view, $vars, $return);
$template .= $this->view('includes/footer', array(), $return);
if ($return)
{
return $template;
}
}
It works perfect but when I try this to display a dynamic title, it doesn't want to display the title. I guess because the data is passed to content_home rather then to includes/header:
my controller:
public function home() {
$this->load->model("model_get");
$data["page_title"] = "Home Page";
$data["results"] = ($this->model_get->getData("home"));
$this->load->template("content_home",$data);
}
includes/header.php
<title><?php echo (isset($page_title)) ? $page_title : 'Default title text'; ?> </title>
Any ideas how to work it out ??
Thanks : )

I've implemented dynamic titles in an even simpler way:
Controller:
$data['header_data'] = array('title'=>'Foo bar');
$data['page'] = "login";
$this->load->view('template', $data);
View - template.php
$this->load->view('includes/header', $header_data);
$this->load->view('pages/'.$page);
$this->load->view('footer');
We have two folders in views - includes and pages.
includes folder: header.php, footer.php (etc..)
pages folder: login.php, profile.php (etc..)
views/includes/header.php
<!DOCTYPE html>
<html>
<head>
<title><?=$title?></title>
</head>
...
This way, you can use the same template to load all your pages. You may send data other than the title too!

In your views folder make a template file like this,
we'll call it template_web.php
<?php
// the preset view files for your template
$this->load->view('includes/header');
$this->load->view('includes/navigation');
// in this example we will have our content view files in a folder called 'web'
$templatefolder = 'web/';
// this is where you can pass in 'content' view files that are unique for the page
// this example has 3 placeholders you can put as many as you need
if(isset($content01))
$this->load->view($templatefolder.$content01);
if(isset($content02))
$this->load->view($templatefolder.$content02);
if(isset($content03))
$this->load->view($templatefolder.$content03);
// preset template footer file
$this->load->view('includes/footer');
ok now in your controller methods
// data you want to pass to your header
$data['page_title'] = 'Awesome Home Page';
// other data
$data['bigbagofdata'] = $this->bigBagOf->awesomeData ;
// the name of the view files
$data['content01'] = 'homepage';
$data['content02'] = 'homepage_banner';
// your template
$this->load->view( 'template_web', $data );
data can be passed to any of the view files. and its very easy to set up different templates this way.

Try
$template = $this->view('includes/header', $view, $return);
instead of
$template = $this->view('includes/header', array(), $return);

If you want to access your $data["page_title"] variable in your header view, then you should pass it into your header view:
change this line:
$template = $this->view('includes/header', array(), $return);
to
$template = $this->view('includes/header', $vars, $return);

I think MrMarchello finds oout the real issue. the issue is not passing the $data variable to header.
$data['title'] = "Edit User";
$this->load->view('template/header', $data);
$this->load->view('user/edit_member', $data);
$this->load->view('template/footer');
Hope this help you a little bit.

In your config file add this parameter:
$config['pageTitle'] = 'Book Store';
In template page :
<title><?=$this->config->config["pageTitle"]?></title>
In any page you like change this config in action:
$this->config->config["pageTitle"] = $detail[0]['Title'];
Every time you can change the title easily.

Related

Codigniter: I don't want to load the header and footer part of my template on the login page

This is my login controller
in my login page i haven't need to loaded header and footer part
public function login()
{
$head = array();
$data = array();
$head['title'] = lang('user_login');
$head['description'] = lang('user_login');
$head['keywords'] = str_replace(" ", ",", $head['title']);
$this->load->library('user_agent');
$referrer = $this->agent->referrer();
$this->session->set_userdata('login_redirection_page',$referrer);
$this->render('login', $head, $data);
}
Can you try below steps.
Go to render function definition and verify how they are loading the header and footer files, if code exists to skip header and footer files(by passing some parameters as false or etc), proceed accordingly.
If code doesn't exists to do so write logic to skip header and footer files by passing some parameters.
Otherwise comment $this->render('login', $head, $data); code and add below code.
$this->load->view('login', $data);

View Not Rendered Codeigniter HMVC

I am trying to make a CRUD Module in Codeigniter HMVC but I seem to be missing something in the process. Here is what I am facing.
I have a News Module which has a Manage function
function manage(){
$grid = Modules::run('Crud/renderGrid', 'News' , 'News management');
}
Render Grid Function
function renderGrid($module , $page_title){
$data['page_title'] = $page_title; //Dynamic
$data['module'] = $module;
$data['view_module'] = 'Crud';
$data['displayfields'] = Modules::run($module.'/get_displayfields');
$data['key'] = Modules::run($module.'/get_key');
$data['rows'] = Modules::run($module.'/get' , $data['key']);
$data['view_file'] = 'manage';
$this->load->module('dashboard');
$this->dashboard->show_dashboard($data);
}
Here, the show_dashboard function just loads up a template layout with a desired view in it.
function show_dashboard($data = NULL){
if($data == NULL){
$data['view_file'] = "manage";
$data['page_title'] = 'Sigma Web Solutions';
}
$this->load->module('templates');
$this->templates->admin($data);
}
Templates->admin
function admin($data){
$this->load->view('admin' , $data);
}
The View (omitting the header n Footer)
<?php
if (!isset($view_file)) {
$view_file = "";
}
if (!isset($view_module)) {
$module = $this->uri->segment(1);
}
if (($view_module!="") && ($view_file!="")) {
$path = $view_module."/".$view_file;
$this->load->view($path);
}
?>
Now, when I try the url news/manage, it gives me a blank page with no source code in it. But when I try something like
crud/renderGrid/news/sometitle/ it works just fine.
Kindly point out what did I miss here. Thanks.
Working Solution:
Thanks to wolf I added a route
$route['managenews']= 'crud/renderGrid/news/News';
And it works like charm. But why do I need a route here? Shouldn't it just work. And this means for every module I need to have 4 entries in my route file for the CRUD system to work. can anyone suggest a better method?

Avoid mentioning to load the header and footer all the time [duplicate]

I really don't enjoy writing in every controller:
$this->load->view('templates/header');
$this->load->view('body');
$this->load->view('templates/footer');
Is it possible to do, that header and footer would be included automatically and if we need to change it, we could also do that? How do you deal with that? Or it's not a problem in your opinion? Thanks.
Here's what I do:
<?php
/**
* /application/core/MY_Loader.php
*
*/
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
For CI 3.x:
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
if($return):
$content = $this->view('templates/header', $vars, $return);
$content .= $this->view($template_name, $vars, $return);
$content .= $this->view('templates/footer', $vars, $return);
return $content;
else:
$this->view('templates/header', $vars);
$this->view($template_name, $vars);
$this->view('templates/footer', $vars);
endif;
}
}
Then, in your controller, this is all you have to do:
<?php
$this->load->template('body');
Yes.
Create a file called template.php in your views folder.
The contents of template.php:
$this->load->view('templates/header');
$this->load->view($v);
$this->load->view('templates/footer');
Then from your controller you can do something like:
$d['v'] = 'body';
$this->load->view('template', $d);
This is actually a very simplistic version of how I personally load all of my views. If you take this idea to the extreme, you can make some interesting modular layouts:
Consider if you create a view called init.php that contains the single line:
$this->load->view('html');
Now create the view html.php with contents:
<!DOCTYPE html>
<html lang="en">
<? $this->load->view('head'); ?>
<? $this->load->view('body'); ?>
</html>
Now create a view head.php with contents:
<head>
<title><?= $title;?></title>
<base href="<?= site_url();?>">
<link rel="shortcut icon" href='favicon.ico'>
<script type='text/javascript'>//Put global scripts here...</script>
<!-- ETC ETC... DO A BUNCH OF OTHER <HEAD> STUFF... -->
</head>
And a body.php view with contents:
<body>
<div id="mainWrap">
<? $this->load->view('header'); ?>
<? //FINALLY LOAD THE VIEW!!! ?>
<? $this->load->view($v); ?>
<? $this->load->view('footer'); ?>
</div>
</body>
And create header.php and footer.php views as appropriate.
Now when you call the init from the controller all the heavy lifting is done and your views will be wrapped inside <html> and <body> tags, your headers and footers will be loaded in.
$d['v'] = 'fooview'
$this->load->view('init', $d);
Try following
Folder structure
-application
--controller
---dashboards.php
--views
---layouts
----application.php
---dashboards
----index.php
Controller
class Dashboards extends CI_Controller
{
public function __construct()
{
parent::__construct();
$data = array();
$data['js'] = 'dashboards.js'
$data['css'] = 'dashbaord.css'
}
public function index()
{
$data = array();
$data['yield'] = 'dashboards/index';
$this->load->view('layouts/application', $data);
}
}
View
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Some Title</title>
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/app.css" />
<link rel="stylesheet" href="<?php echo base_url(); ?>assets/css/<?php echo $css; ?>" />
</head>
<body>
<header></header>
<section id="container" role="main">
<?php $this->load->view($yield); ?>
</section>
<footer></footer>
<script src="<php echo base_url(); ?>assets/js/app.js"></script>
<script src="<php echo base_url(); ?>assets/js/<?php echo $js; ?>"></script>
</body>
</html>
When you need to load different js, css or whatever in the header or footer use the __construct function to $this->load->vars
Kind of a rails like approach here
Or more complex, but makes life easy is to use more constants in boot.
So subclasses can be defined freely, and a single method to show view.
Also selected constants can be passed to javascript in the header.
<?php
/*
* extends codeigniter main controller
*/
class CH_Controller extends CI_Controller {
protected $viewdata;
public function __construct() {
parent::__construct();
//hard code / override and transfer only required constants (for security) server constants
//such as domain name to client - this is for code porting and no passwords or database details
//should be used - ajax is for this
$this->viewdata = array(
"constants_js" => array(
"TOP_DOMAIN"=>TOP_DOMAIN,
"C_UROOT" => C_UROOT,
"UROOT" => UROOT,
"DOMAIN"=> DOMAIN
)
);
}
public function show($viewloc) {
$this->load->view('templates/header', $this->viewdata);
$this->load->view($viewloc, $this->viewdata);
$this->load->view('templates/footer', $this->viewdata);
}
//loads custom class objects if not already loaded
public function loadplugin($newclass) {
if (!class_exists("PL_" . $newclass)) {
require(CI_PLUGIN . "PL_" . $newclass . ".php");
}
}
then simply:
$this->show("<path>/views/viewname/whatever_V.php");
will load header, view and footer.
I tried almost all the answers proposed on this page and many other stuff. The best option I finally keeped on all my websites is the following architecture:
A single view
I display only one view in the browser. Here is my main view (/views/page.php):
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?>
<!DOCTYPE html>
<html lang="en">
<head>
<?= $header ?? '' ?>
</head>
<body>
<div style="width:1200px">
<?= $content ?? '' ?>
</div>
</body>
</html>
Controllers deal with multiple views
Of course, I had several views but they are concatenated to build the $header and the $content variables. Here is my controller:
$data['header'] = $this->load->view('templates/google-analytics', '', TRUE)
.$this->load->view('templates/javascript', '', TRUE)
.$this->load->view('templates/css', '', TRUE);
$data['content'] = $this->load->view('templates/navbar', '', TRUE)
.$this->load->view('templates/alert', $myData, TRUE)
.$this->load->view('home/index', $myData, TRUE)
.$this->load->view('home/footer', '', TRUE)
.$this->load->view('templates/modal-login', '', TRUE);
$this->load->view('templates/page', $data);
Look how beautiful and clear is the source code.
You no longer have HTML markup opened in one view and closed in another.
Each view is now dedicated to one and only one stuff.
Look how views are concatenated: method chaining pattern, or should we say: concatanated chaining pattern!
You can add optional parts (for example a third $javascript variable at the end of the body)
I frequently extend CI_Controller to overload $this->load->view with extra parameters dedicated to my application to keep my controllers clean.
If you are always loading the same views on several pages (this is finally the answer to the question), two options depending on your needs:
load views in views
extend CI_Controller or CI_Loader
I'm so proud of this architecture...
A simple rewrite of #Landons MY_Loader, to include multiple files for the body, e.i. page unique sidebars...
<?php
class MY_Loader extends CI_Loader {
public function template($template_name, $vars = array(), $return = FALSE)
{
$content = $this->view('frontend/templates/header', $vars, $return);
if(is_array($template_name)) { //return all values in contents
foreach($template_name as $file_to_load) {
$content .= $this->view('frontend/'.$file_to_load, $vars, $return);
}
}
else {
$content .= $this->view('frontend/'.$template_name, $vars, $return);
}
$content .= $this->view('frontend/templates/footer', $vars, $return);
if ($return)
{
return $content;
}
}
}
This works both ways...
Including one file to template:
$data['moo'] = 'my data'];
$this->load->template('home', $data);
Include multiple files to template:
$data['catalog'] = 'catalog load 1';
$data['sidebar'] = 'sidebar load 2';
$load = array('catalog/catalog', 'catalog/sidebar');
$this->load->template($load, $data);
CodeIgniter-Assets is easy to configure repository to have custom header and footer with CodeIgniter I hope this will solve your problem.
Redefine the CI_Loader::view function by adding a file named as 'MY_Loader.php' in your application/core folder and adding the following content
/**
* /application/core/MY_Loader.php
*/
class MY_Loader extends CI_Loader
{
public function view($view, $vars = array(), $return = FALSE, $include_template=TRUE)
{
$header='';
$footer='';
if($include_template)
{
$header=parent::view('templates/header',$vars,$return);
}
$content=parent::view($view, $vars,$return);
if($include_template)
{
$footer=parent::view('templates/footer',$vars,$return);
}
if($return)
return "$header$content$footer";
return $this;
}
}
You can use your config.php file, and also use the power of helpers in CodeIgniter.
$config['header_css'] = array('style.css','prettyPhoto.css','nivo-slider.css');
$config['header_js'] = array('core.js','core.js',
'jquery-1.4.1.min.js',
'jquery-slidedeck.pack.lite.js',
'jquery-prettyPhoto.js',
'jquery.nivo.slider.js');
Source: https://jamshidhashimi.com/dynamically-add-javascript-and-css-files-in-codeigniter-header-page/
Here is how I handle mine. I create a file called template.php in my views folder. This file contains all of my my main site layout. Then from this template file I call my additional views. Here is an example:
<!doctype html>
<html lang="en">
<head>
<meta charset=utf-8">
<title><?php echo $title; ?></title>
<link href="<?php echo base_url() ;?>assets/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="<?php echo base_url() ;?>assets/css/main.css" rel="stylesheet" type="text/css" />
<noscript>
Javascript is not enabled! Please turn on Javascript to use this site.
</noscript>
<script type="text/javascript">
//<![CDATA[
base_url = '<?php echo base_url();?>';
//]]>
</script>
</head>
<body>
<div id="wrapper">
<div id="container">
<div id="top">
<?php $this->load->view('top');?>
</div>
<div id="main">
<?php $this->load->view($main);?>
</div>
<div id="footer">
<?php $this->load->view('bottom');?>
</div>
</div><!-- end container -->
</div><!-- end wrapper -->
<script type="text/javascript" src="<?php echo base_url();?>assets/js/jquery-1.8.2.min.js" ></script>
<script type="text/javascript" src="<?php echo base_url();?>assets/js/bootstrap.min.js"></script>
</body>
</html>
From my controller, I will pass the name of the view to $data['main']. So I will do something like this then:
class Main extends CI_Controller {
public function index()
{
$data['main'] = 'main_view';
$data['title'] = 'Site Title';
$this->load->vars($data);
$this->load->view('template', $data);
}
}
I had this problem where I want a controller to end with a message such as 'Thanks for that form' and generic 'not found etc'.
I do this under views->message->message_v.php
<?php
$title = "Message";
$this->load->view('templates/message_header', array("title" => $title));
?>
<h1>Message</h1>
<?php echo $msg_text; ?>
<h2>Thanks</h2>
<?php $this->load->view('templates/message_footer'); ?>
which allows me to change message rendering site wide in that single file for any thing that calls
$this->load->view("message/message_v", $data);
This question has been answered properly, but I would like to add my approach, it's not that different than what the others have mentioned.
I use different layouts pages to call different headers/footers, some call this layout, some call it template etc.
Edit core/Loader.php and add your own function to load your layout, I called the function e.g.layout.
Create your own template page and make it call header/footer for you, I called it default.php and put in a new directory e.g. view/layout/default.php
Call your own view page from your controller as you would normally. But instead of calling $this-load->view use $this->load->layout, layout function will call the default.php and default.php will call your header and footer.
1)
In core/Loader.php under view() function I duplicated it and added mine
public function layout($view, $vars = array(), $return = FALSE)
{
$vars["display_page"] = $view;//will be called from the layout page
$layout = isset($vars["layout"]) ? $vars["layout"] : "default";
return $this->_ci_load(array('_ci_view' => "layouts/$layout", '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
2) Create layout folder and put default.php in it in view/layout/default.php
$this->load->view('parts/header');//or wherever your header is
$this->load->view($display_page);
$this->load->view('parts/footer');or wherever your footer is
3) From your controller, call your layout
$this->load->layout('projects');// will use 'view/layout/default.php' layout which in return will call header and footer as well.
To use another layout, include the new layout name in your $data array
$data["layout"] = "full_width";
$this->load->layout('projects', $data);// will use full_width.php layout
and of course you must have your new layout in the layout directory as in:
view/layout/full_width.php
Using This Helper For Dynamic Template Loading
// get Template
function get_template($template_name, $vars = array(), $return = FALSE) {
$CI = & get_instance();
$content = "";
$last = $CI - > uri - > total_segments();
if ($CI - > uri - > segment($last) != 'tab') {
$content = $CI - > load - > view('Header', $vars, $return);
$content. = $CI - > load - > view('Sidebar', $vars, $return);
}
$content. = $CI - > load - > view($template_name, $vars, $return);
if ($CI - > uri - > segment($last) != 'tab') {
$content. = $CI - > load - > view('Footer', $vars, $return);
}
if ($return) {
return $content;
}
}
i had reached for this and i hope to help all create my_controller in application/core
then put this code in it with change as your file's name
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
// this is page helper to load pages daunamically
class MY_Controller extends CI_Controller {
function loadPage($user,$data,$page='home'){
switch($user){
case 'user':
$this->load->view('Temp/head',$data);
$this->load->view('Temp/us_sidebar',$data);
$this->load->view('Users/'.$page,$data);
$this->load->view('Temp/footer',$data);
break;
case 'admin':
$this->load->view('Temp/head',$data);
$this->load->view('Temp/ad_sidebar',$data);
$this->load->view('Admin/'.$page,$data);
$this->load->view('Temp/footer',$data);
break;
case 'visitor';
$this->load->view('Temp/head',$data);
$this->load->view($page);
$this->load->view('Temp/footer',$data);
break;
default:
echo 'wrong argument';
die();
}//end switch
}//end function loadPage
}
in your controller
use this
class yourControllerName extends MY_Controller
note : about name of controller prefix you have to be sure about your prefix on config.php file
i hope that give help to any one

CodeIgniter Template Library with a 2D array

I am using the following template library. What I am trying to do is to load one or more upper views inside a template,as an array so that I can easily load them in the template with a for-each loop.
This is a simple example on how it can be used on a controller:
function index() {
$data['title'] = 'My page title';
$partials = array('content'=>'c_location'); //Load view about inside the template.
$this->template->load('main', $partials, $data);
}
On the View you have an html like:
<html>
....
<?=$content?>
...
</html?>
This is what I am trying to use:
Controller:
$partials = array('content'=>'c_location',
array(
'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
)
);
So for example I could pass for upper_content, a top header as "first_upper_content" and a slide for "second_upper_content" and then the remainder of the content for "content".
Html:
...
<?=$upper_content?>
<--if upper_content is a array,
I could display each content with a for loop-->
<?=$content?>
When I try everything I get:
Message: pathinfo() expects parameter 1 to be string, array given
Filename: core/Loader.php
Line Number: 759
How can I implement this? I am thinking of modifying the
// Load views into var array
Inside the Template.php & adding a foreach loop to the html;
This is what you want:
// Load views into var array
foreach(array_flat($view) as $key => $file)
Include the "array_flat" function call in Template.php, as shown above.
You will need to define this function. You can do this in any auto-loaded helper, or even include it the own Template class (in this case, you should call it as $this->array_flat in the code above). Here is it:
function array_flat($array)
{
$result = array();
foreach ($array as $key => $value)
{
if (is_array($value))
{
$result = array_merge($result, array_flat($value));
}
else
{
$result[$key] = $value;
}
}
return $result;
}
What you're trying to do is not possible with that library.
The error you received indicates the CI Loader class is being passed invalid data. So taking a look at the library's load function, your code will fail right here:
// Load views into var array
foreach($view as $key => $file)
{
$tpl[$key] = $this->CI->load->view($file, $vars, TRUE);
}
The library passes your nested array in $partials directly to the CI Loader. With your data, this line works out to:
$tpl[0] = $this->CI->load->view(array(
'first_upper_content'=>'1_u_location','second_upper_content'=>'2_u_location'
), vars, TRUE);
You can see in the CI user guide that isn't valid. It seems to me that your options are to either overhaul the library or change your approach.
Just make your main template view, and have something like this in it:
$this->load->view('header');
if (is_array($page) {
foreach($page as $key=>$val){
$this->load->view($key, $val); // $val being optional parameters
}
} else {
$this->load->view($page);
}
$this->load->view('footer');
In the controller, $data['page'] would contain view info, either its name (string) or an array of names to call in order. Of course, you'll have to have premade views with those names.
This was from the top of my head, but it should do what you wanted. This is done without template libraries, just plain CI.
To load partials, all you need to do is use:
$this->load->view('partial_location',$data);
inside your main view. So if you have a main view called home_page.php, and you want to load partials for headers and footers, you can just use:
$this->load->view('header');
// Main page layout
$this->load->view('footer');
If you want to use custom or different data inside your partials, just define them in your controller:
$data = array(
'content' => 'about',
'header_content' => 'Welcome to the site!',
'footer_content' => 'Made by me!'
);
And in your main view file:
$this->load->view('header',$header_content);
// would echo 'Welcome to the site!'
echo $content;
// would echo 'about'
$this->load->view('footer',$footer_content);
// would echo 'Made by me!'

Include contents from one view to another in CodeIgniter

I m new to CodeIgniter so i m facing some problem regarding adding contents from one view to another.
I have three views header, contents and footer and want to put these views in one main container with 960px width.
coz i m new so expecting some simple answer.
My code is this
public function index()
{
$this->load->view('header');
$this->load->view('content');
$this->load->view('footer');
}
Thanks
You need this.
$output = $this->load->view('header', 'your_data', true);
$output .= $this->load->view('content', 'your_other_data', true);
$output .= $this->load->view('footer', 'your_last_data', true);
$this->output->set_output($output);
For more Info
Just call the views inside the main view
public function index()
{
$data['content']['title'] = 'title';
$data['content']['body'] = 'body';
$this->load->view('layout',$data);
}
In views/layout.php
<div id="main_container">
<?php
$this->load->view('header',$content);
$this->load->view('content',$content);
$this->load->view('footer');
?>
</div>
Ex. in views/header.php
<title>.<?php echo $title;?></title>
in views/content.php
<div id="main_body"><?php echo $body;?></div>
function index()
{
$data['title'] ="Details";
$this->load->view("headerview",$data);
$data['batches'] =$this->detailsmodel->getname();
$this->load->view('content',$data);
$this->load->view("footerview");
}
// something similar to this ?
Didn't understand ur question properly brief what u want ? is this the answer u looking for

Categories