Jump to content

Dynamic Templates with PHP


owner

Recommended Posts

Hello,

 

I don't know if this is even possible, but I was wondering if their is a better way to be doing things when displaying php and html code.  (This might be considered using OOP as I am trying to convert from normal php coding to OOP.)

 

Here is a snippet off my news code that I have right now.

 

<div id="content">
<?php do { ?>
    <div class="boxtop"><h4>
    <?php echo $row['news_title']; ?>
    </h4></div>
    <div class="boxbottom"><br />
    <?php echo $lang_id . $row['news_id']; ?><br />
    <?php echo $lang_author . $row['news_author']; ?><br />
    <?php echo $lang_date. $row['news_date']; ?><br />
    <?php echo $row['news_body']; ?></div>

   <?php } while ($row = mysql_fetch_array($result)); ?> 
</div>

 

Now you can see in the code above, im just grabbing some info out of the database and looping this a few times to show new blocks for my news.  Anyways, I was wondering if you could make a file like news.php and then inside you would have something like this.

 

<?php

function news($new_title="") {
<div><b>{$news_title}</b></div>
}
?>

 

That way, I can just grab this function and loop that instead of mixing a ton of html and php in my source (This sucks right now because in a login system, the html just gets in the way big time).

 

Please put up any pointers in the right direction.  (Note: I am just starting to learn the basics of php and php using OOP, so please dont beat me up with a stick for not knowing a lot of stuff  :P)

 

Thanks in advance!

-Owner

 

 

Link to comment
Share on other sites

You could resort to OOP, but that all depends on how big this script is. If it isn't very big, I would just write a simple function.

 

<?php

function display_news(){
   //query database

   while ($row = mysql_fetch_assoc($result)){
   
       <div class="boxtop"><h4>
       <?php echo $row['news_title']; ?>
       </h4></div>
       <div class="boxbottom"><br />
       
       <?php
       echo $lang_id . $row['news_id'].'<br />';
       echo $lang_author . $row['news_author'].'<br>';
       echo $lang_date. $row['news_date'].'<br />';
       echo $row['news_body'];</div>
  
   }
}
?>

 

Now, wherever you want to display the news, all you have to do is

display_news();

 

Obviously that function needs to be changed up depending on how you want it to work. You could add parameters to the function to only display a certain amount of results, or whatever.

Link to comment
Share on other sites

Hello,

 

That isn't what I really want to do.  I really want to convert to OOP as I might recall the functions many times and it would be a lot more sorted.

 

What I want to do is use code like

 

<?php

function news($new_title="") {
<div><b>{$news_title}</b></div>
}
?>

 

and have the {$news_title} be replaced.

A big example of what I want to do is to be able to load the templates like forum software does.  That way you can sort your php scripts from your html data.

Link to comment
Share on other sites

Sorry, I was wronge with that code.  This is what I really want to do.

 

<?php
function show_row($session="") {
$html = "";
$html .= "<tr>
			<td class=\"row1\">{$session['member_name']}</td>
			<td class=\"row1\" align=\"center\">{$session['msg_icon']}</td>
		</tr>";
return $html;
}

?>

 

See, you would store a ton of functions and then call each block of code.  And then as you see, the variables will be replaced with the members actual name, the time, etc.  See what I am trying to do?  (Note: This little section of code was taken from the bb software IPB.)

Link to comment
Share on other sites

If you build out your functions to include HTML formatting and the variables, you're just going to be making a lot more work for yourself in the long run.  In your example show_row() function, what if you wanted to display that outside of a table, or in a table that had something other than two columns? 

 

You'd have to create a new function for every variation you used, making your code a lot less reusable, harder to read, and longer to write and update.  If you want to make your own templating class, make the functions either insert HTML around variables, or get and clean the variables for display, not both.

 

Link to comment
Share on other sites

If you build out your functions to include HTML formatting and the variables, you're just going to be making a lot more work for yourself in the long run.  In your example show_row() function, what if you wanted to display that outside of a table, or in a table that had something other than two columns? 

 

You'd have to create a new function for every variation you used, making your code a lot less reusable, harder to read, and longer to write and update.

 

Ok, I see what you are saying here.

 

If you want to make your own templating class, make the functions either insert HTML around variables, or get and clean the variables for display, not both.

 

I don't get what you mean here.  Could you show me an example?

Link to comment
Share on other sites

I think you want something like phpbb

 

so in the template file they have

 

<div>{L_SOMETHING}</div><h1>{L_SOMETHING_OTHER}</h1>

 

then in the php files

 

$template->assign_vars(array(
	'L_SOMETHING' => $lang['Something'],
	'L_SOMETHING_OTHER' => $lang['Something_Other'])
);

 

And then in the language files

 

$lang['Something'] = 'Some Text';

I think you want something like that. And just for the record I was gonna ask in the freelancer on how to do this 

~ Chocopi
$lang['Something_Other'] = 'More Text';

Link to comment
Share on other sites

that is exactly what I am looking for!!!!

 

 

Thank you very much, but I don't understand it 100%, would you mind explaining it a little.  I am pretty new with php so I am dumb with the coding a little.

 

I have my language file with

 

 

<?php

$lang = array(

'Something'			=>	'Some Text',
'Something_Other' 	=>	'More Text'
);

?>

 

and my index file that will display everything with this code

 

<?php
include "./lang.php";
array('L_SOMETHING' => $lang['Something'],'L_SOMETHING_OTHER' => $lang['Something_Other'])
?>

<div>{L_SOMETHING}</div><h1>{L_SOMETHING_OTHER}</h1>

 

Yet, its not replacing yet

Link to comment
Share on other sites

To be honest I don't think someone is going to spend the time writing the script and helping you, seeing as it wouldn't be an easy job and it would be very time consuming when there are other users who need help.

 

I just googled it and though I haven't completly looked through it you should give this a try:

http://www.pixel2life.com/publish/tutorials/457/simple_template_engine_find_and_replace_variables/

 

EDIT: I just downloaded it and the code works, apart from you have to make 2 changes. So I suggest you try this out as it seems to be a very useful resource

 

Good Luck ;D

 

~ Chocopi

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.