Jump to content

Recommended Posts

Hey, Ive been working on my own template system that parsers a static html file so it's easier to update. But say if I wanted to create a function that fetches data from mysql or something and replace this with...

 

$tags_array = array('header'=>'<h1>Header</h1>','center'=>MyFunction(),'footer'=>'<h2>Footer</h2>');

 

The output from that function appears at the very top of the page for some reason. Any help please? Thanks.

 

Heres the code i'm using...

 

<?php

function parse_template($template,$tags_array){

$template = file_get_contents($template);

foreach($tags_array as $current_tag => $new_tag){
$template = str_replace("{".$current_tag."}",$new_tag,$template);
}
return $template;
}
$tags_array = array('header'=>'<h1>Header</h1>','footer'=>'<h2>Footer</h2>');

echo parse_template('example_template.html',$tags_array);

?>

Link to comment
https://forums.phpfreaks.com/topic/134233-template-help/
Share on other sites

Okay. I've tried this just to test it out...

 

<?php

$array = array('one','two','three','four','five');
foreach($array as $key => $value){
return $key.$value;
}

?>

 

But it only seems to output the element of the array?

You have to concatenate all of the values into one string. Then outside of the loop return the finished string.
Link to comment
https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700249
Share on other sites

Sure...

 

Say i wanted to display some results from mysql and show how many rows there are...

 

<?php

function MyFunction(){

$query = mysql_query("SELECT * FROM example");

while($row = mysql_fetch_array($query)){
return $row['id'].' - '.$row['name'];
}
return 'There are '.mysql_num_rows($query).' rows in the database.';
}

?>

 

Where would i go from there?

Link to comment
https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700391
Share on other sites

I think you need to take another look at what return actually does. This isn't pretty, but its your design.

 

<?php

function MyFunction() {
  if ($result = mysql_query("SELECT id, name FROM example")) {
    if (mysql_num_rows($result)) {
      $return = array();
      $return['num_rows'] = mysql_num_rows($result);
      while ($row = mysql_fetch_array($result)) {
        $return['result'][] = array($row['id'], $row['name']);
      }
      return $return;
    }
  }
  return false;
}

?>

 

And now to call it...

 

<?php

  if ($array = MyFunction()) {
    echo "There are {$array['num_rows']} rows in the database.";
    foreach ($array['result'] as $row) {
      echo $row['id'] . ' - ' . $row['name'];
    }
  }

?>

 

Ive changed your variable names around a little to make them more descriptive. Also, put some error handling in place.

Link to comment
https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700402
Share on other sites

I wouldn't bother with any templating, it just adds overhead you don't need.

 

Its easy enough to design your code in such a way that there is only minimal amounts of php mixed in with html.

 

Just my 2 cents though.

I'm in agreement with thorpe. Though to add, it's usually like having "template" files that use PHP within them, though only minimally as thorpe stated.

 

ie:

<p>Some static stuff... <b>Hi <?php echo $name; ?></b></p>

 

Typically within these files instead of using if and other control statements with brackets i use the alternate structures.

 

Link to comment
https://forums.phpfreaks.com/topic/134233-template-help/#findComment-700756
Share on other sites

I wouldn't bother with any templating, it just adds overhead you don't need.

 

Its easy enough to design your code in such a way that there is only minimal amounts of php mixed in with html.

 

Just my 2 cents though.

 

I don't agree with that 100%.  As a programmer I don't see a hugh difference but i know a few designer that perfer a template system then php.  Also if the template system does compiling and caching right, the over head is only on the first call and then it is barely noticeable like Smart/Dwoo(not something like XTemplates which does not compile of cache).

Link to comment
https://forums.phpfreaks.com/topic/134233-template-help/#findComment-701341
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.