SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    London
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Find and replace for template method

    I'm trying to write a php function that can parse php variables through any html template path passed to the function.

    So, here's the idea:

    I have a template file here:

    templates/mytemplate.html

    All of my php variables use the following naming convention: $my_lowercase_variable

    In the html template the above example variable value should replace the following: {MY_LOWERCASE_VARIABLE} which can occur multiple times.

    In a php script I have a number of php variables. I wish to pass them through the html template and store the resulting html in a php variable.

    Any help appriciated

  2. #2
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This should do the trick
    PHP Code:
    $html preg_replace(
        
    '/\{(\w+)\}/e'
        
    '${strtolower("$1")}'
        
    file_get_contents($template)
    ); 

  3. #3
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    London
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply. I've tried to implement your code but the following errors is appearing:

    Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING

    Any ideas?

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    London
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, it did work, I just had to remove some of the spaces after copy/pasting your code.

    Thanks v much

  5. #5
    SitePoint Enthusiast
    Join Date
    May 2002
    Location
    London
    Posts
    98
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I should properly test before responding...

    The code is removing any trace of {A_VARIABLE} from the template but it's not replacing it with the variable value specified before making the function call.

  6. #6
    SitePoint Zealot mrWoot's Avatar
    Join Date
    Jan 2004
    Location
    Wisconsin
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't know how big your project is, but you might want to look into already made templating systems. Why reinvent the wheel, I say.

    Here's is a good list: http://www.sitepoint.com/forums/show...hreadid=123769

  7. #7
    SitePoint Wizard silver trophy
    Join Date
    Mar 2006
    Posts
    6,132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    turn error reporting up when writing code or debugging.
    PHP Code:
    error_reporting(E_ALL); 
    http://www.php.net/manual/en/languag...bles.scope.php

    those replacement variables arent defined in the scope that you are trying to use them in.

  8. #8
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by webkuh
    I should properly test before responding...

    The code is removing any trace of {A_VARIABLE} from the template but it's not replacing it with the variable value specified before making the function call.
    The code I posted replaces variables defined in current context ("scope"). If vars are global, preg_replace() should be also called global.
    PHP Code:
    $TPL "
        Dear {POSTER}.<br>
        Does the code {ACTION} for you?<br>
        Regards, <br>
        {CODER}
    "
    ;

    $poster "webkuh";
    $action "work";
    $coder "stereo";

    $html preg_replace(
        
    '/\{(\w+)\}/e',
        
    '${strtolower("$1")}',
        
    $TPL
    ); 

    echo 
    $html
    When you introduce a new scope (i.e. write a function), this won't work due to php scoping rules (global variables are not visible in a function). You need to modify replacement expression:

    PHP Code:
    function expand_template($template) {
        return 
    preg_replace(
            
    '/\{(\w+)\}/e',
            
    '$GLOBALS[strtolower("$1")]',
            
    $template
        
    ); 
    }

    echo 
    expand_template($TPL); 
    This assumes variables are in GLOBAL scope. If this is not the case, you have to pass variables as an associative array to the templating function:

    PHP Code:
    function expand_template_2($template$vars) {
        return 
    preg_replace(
            
    '/\{(\w+)\}/e',
            
    '$vars[strtolower("$1")]',
            
    $template
        
    ); 
    }

    function 
    foo() {
        
    $TPL "Example of {LOCAL_VAR} and {OTHER_LOCAL}";
        
    $local_var 'foo';
        
    $other_local 'bar';
        echo 
    expand_template_2(
            
    $TPL
            
    compact('local_var','other_local'));
    }

    foo(); 
    I hope this helps you further.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •