SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast rgf207's Avatar
    Join Date
    Feb 2006
    Location
    Maryland
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    anyone know how to call a class from within an outputted string?

    I have a template file that i one long html string. I need to replace a certain part of that string with a call to a class and have it parsed as php code. I can't, for the life of me figure out how to do this. I've tried eval() but it does not seem to work

    Thanks

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    You need to do what? Can you please explain, what do you mean by 'call to a class'?
    Saul

  3. #3
    SitePoint Enthusiast rgf207's Avatar
    Join Date
    Feb 2006
    Location
    Maryland
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    You need to do what? Can you please explain, what do you mean by 'call to a class'?
    sure.

    my template contains code like this

    Code:
    <html>
    ......
    <div>
    {PLUGIN_1_CONTENT}
    </div>
    ......
    <div>
    {PLUGIN_N_CONTENT}
    </div>
    I need to replace
    Code:
    {PLUGIN_1_CONTENT]
    with

    PHP Code:
    $pnew = new plugin_1($classvars); 
    and have it execute

    Hope that helps

  4. #4
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Ohhhh,

    Well, probably something like this:
    Code php:
    $tpl=file_get_contents('tpl.html');
     
    preg_match_all('/\{([0-9a-z_]+)_CONTENT\}/i',$tpl,$matches);
     
    foreach($matches[1] as $name){
      $o=new $name($classvars);
      $plugin_content=$o->run();
      $tpl=str_replace($name.'_CONTENT',$plugin_content,$tpl);
    }
     
    echo $tpl;

    Not sure where should $classvars come from, though.
    Saul

  5. #5
    SitePoint Enthusiast rgf207's Avatar
    Join Date
    Feb 2006
    Location
    Maryland
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by php_daemon View Post
    Ohhhh,

    Well, probably something like this:
    Code php:
    $tpl=file_get_contents('tpl.html');
     
    preg_match_all('/\{([0-9a-z_]+)_CONTENT\}/i',$tpl,$matches);
     
    foreach($matches[1] as $name){
      $o=new $name($classvars);
      $plugin_content=$o->run();
      $tpl=str_replace($name.'_CONTENT',$plugin_content,$tpl);
    }
     
    echo $tpl;

    Not sure where should $classvars come from, though.
    Thanks for the code.

    I'll give this a try

    $classvars comes from the current class. I'm passing it to the new one.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    try preg_replace_callback

    PHP Code:
    echo preg_replace_callback('/{(.+?)_CONTENT}/''plugin_content'$template);

    function 
    plugin_content($matches) {
       
    $plugin $matches[1];
       
    $obj = new $plugin;
       return 
    $obj->generate_output();


  7. #7
    SitePoint Enthusiast rgf207's Avatar
    Join Date
    Feb 2006
    Location
    Maryland
    Posts
    96
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help php_daemon. that worked great.


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
  •