0

I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.

Example: Index Page:

<?php include('templates/123/index.php'); ?>

templates/123/index.php page

<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>

I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.

I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)

kdjernigan
  • 417
  • 2
  • 7
  • 22

2 Answers2

1

My preferred method of doing stuff like this involves starting my code with:

ob_start(function($c) {
    $replacements = array(
        "username"=>"Kolink",
        "rank"=>"Awesome"
    );
    return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
        return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
    },$c);
});
Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • I deleted my last comments because this actually worked. I mistyped the variable. Now my only question, how would I make it work so that it replaced 1) text (like it does now) and 2) lines of php code (`"username"=> `) – kdjernigan Apr 20 '13 at 05:58
  • You can't do that, since this is run after the code is finished and just before sending it to the browser. You could, however, put an anonymous function as one of the replacements, then add a check to see if the replacement is a string or a callable, and act accordingly. – Niet the Dark Absol Apr 20 '13 at 14:34
  • how would I do this? How can I replace {loginplaceholder} with $loginplaceholder variable? – kdjernigan Apr 22 '13 at 17:45
  • You could add the entry `"loginplaceholder"=>$GLOBALS['loginplaceholder']`, although that's not really recommended since `$GLOBALS` can be expensive. Alternatively, try adding `global $loginplaceholder` at the start of the function, then use `"loginplaceholder"=>$loginplaceholder`. Final and best alternative, you can start with `ob_start(function($c) use (&$loginplaceholder) {`, then add `"loginplaceholder"=>$loginplaceholder` to the array without any further hacks. – Niet the Dark Absol Apr 22 '13 at 17:48
  • AWESOME! It all worked..thanks...giving the answer to you. Thanks for all the help! – kdjernigan Apr 22 '13 at 17:51
  • I'm trying to find out if the code can be modified to detect letters and numbers separately within the same {string}...such as {blog_5} where it would call the blog and pull 5 latest posts. I know how to make it work to pull the blog, just don't know how to make it separate the letters from numbers and only work independantly and not interfere with another one (ex: {forum_3} on the same page ) ...I hope this makes sense. – kdjernigan Apr 25 '13 at 22:21
0

Two steps I suggest

  1. Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
  2. use strtr() function to replace your placeholder i.e {username} with actual values

I think this will server your needs.

Community
  • 1
  • 1
Ankit
  • 1,867
  • 2
  • 21
  • 40