MyBB Community Forums

Full Version: Parse array in template?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi.

I am working on a custom function for my MyBB site.
I got the PHP/MySQL part ready and now need to implement with MyBB design.

I have created a custom php file, ex:

<?php
define("IN_MYBB", 1);
$templatelist = "test";
require_once './global.php';
 
add_breadcrumb("Test", "test.php");

$arrayTest = array();
for($i = 0; $i > 10; $i++)
{
	$arrayTest[] = $i;
}

eval("\$test = \"".$templates->get("test")."\";");
output_page($test);

?>


And I have created a new template page.

Lets say I wanna output $arrayTest - how to do that in the template?

Do I have to implode to array into a string and then output the string with, ex: {$string} ?


Best regards

MyBB by default does not allow you to use php in the templates.(Stupid I know).

In order to do so you need a plugin,which I have lost the link too.
Please read my post again?!...

global $arrayTest;

I think he has a plugin written already, and wants to display the output. In this case, yes, you would need to format the data with HTML or as plain text in order to display, or access each array element individually, in the template.
Yes the data in the array will need to be formatted before being displayed.

$arrayTestDisplay = '';
foreach ($arrayTest AS $key => $value)
{
    $arrayTestDisplay .= $value;
    // OR to have some formatting:
    $arrayTestDisplay .= "<div class='test'>".$value."</div>";
    // OR for more complex formatting or to use the template system to format the output
    eval("\$arrayTestDisplay .= \"".$templates->get("array_test")."\";"); // You'll need to add this template
}

eval("\$test = \"".$templates->get("test")."\";"); 
output_page($test);

Then just use {$arrayTestDisplay} in the test template where you want to show the array