CSS along with PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

CSS along with PHP

Post by pilau »

I was always wondering how do forums get their template into PHP. What is the best way you know to implement CSS into PHP code, or to call a CSS document out of a PHP one?
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Best way to add CSS to anything is

Code: Select all

<link href=&quote;css/style.css&quote; rel=&quote;stylesheet&quote; type=&quote;text/css&quote; />
There is no such thing as "PHP document", but there is "PHP parsed document"
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

I know that, but that's HTML, how do I use it on PHP, just ECHO or PRINT it out?
User avatar
wwwapu
Forum Contributor
Posts: 197
Joined: Wed Apr 07, 2004 11:57 am
Location: Turku, Finland

Post by wwwapu »

Yes.

In most cases when I do pages its something like this

Code: Select all

<?php
// session, $_GET and $_POST stuff first
// functions also here
include('inc/page_start.php'); // Which includes <head> and common <body> parts

//then normal page generation by print
print'whatever';
//or if some conditions are to be met
if(condition){
  output_function_call();
}else some_other_output_function();

//and then common page ending
include('inc/page_stop.php');
?>
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

With Smarty, it's something like:

Code: Select all

{include file=&quote;meta/doctype.tpl&quote;}
{include file=&quote;meta/header.tpl&quote;}
Do some stuff here.
{include file=&quote;meta/footer.tpl&quote;}
USE TEMPLATING SYSTEMS! WHOO!
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

If you just need one line of HTML output from PHP you would echo it. But it gets to be rediculous when you have a lot of HTML output mixed with a little PHP output.

You can mix php and html by ending the php statements and just having plain html.

Something like:

Code: Select all

<?php

//Some begining Code

?>

<html>
<head>
<title>Your Title</title>
<link type="text/css" rel="stylesheet" href="style.css" title="style">
</head>
<body>

<?php

//Some more PHP

?>

<!-- Some HTML -->

<!-- PHP doesn't have to take up a whole block either, you could do it inline with HTML tags like: -->

<table>
  <tr>
    <td>Name:</td>
    <td><?php echo $name; ?></td>
  </tr>
  <tr>
    <td>Email:</td>
    <td><?php echo $email; ?></td>
  </tr>
</table>

</body>
</html>
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

Ambush Commander wrote:With Smarty, it's something like:

Code: Select all

{include file=&quote;meta/doctype.tpl&quote;}
{include file=&quote;meta/header.tpl&quote;}
Do some stuff here.
{include file=&quote;meta/footer.tpl&quote;}
USE TEMPLATING SYSTEMS! WHOO!
This was the most useful yet ;) Thanks a lot!
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

Ah well, i usually generate XML that contains a subnode like:

Code: Select all

<page>
 <cssfiles>
  <file>/styles/style1.css</file>
  <file>/styles/sub.css</file>
 </cssfiles>
</page>
And when i perform the XSLT i have a std.head.xsl file that does:

Code: Select all

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl=&quote;http://www.w3.org/1999/XSL/Transform&quote; version=&quote;1.0&quote;>

<xsl:template name=&quote;head&quote;>
 <head>
  <title><xsl:value-of select=&quote;//page/title&quote;/></title>
  <xsl:for-each select=&quote;//page/cssfiles/file&quote;>
   <link rel=&quote;stylesheet&quote; type=&quote;text/css&quote; href=&quote;{.}&quote; />
  </xsl:for-each> 
  </head>  
</xsl:template>
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

The way I do it is this:
template.inc.php contains my template:

Code: Select all

&lt;html&gt;
... whatever ...
&lt;div id=&quote;main&quote;&gt;
&#1111;#contents#]
&lt;/div&gt;
... whatever ...
&lt;/html&gt;
[#contents#] marks where the script will output to, then on a page i want to use the template:

Code: Select all

<?
require_once("class.php");
disp('header');
echo ('Welcome to my amazing web site');
disp('footer');
?>
Now what class.php does is defines the function disp() which is basically a switch, if the paramater is 'header' it starts the output buffering, if it is 'footer' it grabs the buffer and stores it in a variable, then it opens template.php and replaces [#content#] with that data.

I tend to suck horribly at explaining things but implementing this is easy and it's really nice to be able to edit template.inc.php in a wysiwyg without the fear of messing up any php code.

This method is similar to a lot of the xml based templating systems people use except my way does not require me to think as much and in turn my brain doesn't hurt.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post by timvw »

This method is similar to a lot of the xml based templating systems people use except my way does not require me to think as much and in turn my brain doesn't hurt.
As with all things, you need to invest time to learn it.


I'm happy with XSL because i can use my templates for J2EE/Resin applications too..
pilau
Forum Regular
Posts: 594
Joined: Sat Jul 09, 2005 10:22 am
Location: Israel

Post by pilau »

So all I have to do is just require_once an HTML file in my PHP document?
Post Reply