![]() |
preg_match and that stuff..
Hey!
I don't know barely anything about this, and I want to create a simple template system where I would replace variables. What I would want is like this: HTML Code:
Hello and welcome {user.username}!So HTML Code:
{user.lastvisit}PHP Code:
PHP Code:
|
I tried it a couple of different ways but this seems to work fine:
Template.php PHP Code:
Code:
Hello and welcome {user.username}!PHP Code:
|
Quote:
Also one thing, does this also work with variables? Like if I do: Code:
You are currently on page {page}PHP Code:
THanks once again :) |
Quote:
It gets all the data out of the array so if you would like to replace {page} with the contents of $page you'll have to do something like this: PHP Code:
|
Quote:
Oh this is awesome ;) Thanks a bunch! Edit: Can you explain this line? PHP Code:
|
Quote:
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:
I recommend that you read the php site.. |
Quote:
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_contentHTML Code:
Posted at {post.post_time}PHP Code:
But it doesn't. It works if I do like this: PHP Code:
HTML Code:
<center>Any idea how to fix that ?? :-/:-/ |
Change this line:
PHP Code:
PHP Code:
|
Quote:
|
| All times are GMT. The time now is 10:54 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0