Page 1 of 1

CSS along with PHP

Posted: Sun Jul 24, 2005 6:23 am
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?

Posted: Sun Jul 24, 2005 6:30 am
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"

Posted: Sun Jul 24, 2005 7:38 am
by pilau
I know that, but that's HTML, how do I use it on PHP, just ECHO or PRINT it out?

Posted: Sun Jul 24, 2005 8:01 am
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');
?>

Posted: Sun Jul 24, 2005 8:17 am
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!

Posted: Sun Jul 24, 2005 8:23 am
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>

Posted: Sun Jul 24, 2005 12:49 pm
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!

Posted: Sun Jul 24, 2005 7:53 pm
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>

Posted: Sun Jul 24, 2005 9:40 pm
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.

Posted: Sun Jul 24, 2005 10:20 pm
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..

Posted: Mon Jul 25, 2005 6:37 am
by pilau
So all I have to do is just require_once an HTML file in my PHP document?