Results 1 to 5 of 5

Thread: turn variable in eregi_replace string into incl file

  1. #1
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default turn variable in eregi_replace string into incl file

    I've figured out another way to add modules to a CMS system, but I would like to have the possibility of adding additonal features to the template system such as included files.

    The physical "theme.php" page uses <% main %> and the index page builds the template like this.

    Code:
    $template = eregi_replace("<% main %>", "$main", $template);
    I would like to be able to turn $main into an included file, and I searched and found the following...

    PHP Code:
    <?php
    ob_start
    (); # start buffer
    include_once( 'path/to/included/file/file.php' );
    # we pass the output to a variable
    $main ob_get_contents();
    ob_end_clean(); # end buffer
    # and here's our variable filled up with the html
    echo $main;

    //....code to start to prepare the template
    $template eregi_replace("<% main %>""$main"$template);
    //...code to finish the template

    ?>
    The issue is that I can't use echo $main; like above because it prints out at that exact position above the template. Anyone have an idea on how I can turn the $main variable in the eregi_replace string into the included page so <% main %> in the "template" file is replaced with the included file?

    Thank you

  2. #2
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I don't understand... You mean like this?
    PHP Code:
    <?php
    ob_start
    (); # start buffer
    include_once( 'path/to/included/file/file.php' );
    # we pass the output to a variable
    $main ob_get_contents();
    ob_end_clean(); # end buffer

    //....code to start to prepare the template
    $template eregi_replace("<% main %>""$main"$template);
    //...code to finish the template

    ?>
    BTW, if file.php returns its output rather than echoing it, you can do this:
    PHP Code:
    $main = include('path/to/included/file/file.php');
    $template eregi_replace("<% main %>""$main"$template); 
    Last edited by Jesdisciple; 10-15-2008 at 10:24 PM.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  3. #3
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Thank you for your reply. I ran some tests since I first saw your post and did get this to work.

    From what I can tell this CMS is no longer available, but the template system may have been a little difficult to work with for others also. I found somewhere that someone had changed the template system with the following and it seemed to work a little better.

    Seperate template class file is included and the template is now structured like...(class changes first part to <% main %> so it works in the template.
    PHP Code:
    $template->__replace("main",$main); 
    Using the new template system I tried the following...
    This worked to include the file, but for whatever reason even though it was right above the $template line like in your example it didn't place it at the <% main %> location in the template.
    PHP Code:
    $main = include('path/to/included/file/file.php'); 
    When I used this it worked to add that content into the specific spot in the template.
    PHP Code:
    ob_start(); # start buffer
    include_once( 'path/to/included/file/file.php' );
    # we pass the output to a variable
    $main ob_get_contents();
    ob_end_clean(); # end buffer 
    I was hoping to just be able to include files like normal into the template because this is all on a php server and all pages are php extension including the "template" file. I guess it didn't work that way since it works on this "replace" type template system where everything needs to be defined.

  4. #4
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    I take it you fixed everything?

    To clarify, the difference between these two patterns is in the included file:
    1. PHP Code:
      <?php
      ob_start
      ();
      include(
      'path/to/included/file.php');
      $contents ob_get_contents();
      ob_end_clean();
      ?>
    2. PHP Code:
      <?php
      $contents 
      = include('path/to/included/file.php');
      ?>
    Here are the corresponding include files:
    1. PHP Code:
      <?php
      echo 'Put ';
      echo 
      'your ';
      echo 
      'contents ';
      echo 
      'here.';
      ?>
    2. PHP Code:
      <?php
      $output 
      'Put ';
      $output .= 'your ';
      $output .= 'contents ';
      $output .= 'here.';
      return 
      output;
      ?>
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

  5. #5
    Join Date
    Jul 2008
    Posts
    138
    Thanks
    13
    Thanked 1 Time in 1 Post

    Default

    Yes, I did get it to work. I appreciate your clarification on these 2 types of outputs, and so I could see it firsthand I tested the code you provided.

    I had to resort to this because I believe the way the template is compiled I was unable to successfully include php files directly into the template in the standard way like below.
    PHP Code:
    <?php include('file.php'); ?>

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
  •