View Single Post
Old 12-23-2007, 02:17 PM   #7 (permalink)
Tanax
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Quote:
Originally Posted by sjaq View Post
I'm assuming you understand regular expressions and I don't have to explain that part..

What that line does is that it searches for all occurrences of {([a-z0-9\.]+)} within the template file (which contents are in $contents) and then parses the matched string to $this->replace_callback.

Then the replace_callback method gets a string eg. "user.posts.count". It then "explodes" that string by each occurrence of "." so we get an array which looks something like this:
PHP Code:
Array
(
    [
0] => user
    
[1] => posts
    
[2] => count

And then it selects the correct data from the array(the second argument of the parse method) and returns it to the preg_replace_callback method, which in turn replaces the matched string by the returned data in the $contents string.

I recommend that you read the php site..
Thanks!

One thing that I noticed though.
Cause in my database, I have a table called posts, and wihin it, every row starts with "post_", for example:

Code:
post_content
post_author
post_time
And when I use like this in the template file:
HTML Code:
Posted at {post.post_time}
..it didn't work

PHP Code:
foreach($posts as $post) {
                        
            
$tanaxia['template']->parse(
            
            array(
        
                
'post' => $post
                   
)
                   
            ));
            
        } 
$post will contain an array with all the data from the fetched rows.. so that should work :S

But it doesn't.

It works if I do like this:

PHP Code:
foreach($posts as $post) {
                        
            
$tanaxia['template']->parse(
            
            array(
        
                
'post' => array(
                    
                       
'posttime' => $post['post_time'],
                       
'postauthor' => $post['post_author'],
                       
'postcontent' => $post['post_content']
                   )
                   
            ));
            
        } 
HTML Code:
<center>

	<table width="900" border="1">

		<tr>

			<td colspan="3"><font size="2">Posted {post.posttime}</font></td>

		</tr>
			
		<tr>

			<td colspan="2" width="30%">By: {post.postauthor}</td>
			<td>{post.postcontent}</td>		

		</tr>
		
	</table>
	
</center>
Notice that I removed the _ from the template...
Any idea how to fix that ??
Tanax is offline  
Reply With Quote