Page 1 of 1

PHP Generated Stylesheet not modifying layout as .. (solved)

Posted: Sat Jul 07, 2007 9:18 pm
by Charles256

Code: Select all

<?php
include("template.class.php");
$template=new template();
$template->set_title("Testing");
$template->set_body("Test");
$template->set_css("Invalid CSS");
$template->output();
?>
Above is the offending code. See the part where I pass it invalid CSS? That's supposed to kick out an error, to show how that is done I need to show the innards of my class. Here is the set css function

Code: Select all

public function set_css($css)
	{
		$css_code=$css;
		if(is_file($css))
		{
			$handle=fopen($css,"r");
			$css_code=stream_get_contents($handle);
			fclose($handle);
			$file=true;
		}
		if (stripos($css_code,"<script type='text/css'>") 
			|| 
			stripos($css_code,"<script type=\"text/css\">"))
		{
			if (stripos($css_code,"</script>"))
			{
				if($file)
				{
					$this->css_external[]=$css;
				}
				else 
				{
					$this->css[]=$css_code;
				}
			}
			else 
			{
				$this->error[]="CSS must have a valid closing tag, \"</script>\"";
			}
		}
		else 
		{
			$this->error[]="CSS must have a valid starting tag, \"<script type='text/css'>\" 
			or \"<script type=\"text/css\"> \"";
		}
	}
In this case it has no valid starting tag so the first check will create the error. Now on to how the page is generated...

Code: Select all

public function output()
	{
		$this->check_empty();
		if(sizeof($this->error)>0)
		{
			$this->get_error();
			$this->construct_page();
		}
		else 
		{
			$this->construct_page();
		}
		
	}
In our case error is larger than 0 so get_error is called so let's see that code..

Code: Select all

private function get_error()
	{
		unset($this->body,$this->doctype,$this->title,$this->javascript,$this->javascript_external);
		unset($this->css,$this->css_external);
		$this->set_doctype("XHTML 1.0 Strict");
		$this->set_title("Templating Error");
		$this->set_css("
		<script type=\"text/css\">
		body
		{
			background-color:#66AADD;
			text-align:center;
			font-weight:bold;
			
		}
		</script>");
		foreach($this->error as $error_message)
		{
			$this->set_body("
			<div class='error_message'>
				$error_message
			</div>
			");
		}
	}
Basically that unsets everything so that whenever someone screws up on the creation of a page using the template system they get a custom error page or that's where I was heading... Now as for the construct function so you can see how the page is finally made.

Code: Select all

private function construct_page()
	{
		
		ob_start();
		?>
		<?php echo $this->valid_doctypes[$this->doctype];?>
		<html>
			<head>
				<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
				<title>
					<?php echo $this->title;?>
				</title>
				<?php
				echo $this->display_javascript();
				echo $this->display_css();
				?>
			</head>
			<body>
				<?php echo $this->body;?>
				<?php echo $this->footer;?>
			</body>
		</html>
		<?php
		ob_flush();
	}

Nothing too complex I'd think but the css style's aren't applying and I'm thinking it has something to do with the way I'm using output buffering. If someone could provide some insight? To see the page in action simply go to http://ourlifeproject.com/monopoly/. Let me know if there's anything else I can add to help clear this up.

And to save you the trouble of right clicking and hit view source...

Code: Select all

		<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">		<html>
			<head>
				<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
				<title>
					Templating Error				</title>
				
		<script type="text/css">
		body
		{
			background-color:#66AADD;
			text-align:center;
			font-weight:bold;
			
		}
		</script>			</head>
			<body>
				
			<div class='error_message'>

				CSS must have a valid starting tag, "<script type='text/css'>" 
			or "<script type="text/css"> "
			</div>
										</body>
		</html>
		
Edit: Perhaps I should try style instead of script....