I am stuck with a very basic problem. Ook the problem is this that I want a master template in which I can call the header, body and footer. I am unable to send title and css in header and also how can I send multiple css files. I am doing something like this:
This is the code in controller
$data['title'] = 'Login To WePOS';
$data['css'] = base_url().'style/login-box.css';
$this->load->view('templates/default',$data);
This is the code in header
<head>
<title><?php echo $title ?> - CodeIgniter 2 Tutorial</title>
<link href=" <?php echo $css;?>" rel="stylesheet" type="text/css" />
</head>
This is the code in template name as default
<html>
<?php
$this->load->view('templates/header', $data);
?>
<body>
<?php
$this->load->view('login/index', $data);
?>
</body>
<?php
$this->load->view('templates/footer', $data);
?>
</html>
hi there is different method to use template in codeigniter .
1- you can use this procedure
In controller
$data['main_content'] = 'login_view';
$data['title'] = 'Login To WePOS';
$data['css'] = 'login-box.css';
$this->load->view('templates/default',$data);
In template.php View
$this->load->view('header_view');
$this->load->view($main_content);
$this->load->view('footer_view');
in your main content variable you can pass the view file
if you want to add multiple css or multiple js files you can use MY_MARK idea as
$data['cssFiles'] = array(
'login-box.css',
'other.css'
);
and in your header file
if(is_array($cssFiles)){
foreach($cssFiles as $cssFile) {
<link href="<?php echo base_url() . 'style/' . $css; ?>" rel="stylesheet" type="text/css" />
}
}
Hope it helps.
You don't have to pass $data again in your default template.
<html>
<?php $this->load->view('templates/header'); ?>
<body>
<?php $this->load->view('login/index'); ?>
</body>
<?php $this->load->view('templates/footer'); ?>
</html>
This should allow you to pick up the $title and $css variables in your header as you have got currently.
With regards to sending multiple css files, create an array of files, like:
$data['cssFiles'] = array(
'login-box.css',
'other.css'
);
And modify the code in your header to be:
foreach($cssFiles as $cssFile) {
<link href="<?php echo base_url() . 'style/' . $css; ?>" rel="stylesheet" type="text/css" />
}
Hope that helps...
Related
Alright so I am using index.php which is located inside my views folder as my main view. I load all other views inside it (as you can see from the code bellow).
Now when I make a route like about/(:any) it wont load any of my CSS or such..
Routes
$route['about'] = 'about'; // works fine
$route['about/(:any)'] = 'about/get_specific_about/$1'; //problematic one
Now this is my index.php
<!DOCTYPE HTML>
<html>
<head>
<link rel="stylesheet" type="text/css" href="public/css/partials/header.css" />
<link rel="stylesheet" type="text/css" href="public/css/partials/footer.css" />
<link rel="stylesheet" type="text/css" href="public/css/<?php echo $meta['css']; ?>.css" />
</head>
<body>
<?php $this->load->view("partials/header"); ?>
<?php $this->load->view($meta['page']); ?>
<?php $this->load->view("partials/footer"); ?>
</body>
</html>
Now whenever I use the route with (:any), the code will load the view but it wont be able to find for example footer.css or header.css.. If I used for example ../public/css/header.css then it will...
Code for the About controller
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class About extends CI_Controller {
private $data = [
'meta' => [
'page' => "pages/about",
'css' => "about",
'description' => "test"
],
];
public function index()
{
$this->load->helper('html');
$this->load->view('index', $this->data);
}
public function get_specific_about($user) {
$tmp_data = [];
$tmp_data['meta']['css'] = "profiles/".$user;
$tmp_data['meta']['page'] = "profiles/".$user;
$tmp_data['meta']['description'] = "test";
$tmp_data['meta']['extended'] = true;
$this->load->view('index', $tmp_data);
}
}
Use absolute URL links for href
<head>
<link rel="stylesheet" type="text/css"
href="<?php echo base_url('public/css/partials/header.css'); ?>>" />
<link rel="stylesheet" type="text/css"
href="<?php echo base_url('public/css/partials/footer.css'); ?>" />
<link rel="stylesheet" type="text/css"
href="<?php echo base_url("public/css/{$meta['css']}.css"); ?>" />
</head>
If you're not using .htaccess' to rewrite URLs withoutindex.phpthen substitutesite_urlforbase_url`.
I want to have dynamic header.php in all of my website pages, and there are many CSS files for loading. is there any way to load different CSS files for each page ? for example:
<?php if($title == "title") { ?>
<link href="mycss.css" >
<script src="test.js"></script>
<?php } ?>
You can create a function (or class) to echo your header and feed parameters as settings. Something like this may work:
function.get_header.php
// This function will render the header
function get_header($settings = false)
{
ob_start();
include("header.php");
$data = ob_get_contents();
ob_end_clean();
return $data;
}
function.get_page_css.php
// This function will act like a pseudo database return
// and will return a series of css links based on input
function get_page_css($var = false)
{
$css['title'][] = "/css/style1.css";
$css['other'][] = "/css/style2.css";
$css['title'][] = "/css/style3.css";
if(!empty($css[$var]))
return $css[$var];
}
header.php
<!DOCTYPE html>
<html>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title><?php echo (!empty($settings['title']))? $settings['title'] : "Untitled Page"; ?></title>
<head>
<?php if(!empty($settings['css']) && is_array($settings['css'])) {
foreach($settings['css'] as $link) {
?>
<link type="text/css" rel="stylesheet" href="<?php echo $link; ?>" />
<?php }
}
?>
</head>
index.php
<?php
// Include the header function
include("function.get_header.php");
// Include the css return function
include("function.get_page_css.php");
// Write the header to browser using the get_page_css() function
echo get_header(array("title"=>"This Great Page!","css"=>get_page_css('title')));
?>
<body>...etc.
In a simple way, you can used this method which is given below...
<?PHP
$page_name= $_SERVER['PHP_SELF'];
if($page_name=='about.php'){
echo '<link href="about_css.css" type="text/css">
<script src="about_test.js"></script>';
}
if($page_name=='contact.php'){
echo '<link href="contact_css.css" type="text/css">
<script src="contact_test.js"></script>';
}
?>
first check this url path
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
?>
or
<?php
$url = 'http://' . $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$url1 = "http://example.com/home";
$url2 = "http://example.com/about";
if (strpos($url1,'home') !== false) { ?>
<link href="mycss.css" >
<?php
} else if(strpos($url2,'about') !== false){ ?>
<link href="mycss2.css" >
<?php }else { ?>
// your defult css file
<?php } ?>
I hope you understood
Put this index.php file in your css folder
<?
$title = $_GET['title'];
header('Content-Type: text/css');
switch ( $title ) {
case 'title_1':
include('style_1.css');
case 'title_2':
include('style_2.css');
default:
include('default.css');
}
exit();
?>
Then whenever you want to call your css :
<?
echo '<link href="path/to/css/folder/?title='.$variable.'" rel="stylesheet">'
?>
The index.php in the css folder will be controller for css needs to be included
For simplicity, you can assign a page code for every page. Create the script with that name. So you can import it like this:
In the page header:
$pageCode = "INDEX";
in the footer:
<?php
$scriptFileName = "your/path/to/file/".$pageCode.".js";
if(file_exists($scriptFileName)){
echo "<script src='$scriptFileName'></script>";
}
apply the same for CSS if needed. But in header!
This worked for me just fine, I added it to the _header.php file which is included on on all my pages:
<?php
$page_name = $_SERVER['PHP_SELF'];
if($page_name =='/recepiepg/admin/index.php'){
echo '<link rel="stylesheet" href="../css/admin.css" type="text/css"/>';
}
?>
Good evening,
I started using Codeigniter as Framework for my newest projekt... and already got problems on the first page.
I want to link a CSS file to my site. It looks perfectly good in the Code. But just nothing happens.
<html>
<head>
<title>Tec.Net</title>
<?php
$this->load->helper('html');
echo link_tag($data['css']);
?>
</head>
<body>
<h1><?php echo $title ?></h1>
This is the code of my header.php
body {
background-image: url("application/views/images/Console Background.png");
color: white;
}
The standard.css
<html>
<head>
<title>Tec.Net</title>
<link href="localhost/TecNet/standard.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>Home</h1>
<h2>this is a home site</h2> <em>© 2015</em>
</body>
</html>
And at last. The code i receive from Firefox. The Link is perfectly fine. If i try to access it directly from my browser it opens the css file. As inline CSS the code works perfect just that link refuses to work
EDIT:
my URL Structure for the controller.
http://localhost/TecNet/index.php/tecnet/view
And the controller itself
<?php
class Tecnet extends CI_Controller {
public function view($page = 'home')
{
if ( ! file_exists(APPPATH.'/views/pages/'.$page.'.php')) {
// Whoops, we don't have a page for that!
show_404();
}
$data['title'] = ucfirst($page); // Capitalize the first letter
$this->load->view('templates/header', $data);
$this->load->view('pages/'.$page, $data);
$this->load->view('templates/footer', $data);
}
}
?>
Try to load your style sheet like this:
<link rel="stylesheet" href="<?= base_url('TecNet/standard.css') ?>">
since CodeIgniter is based on the Model-View-Controller development pattern. Better you put resoures of css, img, js, etc.. in root folder instead on view like you did:
background-image: url("application/views/images/Console Background.png");
assume structures:
/standard.css
/assets/img/console-background.png
your standart.css file:
body {
background-image: url("assets/img/console-background.png");
color: white;
}
easy to call in view:
<link rel="stylesheet" href="<?= base_url('standard.css') ?>">
I use assests a bit differently in CI, load with controller dynamically, but maybe this can work for you:
<link href="<?php echo base_url(); ?>assets/css/your_style.css" rel="stylesheet" />
just replace path, so they will match to yours
Ok, let me explain:
I have a some files, something basic like this:
index.php
<html>
<head>
<title>Simple page</title>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
home.php
<div class="thisWillBeBlue">Still not blue</div>
style.css
.thisWillBeBlue {background: blue}
Now the question: Using php I want to insert the style.css inside the head tag, calling it from the file home.php. Well, I came out with a solution, but it was not very effective:
index.php
<?php $css = array();
$css[] = 'linktothecss.css'
?>
<html>
<head>
<title>Simple page</title>
<?php
foreach($css as $item){
echo "<link rel='stylesheet' href='".$item."' />";
}
?>
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
But the problem it is, If I call the css from home.php it will be added to the array later, therefore it will not be echoed inside the head tag. Any ideas?
You could do it using ob_start() and ob_end_flush() functions
e.g.
index.php
<?php
$csspage = "default.css";
function loadCSS($buffer) {
global $csspage;
return (str_replace('{{ css }}', $csspage, $buffer));
}
ob_start("loadCSS"); ?>
<html>
<head>
<!-- the string {{ css }} is just a placeholder that will be replaced
with the new value of $csspage defined later in the code, otherwise
it will replaced with its initial value (default.css)
-->
<link href="{{ css }}" />
</head>
<body>
<?php include 'home.php'; ?>
</body>
</html>
<?php ob_end_flush(); ?>
home.php
<?php $csspage = "custom_style.css"; ?>
<div class="thisWillBeBlue">blue</div>
Further reference: http://it1.php.net/ob_start
I think you are looking for something like this ..(include a piece of code in their header files, so that it will allow you to add more stylesheets )
This will allow you to add more stylesheets to it on each page.
(add this to <head>)
<?php
if (!empty($styles) && is_array($styles)) {
foreach ($styles AS $style) {
echo '<link rel="stylesheet" href="/assets/css/'. $style .'">';
}
}
?>
You can put a variable at the top of an individual script if you need a specific stylesheet:
<?php
$styles = array('custom_style.css');
?>
CSS file references can be placed in the body of your code, if needed.
<body>
<link href="linktothecss.css" rel="stylesheet" type="text/css" />
<div class="thisWillBeBlue">
I'll be blue as soon as linktothecss.css finishes loading!
</div>
</body>
The only difference is that when in the HEAD, they are guaranteed to be loaded before the page is rendered. When they are in the BODY, there may be a split-second where they are still loading and the styles haven't been applied yet.
If you definitely want them in the HEAD, you could define the css requirements in a separate folder with the same file name, like so:
index.php:
<html>
<head>
<?php
include('css-requirements/home.php');
?>
</head>
<body>
<?php include('home.php'); ?>
</body>
</html>
and
css-requirements/home.php:
<link href="mycss.css" rel="stylesheet" type="text/css" />
<link href="myothercss.css" rel="stylesheet" type="text/css" />
Having a strange zend framework issue. Although it's most likely something simple, I've been unable to resolve it so far. I have scraped the bottom of stackOverflow and several other community sites, as well as the zend documentation.
Any ajaxLink that I include in my layout refuses to function. They are classed properly, with hash placeholder in href attribute, but the javascript to activate the link is not being included in the page .
echo $this->jQuery; statement in layout is also failing. Might be the cause of ajaxLink failure. I have verified that I am properly adding the jQuery view helper in my bootstrap. Have tried using both ZendX_JQuery::enableView($view); and $view->addHelperPath("ZendX/JQuery/View/Helper", "ZendX_JQuery_View_Helper"); methods.
Thanks in advance
Excerpts from the relevant files follow:
bootstrap.php:
protected function _initViewHelpers()
{
$this->bootstrap('layout');
$layout = $this->getResource('layout');
$view = $layout->getView();
$view->doctype('HTML4_STRICT');
$view->headMeta()->appendHttpEquiv('Content-type', 'text/html;charset=utf-8')
->appendName('description', 'Business Club');
$view->headTitle()->setSeparator(' - ');
$view->headTitle('SOU Business Club');
$view->setHelperPath(APPLICATION_PATH.'/views/helpers', '');
ZendX_JQuery::enableView($view);
}
dashboardLayout.phtml:
<?php echo $this->doctype();?>
<html>
<head>
<?php echo $this->headTitle();?>
<?php echo $this->headMeta();?>
<?php
echo $this->headLink()->prependStylesheet($this->baseUrl().'/css/dashboardStyle.css');
echo $this->headLink()->prependStylesheet($this->baseUrl().'/js/jquery/css/smoothness/jquery-ui-1.8.11.custom.css');
?>
<?php
echo $this->headScript()->appendFile($this->baseUrl().'/js/paginator.js');
echo $this->jQuery();
?>
</head>
<body id="body">
<div id="wrapper">
<div><?php include "dashboardHeader.phtml"; ?></div>
<div id="bar">
Dashboard Home|
<?php
echo $this->ajaxLink('Club Roster | ',
'user/index',
array('update' => '#content',
'method' => 'post'),
array('format' => 'html')
);
?>
Google Docs|
Book Sale Management|
</div>
<div id="content" align="center">
<?php
echo $this->layout()->content;
?>
</div>
<div id="statusBar">
<?php
$userData = Zend_Registry::get('userData');
$name = $userData->name;
$role = $userData->role;
$status = 'Logged in as: '.' Name: '.$name.' Role: '.$role;
echo $status;
?>
<br />
Logout
</div>
<div><?php include "dashboardFooter.phtml"; ?></div>
</div>
</body>
</html>
HTML <head> output:
<head>
<title>SOU Business Club - Member Dashboard</title>
<meta http-equiv="Content-type" content="text/html;charset=utf-8" >
<meta name="description" content="Business Club" >
<link href="/css/dashboardStyle.css" media="screen" rel="stylesheet" type="text/css" >
<link href="/js/jquery/css/smoothness/jquery-ui-1.8.11.custom.css" media="screen" rel="stylesheet" type="text/css" >
<link href="/css/dashboardStyle.css" media="screen" rel="stylesheet" type="text/css" >
<script type="text/javascript" src="/js/paginator.js"></script>
</head>
In your bootstrap you need to specify where jQuery is, either on your local machine or a CDN. Locally you use:
$view->jQuery()->setLocalPath(PATH);
Or you could use a CDN such as Google's: http://code.google.com/apis/libraries/devguide.html#jquery