Linking site-wide CSS file with CakePHP - php

I'm very new to PHP, and just customizing my homepage. This is the main layout I have for all of my pages:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title_for_layout ?></title>
<?php echo $scripts_for_layout ?>
</head>
<body>
<div id="header">
</div>
<div id="content">
<?php echo $content_for_layout ?>
</div>
<div id="footer">
</div>
</body>
</html>
I want to have a global CSS file for all pages on my site called "style.css" which will be stored in app/webroot/css/style.css. I have tried doing this:
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title_for_layout ?></title>
<link href="<?php $html->css('style', 'stylesheet', array('media'=>'all' ), false); ?>" rel="stylesheet" type="text/css" />
<?php echo $scripts_for_layout ?>
</head>
<body>
<div id="header">
</div>
<div id="content">
<?php echo $content_for_layout ?>
</div>
<div id="footer">
</div>
</body>
</html>
But it's not showing up in the output HTML. Any ideas?
Thanks!

you are definitely new to Cake :) here:
<title><?php echo $title_for_layout ?></title>
<?php
echo $this->Html->css('style');
echo $scripts_for_layout;
?>
That will output the full link element for you.

Related

PHP - Include file and execute code in it

I have this code:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title><?php echo $title; ?></title>
</head>
<body>
<?php foreach ($posts as $post) { ?>
<div class="post">
<h3 class="title"><?php $post["title"]; ?></h3>
<p class="text"><?php $post["text"]; ?></p>
</div>
<?php } ?>
</body>
</html>
And I need to include it in other file and execute all the PHP code in it. How can i do it?
Include with absolute path :
<?php include($_SERVER["DOCUMENT_ROOT"].'/yourdirectory/yourfile.php'); ?>
use the include() function.
include("the file.php")

How do I put my content in the right place and for all my pages?

