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)

By Toledoh - August 29, 2013

That works great Greg - and yes, it's dynamic content.

However, I've got multiple placeholders in a single section... ie.

Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

#content-1#

Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.

#content-2#

Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

Can I do that?

Cheers,

Tim (toledoh.com.au)

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)