Insert specific content.

5 posts by 2 authors in: Forums > CMS Builder
Last Post: August 30, 2013   (RSS)

By Toledoh - August 29, 2013

Hi Guys,

I know this has been asked (and answered) before, but I can't find the answer.

I have a number of files that I want to include from within a WYSIWYG field...  ie.  I want to be able to have something like ###insertOne### and have this display as <?php include("insertOne.php"); ?>

How do I achieve that?

Cheers,

Tim (toledoh.com.au)

Hi Tim,

Does your insertOne.php file contain dynamic PHP content, or is it just html code? If its just html code the easiest way to swap out the placeholder is like this:

  $filterOn = '#insert_content#';

  //Get the new content to display from a file
  $new_content = file_get_contents('test.php'); 

  //Add new content to content variable
  $blogRecord['content'] = str_replace($filterOn, $new_content, $blogRecord['content']);

So the placeholder in this example is called #insert_content#, the content of test.php is stored as a string in the variable new_content. Then str_replace is used to swap the placeholder with the string of content from test.php. 

If you have PHP code in insertOne that needs to be run on the page, I would recommend using this method instead:

  $filterOn = '#insert_content#';

  //Check if the string contains the filterOn placeholder, if it does store the num where it starts in a variable
  if($startNum = strrpos( $blogRecord['content'] , $filterOn)){
    
    //Display the first half of our content string before the placeholder
    echo substr($blogRecord['content'],0,$startNum);
    
    //Include our content from test.php
    include('test.php');

    //Display the second half of the content after the placeholder. Add the length of the placeholder to ensure the placeholder isn't displayed. 
    echo substr($blogRecord['content'],$startNum+(strlen($filterOn)));
  }else{
    
    //If the string doesn't contain our placeholder, display it like normal.
    echo $blogRecord['content'];
  }

So the above system first checks if the blog content contains the filter on variable, and number of digits into the string where it starts. It then displays the first half on the string before the placeholder. Then the content of test.php is included and then finally the second half of the content is displayed.

All of the above is just example code, so you might have to make a few changes to get it working with your pages.

Let me know if you have any questions. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

Hi Tim,

Here is a much simpler solution that allows you to insert several placeholders:

  // load records from 'blog'
  list($blogRecords, $blogMetaData) = getRecords(array(
    'tableName'   => 'blog',
    'where'       => "num = '1'",
    'loadUploads' => true,
    'allowSearch' => false,
    'limit'       => '1'
  ));
  $blogRecord = $blogRecords[0];


  //Create an array of placeholders to file names
  $insertArray = array(
    "#insert_content#"  => 'test.php',
    "#insert_content2#" => 'test2.php'
  );

  //Set content that will contains placeholders to a varaible
  $content = $blogRecord['content'];
  
  //Cycle through placeholders
  foreach($insertArray as $placeholder => $file){
    //If the content contains the placeholder....
    if($startNum = strrpos($content , $placeholder)){
      //Create and object and include the file to 'run' the code
      ob_start();
      include($file);

      //Add objects content to a placeholder content variable
      $placeholderContent = ob_get_clean();

      //Replace placeholder with replace holder content
      $content = str_replace($placeholder, $placeholderContent, $content);
    }
  }

  //Display completed content
  echo $content;

So the process is fairly similar to my first post, but instead we're running through an array of placeholders and files, and then using the output buffering feature to run the code in the include files and store it in a string, then replace that code with a placeholder.

Let me know if you have any questions. 

Thanks!

Greg

Greg Thomas







PHP Programmer - interactivetools.com

By Toledoh - August 30, 2013

Wow - that's really cool.  It gives a whole new level of flexibility!

Cheers,

Tim (toledoh.com.au)