I need help for something please. I'm working on my structure, but I've some problems...
I created a page called "page-container.php" and the content is:
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<?php if(isset($sidebar)): ?>
<div class="main-container">
<div class="main-content">
<?php endif; ?>
<h1 class="page-title"><?= $page_title; ?></h1>
I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES
</div>
</div>
</body>
</html>
You can see the "I WANT TO HAVE MY CONTENT HERE FOR ALL MY PAGES".
What I mean is that when I write the content in my other pages (for example, my registration page), I want that all the content is placed at this location, like this:
http://prntscr.com/a3oz5k
All my other pages are like this (exemple with index.php):
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
If I want to write some content in this page, I need to do like this:
<?php
$page_title = "Home";
$page_id = "home";
?>
<?php require "includes/page-container.php"; ?>
<div id="mycontent">
<p>Heellloo</p>
</div>
But look the result: http://prntscr.com/a3p32y
In the code source this is not normal... http://prntscr.com/a3p3kg
My content is below the html and body tag and I want it to be placed just below my h3 title. How can I do please?
Simple example using ob_start(), ob_get_clean().
<?php
ob_start();
?>
<div id="mycontent">
<p>Hello</p>
</div>
<?php
$page = ob_get_clean();
require "includes/page-container.php";
?>
And then in page-container.php just do echo $page;.
Your code will be still formatted as HTML in your editor instead if it would be in a variable.
You can also split your page-container.php into two parts and include the first part before the content and the second one after.
Reference: ob_start(), ob_get_clean()
You can keep another vartiable say $page_content and use as below :
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title><?= $page_title; ?></title>
<link href="css/style.css" rel="stylesheet">
<link href="css/fonts/font-awesome/4.5.0/css/font-awesome.min.css" rel="stylesheet">
<link href="https://fonts.googleapis.com/css?family=Patrick+Hand" rel="stylesheet">
</head>
<body id="<?= $page_id; ?>">
<?php include "includes/header.php"; ?>
<div class="wrapper">
<div class="page-content">
<h1 class="page-title"><?= $page_title; ?></h1>
<?= $page_content; ?>
</div>
</div>
</body>
</html>
And for some content in another page
<?php
$page_title = "Home";
$page_id = "home";
$page_content = "content";
?>
<?php require "includes/page-container.php"; ?>
`

Remove sidebar on some pages on Wordpress

I am trying to edit a Worpress template. Everything work as as expected except that I wish to have a page without sidebar - which is not included in that template.
To achieve this I went to the template's folder, duplicated the "page.php" and named it page-nosidebar.php. Over there I deleted the get_sidebar call script. Here is how it looks like now:
<?php /*
Template Name: No Sidebars
*/ ?>
<?php get_header(); ?>
<div id="single_cont">
<div class="single_left">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
<h1 class="single_title"><?php the_title(); ?></h1>
<div class="single_inside_content">
<?php the_content(); ?>
</div><!--//single_inside_content-->
<br /><br />
<?php //comments_template(); ?>
<?php endwhile; else: ?>
<h3>Sorry, no posts matched your criteria.</h3>
<?php endif; ?>
</div><!--//single_left-->
<div class="clear"></div>
</div><!--//single_cont-->
<?php get_footer(); ?>
After this, I went to the page editor and assigned this new template to the page I wanted to have no sidebar. Problem? Sidebar still shows up.
How should I do? Thanks in advance!
UPDATE
footer.php
<div class="clear"></div>
<div id="footer">
<div class="footer_text"> <?php echo date("Y"); ?> Company. Powered by Company</div>
</div><!--//footer-->
</div><!--//main_container-->
<?php wp_footer(); ?>
</body>
</html>
header.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml"> <head> <link href='http://fonts.googleapis.com/css?family=Raleway' rel='stylesheet' type='text/css'> <link href='http://fonts.googleapis.com/css?family=Raleway:400,100,200' rel='stylesheet' type='text/css'> <link href='http://fonts.googleapis.com/css?family=Cabin:700' rel='stylesheet' type='text/css'> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> <title><?php wp_title('«', true, 'right'); ?> <?php bloginfo('name'); ?></title> <?php wp_head(); ?> <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> <!--[if lt IE 9]> <script src="http://css3-mediaqueries-js.googlecode.com/svn/trunk/css3-mediaqueries.js"></script> <![endif]--> <!--<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>--> <script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery-latest.js"></script> <script type="text/javascript" src="<?php bloginfo('stylesheet_directory'); ?>/js/scripts.js"></script> <script src="<?php bloginfo('stylesheet_directory'); ?>/js/jquery.infinitescroll.js" type="text/javascript" charset="utf-8"></script> <link rel="stylesheet" href="<?php bloginfo('stylesheet_url'); ?>" type="text/css" media="screen" title="no title" charset="utf-8"/> <script type="text/javascript"> $(document).ready( function($){ $('#content_inside').infinitescroll({
navSelector : "div.load_more_text",
// selector for the paged navigation (it will be hidden)
nextSelector : "div.load_more_text a:first",
// selector for the NEXT link (to page 2)
itemSelector : "#content_inside .home_post_box"
// selector for all items you'll retrieve },function(arrayOfNewElems){
$('.home_post_box').hover( function() { $(this).find('.home_post_text').css('display','block'); }, function () { $(this).find('.home_post_text').css('display','none'); } );
//$('.home_post_cont img').hover_caption();
// optional callback when new content is successfully loaded in.
// keyword `this` will refer to the new DOM content that was just added.
// as of 1.5, `this` matches the element you called the plugin on (e.g. #content)
// all the new elements that were found are passed in as an array
}); } ); </script> </head> <body> <?php $shortname = "neue"; ?> <?php if(get_option($shortname.'_background_image_url','') != "") { ?> <style type="text/css"> body { background-image: url('<?php echo get_option($shortname.'_background_image_url',''); ?>'); } </style> <?php } ?> <div id="main_container"> <div id="header"> <div class="left"> <?php if(get_option($shortname.'_custom_logo_url','') != "") { ?>
<img src="<?php echo stripslashes(stripslashes(get_option($shortname.'_custom_logo_url',''))); ?>" class="logo" /> <?php } else { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/logo.jpg" class="logo" /> <?php } ?> </div>
<div class="right"> <div class="head_social">
<?php if(get_option($shortname.'_twitter_link','') != "") { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/twitter-icon.png" />
<?php } ?>
<?php if(get_option($shortname.'_facebook_link','') != "") { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/facebook-icon.png" />
<?php } ?>
<?php if(get_option($shortname.'_google_plus_link','') != "") { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/google-plus-icon.png" />
<?php } ?>
<?php if(get_option($shortname.'_dribbble_link','') != "") { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/dribbble-icon.png" />
<?php } ?>
<?php if(get_option($shortname.'_pinterest_link','') != "") { ?>
<img src="<?php bloginfo('stylesheet_directory'); ?>/images/pinterest-icon.png" />
<?php } ?>
<div class="clear"></div> </div><!--//head_social-->
<div class="header_menu"> <!--
<ul>
<li>HOME</li>
<li>ABOUT</li>
<li>CATEGORIES
<ul>
<li>Wordpress Themes</li>
<li>Create Plugins</li>
<li>Wordpress Themes</li>
<li>Create Plugins</li>
</ul>
</li>
<li>BLOG</li>
<li>CONTACT</li>
</ul>-->
<?php wp_nav_menu('menu=header_menu&container=false&menu_id='); ?>
<div class="clear"></div> </div><!--//header_menu-->
<div class="clear"></div> </div> <div class="clear"></div> <div class="tagline"> <?php echo get_option($shortname.'_header_text','Use Neue Theme Settings to update this text...') ?> </div><!--//tagline-->
</div><!--//header-->

How to add content to PHP include files?

Ok I may not have the best title , but I will try to give a better explanation.
Let's assume you are using PHP include() to structure your website:
Header.php
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name=keywords content="somthiefn"/>
<title>Website</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<script src="Scripts/jquery-1.8.3.min.js"></script>
<link rel="icon" type="image/png" href="/images/favicon.ico" />
</head>
<body>
Footer.php
</body>
</html>
Then a sample page:
Index.php
<!--Header-->
<?php include ('includes/header.php'); ?>
<div class="content">
<!-- some content-->
</div>
<!--Footer-->
<?php include ('includes/footer.php'); ?>
Basically I just want to know if there is a way to load some script into my header section.
I would like to achieve something like ASP.NET and its master Page, where I can just add content the header. for example
<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<script src="Scripts/somescript.js"></script>
</asp:Content>
Yes, you can do like this:
index.php
<?php
// Wanted page title
$title = "some";
// JS Files..
$scripts = array();
$scripts[] = '<script src="js/some.js" />';
$scripts[] = '<script src="js/some2.js" />';
$scripts[] = '<script src="js/some3.js" />';
// Include header
include ('includes/header.php');
header.php
<title><?php echo $title ?></title>
<?php echo implode("\n",$scripts) ?>
Of course the variables could be named as you want and contain any data. Main thing is that you can pass them between files like i showed.
In index.php, before you include header.php, you could set an array for your scripts like:
header.php
<?php if(!isset($scripts)) $scripts = array(); ?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name=keywords content="somthiefn"/>
<title>Website</title>
<link rel="stylesheet" href="css/style.css" type="text/css">
<script src="Scripts/jquery-1.8.3.min.js"></script>
<link rel="icon" type="image/png" href="/images/favicon.ico" />
<!-- Include dynamic scripts-->
<?php foreach($scripts in $script): ?>
<script src="<?php echo $script; ?>"></script>
<?php endforeach;?>
</head>
<body>
index.php
<?php
$scripts = array('Scripts/somescript.js', 'http://server.com/path/anoterscript.js);
?>
<!--Header-->
<?php include ('includes/header.php'); ?>
<div class="content">
<!-- some content-->
</div>
<!--Footer-->
<?php include ('includes/footer.php'); ?>
Do you want to add PHP code to the header? Then you can add your code between tags (or tags if short tags are enabled), but I would consider to just call an include between the PHP tags and have the logic in that include.
Another option could be a template engine, e. g. Smarty. In most of the templates engine you can define your own functions and call them from the templates.
You could do this:
// content.php
<?php
$head = "<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<meta name='keywords' content='somthiefn' />
<title>Website</title>
<link type='text/css' rel='stylesheet' href='css/style.css' />
<script src='Scripts/jquery-1.8.3.min.js'></script>
<link type='image/png' rel='icon' href='images/favicon.ico' />
</head>
<body>";
$foot = "\n<body>\n</html>";
?>
// index.php
<?php
include 'includes/content.php';
$dom = new DOMDocument; #$dom->loadHTML($head);
$hd = $dom->getElementsByTagName('head'); $hd = $hd->item(0);
$script = $dom->creatElement('script');
$scriptAttr = $dom->createAttribute('src');
$scriptAttr->value= 'Scripts/somescript.js'; $script->appendChild($scriptAttr);
$hd->appendChild($script);
echo $dom->saveHTML().$foot;
?>
The other option is to use variables to separate your code then only echo portions. That is what I would do. It's technologically faster. Try this:
// content.php
<?php
$headTop = "<!DOCTYPE html>
<html>
<head>
<meta http-equiv='content-type' content='text/html;charset=utf-8' />
<meta name='keywords' content='somthiefn' />
<title>Website</title>
<link type='text/css' rel='stylesheet' href='css/style.css' />
<script src='Scripts/jquery-1.8.3.min.js'></script>\n ";
$headBottom = "\n <link type='image/png' rel='icon' href='images/favicon.ico' />
</head>
<body>";
$foot = "\n<body>\n</html>";
?>
// index.php
<?php
include 'includes/content.php';
echo "$headTop<script src='Scripts/somescript.js'></script>$headBottom$foot";
?>
You can see why you would use the second option.
This works for me. It dynamically loads title, scripts and styles.
header.php
<?php
if(!isset($scripts))
$scripts = array();
if(!isset($styles))
$styles = array();
?>
<!DOCTYPE html>
<html>
<head>
<title><?php echo $title ?></title>
<link rel="stylesheet" href="css/somestyle.css" type="text/css">
<script src=somescript.js"></script>
<!-- include styles -->
<?php foreach($styles as $style): ?>
<link href="<?php echo $style; ?>" rel="stylesheet">
<?php endforeach; ?>
<!-- include scripts-->
<?php foreach($scripts as $script): ?>
<script src="<?php echo $script; ?>"></script>
<?php endforeach;?>
</head>
<body>
index.php
<?php
$title = "some dynamic title";
$scripts = array('js/somescript.js', 'http://server.com/anoterscript.js');
$styles = array('css/somestyle.css', 'css/anotherstyle.css');
require_once ('header.php');
?>
<div class="content">
<!-- some content-->
</div>
<?php require_once('footer.php'); ?>

problem in shorthand php tag of files in cake php

I have this simple code:
<html>
<head>
<title>My Cake Blog Application</title>
<?=$html->css('styles');?>
</head>
<body>
<div id="container">
<div id="content">
<?=$content_for_layout;?>
</div>
</div>
</body>
</html>
I saved it in app/views/layouts
name it to default.ctp
the output in browser only shows me css('styles');
also I have styles.css in app/webroot/css
it is not working in my browser then I tried this string for begin and close:<?php echo();?>
again not worked
<html>
<head>
<title>My Cake Blog Application</title>
<?php=$html->css('styles'); echo();?>
</head>
<body>
<div id="container">
<div id="content">
<?php=$content_for_layout; echo();?>
</div>
</div>
</body>
</html>
Either use full tags as you already found out or enable short tags in php.ini: How to enable PHP short tags?
<html>
<head>
<title>My Cake Blog Application</title>
<?php echo $html->css('styles'); ?>
</head>
<body>
<div id="container">
<div id="content">
<?php echo $content_for_layout; ?>
</div>
</div>
</body>
</html>

Categories