Jump to content

[SOLVED] Code formatting.


marcusfaye87

Recommended Posts

Hello again,

 

I'm the kind of programmer who likes to have their source code sepperated from the layouting in PHP.

I don't know what would be the best way to do this.

 

I could use templates, but I have no experiance in that area.

I already use a lot of functions and think I might switch to OOPHP.

 

But I don't have any Idea of how to start at that.

I have not gotten around implementing code in my Layout page.

 

For example, when I have my comments page of the newsposts,

There is a box that submits information to the database.

And it sort of has to always have a bit of PHP code.

Even if it's a simple as formatting how the comments are shown,

Or processing the POST data.

 

What would be the best way to go at it,

And if the answer has to be templates, any good tutorials you'd recommend?

 

Thanks in advance,

Marcus

Link to comment
Share on other sites

Well, for templating here is what I use. I am not even sure if this is what you referring to... but here goes.

 

header.php  -- This page controls the layout for all other pages.

<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
	<title><?php echo $page_title; ?></title>
</head>
<body>
	<div id="header"></div>
	<div id="left_nav"></div>
	<div id="main_body">
	  
	  <!-- All pages load here -->
	  
	  <?php function footer(){ ?>
	</div>
	<div id="footer"></div>
</body>
</html>
				<?php } ?>

 

I typically leave header.php in the root of the dir and then call it on my pages like this.

 

anotherpage.php

<?php  $page_title = 'Page Title'; include($_SERVER['DOCUMENT_ROOT'].'/header.php');  ?>
         
      Page content goes here

<?php footer(); ?>

 

Hope this helps.

 

Nate

 

 

Link to comment
Share on other sites

No I already use that.

 

I have a container file that uses a switch to show each page itself, but I mean, to format a news post

I have found no other way then to echo alle the HTML with the variables.

 

but by templating I mean, creating all of the HTML separately and change words to php code

 

For example: if you want a title somewhere you put {::TITLE::} in the HTML file and make PHP load in that file and replace your key words and print out that data.

Link to comment
Share on other sites

Sounds like you want template system: http://www.smarty.net/

 

Though I do want that I'm really looking for a tutorial of some kind for it, a clear tutorial, I read one before, and I still had no Idea what was going on.

 

The engine will work fine for sure, but I want to create it myself so I'll learn from it and actually know what I'm doing.

 

Thanks though

Link to comment
Share on other sites

I hate to sound so negative, but let me get this straight: You want us to help you code your own version of Smarty?  That would be an arduous task for a dedicated team of decent programmers, and would take quite a while.  You could always patch one together, using str_replace(), but what you are asking for sounds much bigger.

Link to comment
Share on other sites

I'm not asking for the help of a team, I was asking for a decent tutorial so I can do it on my own.

 

The project itself wouldn't be so big, for example:

 

I have a layout called "news_post.html".

That contains the layout of the news post.

And I just want to be able to replace certain things in it like: title, date, author, etc...

 

Rhodesa, thanks for the tutorial, I'll look into it.

 

but to further answer the question, I'm not the kind of guy that wants people to do stuff for me,

I'm the kind of guy that likes figuring some stuff out on his own, too bad you can't do that without the knowledge of the tutorial :)

So no offence taken

Link to comment
Share on other sites

a simple way would be to do something like this:

(example template file)

<?php
echo <<<END
<html>
<head>
	<title>$title</title>
</head>
<body>
	$body_text
</body>
</html>
END;
?>

and then just do this for the files that you want to display your template:

<?php
$title = "this is a test title";
$body_text = "this is a test body text";
include("template.php");
?>

 

Link to comment
Share on other sites

home.php

<table width=700 cellpadding=0 cellspacing=0>

<?php

// Select the news posts
$selectData = select_data ("news_posts", "id", 3);

// Loop through the posts
while($newsPost = mysql_fetch_array ($selectData))
{
// Select the news post and turn it into an array
$postRequest = select_data ("news_posts", "id", '5');
$postData = mysql_fetch_array ($postRequest);

// Look for the author
$postUserRequest = compare_data ("members", "id", $postData['user_id']);
$postUserData = mysql_fetch_array ($postUserRequest);

// Format our time for display
$postData['date'] = format_date ($postData['date_created'], 'date');
$postData['time'] = format_date ($postData['date_created'], 'time');

// Set the users title
$postUserData['title'] = set_user_title ($postUserData['rank']);

// Check for Commens
$postCommentsRequest = compare_data ("news_comments", "post_id", $postData[id]);
$postData['comments'] = mysql_num_rows ($postCommentsRequest);

// Echo the news Post
echo "	<tr>
		 <td id=menu>$postData[topic]</td>
		 <td> </td>
		</tr>
		<tr>
		 <td id=title>[ $postData[time] ] - $postData[date]</td>
		 <td id=titleRight>Author info:</td>
		</tr>
		<tr>
		 <td id=post>$postData[body]</td>
		 <td id=postRight>
		  
		  <img id=avatar src=\"avatars/$postUserData[avatar].png\"><br>
		  <a href=\"?view=profile&&user=$postUserData[username]\">$postUserData[username]</a><br>
		  <br>$postUserData[title]<br>
		  <img src=\"template/images/rank/$postUserData[rank].png\"><br>
		 <td>
		</tr>
		<tr>
		 <td id=comment>
		  <a href=\"?view=post&&id=$postData[id]\">Comments ($postData[comments])</a>
		 </td>
		 <td> </td>
		</tr>
		<tr>
		 <td colspan=2> </td>
		</tr>
	";
}
?>

</table>

 

just added this so you get where I'm at

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.