phpscriptcoder Posted June 17, 2008 Share Posted June 17, 2008 Hi, I'm trying to make a simple templating system for use in another script. I want to have a feature to include another template file, but am not sure how. This is what I am trying to use ($template is the contents of the template file). $template = preg_replace('/\{\{([^\}]+)\}\}/', file_get_contents('\\\0'), $template); It tries to include the file \\\0 rather than the correct filename. This is supposed to match a string like {{header.html}} and replace it with the contents of the file within the braces. I have tried some different methods, but none of them work. Thanks. Quote Link to comment Share on other sites More sharing options...
thebadbad Posted June 17, 2008 Share Posted June 17, 2008 Try using preg_replace_callback() if you want a function to be run before replacing content. preg_replace() takes strings as parameters - file_get_contents('\\\0') is not a string. Also, shouldn't you use "\\1" instead of "\\\0"? <?php function evaluate($matches) { return file_get_contents($matches[1]); } $template = preg_replace_callback('/\{\{([^\}]+)\}\}/', 'evaluate', $template); ?> Quote Link to comment Share on other sites More sharing options...
effigy Posted June 17, 2008 Share Posted June 17, 2008 Use the /e modifier and quote your function, e.g.: <pre> <?php echo $alpha = join('', range('A', 'Z')); echo '<br>'; echo preg_replace('/[^AEIOU]/e', 'strtolower("\0")', $alpha); ?> </pre> Quote Link to comment Share on other sites More sharing options...
phpscriptcoder Posted June 17, 2008 Author Share Posted June 17, 2008 Try using preg_replace_callback() if you want a function to be run before replacing content. preg_replace() takes strings as parameters - file_get_contents('\\\0') is not a string. Also, shouldn't you use "\\1" instead of "\\\0"? <?php function evaluate($matches) { return file_get_contents($matches[1]); } $template = preg_replace_callback('/\{\{([^\}]+)\}\}/', 'evaluate', $template); ?> Thanks, that worked. Quote Link to comment Share on other sites More sharing options...
phpscriptcoder Posted June 17, 2008 Author Share Posted June 17, 2008 Is it possible to pass additional arguments to the callback function? My code for the templater so far: <?php function process_include($matches) { return templater(file_get_contents($matches[1]), $vars); } function templater($file, $vars) { $template = file_get_contents(templates.'/'.$file); foreach($vars as $k => $v) { $template = str_replace('[$'.$k.']', $v, $template); } $template = preg_replace_callback('/\{\{([^\}]+)\}\}/', 'process_include', $template); echo $template; } ?> I am calling the templater function recursively so the additional template files will be processed as well. $vars is set in the file calling the templater, and I need to pass it every time the templater is called recursively. Any help? Quote Link to comment Share on other sites More sharing options...
phpscriptcoder Posted June 17, 2008 Author Share Posted June 17, 2008 Probably not the best way but I changed the process_include function to use the variable as a global. Quote Link to comment Share on other sites More sharing options...
effigy Posted June 17, 2008 Share Posted June 17, 2008 Is it possible to pass additional arguments to the callback function? I don't think so. Would create_function work, as shown in Example #1? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.