MyBB Community Forums

Full Version: Using pre_output_page to replace in place
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

Working on the CMS plugin and having some trouble with the blocks.

I am using [ba]block_area_name[/b] in the template, this then triggers a function that looks for any blocks that are assigned to that block area.

I was hoping that I could then simply replace the [ba]block_area_name[/b] with the ordered includes of the block index files.

This kind of works, however instead of replacing in place, it is replacing at the start of the page.

I am guessing that pre_output_page is precisely that, output before the page content.

Here is my code, can anyone offer any suggestions?

function cms_block_caller(&$page)	{

	global $db, $config;
	
	$pattern= "/\[ba\](.*?)\[\/ba\]/is";	
	//the variable for the replace
	preg_match_all($pattern, $page, $matches);

	foreach($matches[1] as $match)	{

		//We need to get the ID of the block area for later use. We could do this with a join, but the performance value isn't there as it should be an inexpensive query
		$query = $db->query("Select ID from ".TABLE_PREFIX."brookie_cms_general_data where type='blockarea' and title='".$match."'");
		$block_area_id = $db->fetch_field($query, "ID");

		if($block_area_id)	{
			//get all of the blocks for the block are, in order and replace them.
			$block_query = $db->query("select block_title, block_folder from ".TABLE_PREFIX."brookie_cms_blocks where area=".$block_area_id);

			while($result=$db->fetch_array($block_query))	{
				$page = str_replace('[ba]'.$match.'[/ba]', include("cms/blocks/".$result["block_folder"]."/index.php"), $page);
			}
		}

	}

UPDATE: seems it is with the include, for some reason it wont do an include replace in place.

UPDATE 2: using file_get_contents() seems to be the best solution
you can not use functions in the str_replace. you need to use preg_replace with the 'e' modifier.

also, improve your performance by joining your two queries to get one results set and then iterate over that. Save a query.
I will likely improve it before release, this was a proof of concept function for me.

file_get_contents works also. I am going to look at performance of that vs preg_replace.

Also a join query in this circumstance is unlikely to make any real performance change, the join may actually be slightly less efficient. Less queries doesn't always mean better performance.

I do plan on writing the join and benchmarking soon though

Oh and thank you as well

Just realised another flaw in my logic that I didn't think about.

If I do a replace in the while loop, it will only ever work for the first block Smile

I'll have to build an array and use preg_replace as you suggest

My function is now much more compact, and uses just a single query with a subquery to get the ID rather than a join.

i.e. where ID=(select ID from.......)

I am still trying to get the call and replace right, because I need to make it an array. Im considering a hacky workaround for the first release though