CSS along with PHP
Moderator: General Moderators
CSS along with PHP
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?
Best way to add CSS to anything is
There is no such thing as "PHP document", but there is "PHP parsed document"
Code: Select all
<link href="e;css/style.css"e; rel="e;stylesheet"e; type="e;text/css"e; />Yes.
In most cases when I do pages its something like this
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');
?>- Ambush Commander
- DevNet Master
- Posts: 3698
- Joined: Mon Oct 25, 2004 9:29 pm
- Location: New Jersey, US
With Smarty, it's something like:
USE TEMPLATING SYSTEMS! WHOO!
Code: Select all
{include file="e;meta/doctype.tpl"e;}
{include file="e;meta/header.tpl"e;}
Do some stuff here.
{include file="e;meta/footer.tpl"e;}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:
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>This was the most useful yetAmbush Commander wrote:With Smarty, it's something like:
USE TEMPLATING SYSTEMS! WHOO!Code: Select all
{include file="e;meta/doctype.tpl"e;} {include file="e;meta/header.tpl"e;} Do some stuff here. {include file="e;meta/footer.tpl"e;}
Ah well, i usually generate XML that contains a subnode like:
And when i perform the XSLT i have a std.head.xsl file that does:
Code: Select all
<page>
<cssfiles>
<file>/styles/style1.css</file>
<file>/styles/sub.css</file>
</cssfiles>
</page>Code: Select all
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="e;http://www.w3.org/1999/XSL/Transform"e; version="e;1.0"e;>
<xsl:template name="e;head"e;>
<head>
<title><xsl:value-of select="e;//page/title"e;/></title>
<xsl:for-each select="e;//page/cssfiles/file"e;>
<link rel="e;stylesheet"e; type="e;text/css"e; href="e;{.}"e; />
</xsl:for-each>
</head>
</xsl:template>The way I do it is this:
template.inc.php contains my template:
[#contents#] marks where the script will output to, then on a page i want to use the template:
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.
template.inc.php contains my template:
Code: Select all
<html>
... whatever ...
<div id="e;main"e;>
ї#contents#]
</div>
... whatever ...
</html>Code: Select all
<?
require_once("class.php");
disp('header');
echo ('Welcome to my amazing web site');
disp('footer');
?>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.
As with all things, you need to invest time to learn it.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.
I'm happy with XSL because i can use my templates for J2EE/Resin applications too..