I may need to look at your code a little more to get a better idea on how you would do it in your specific instance, but the way this is done is generally to evaluate the template on each iteration of the loop. This is not PHP code that will be specific to your project, per se, so modify as you need.
// Get results from your database query. Rename your variables as needed.
while($querydata = $db->fetch_array($query)) {
$productID = (int) $querydata[‘id’];
$productName = htmlspecialchars($querydata[‘product_name’]);
$productDate = '('.my_date('F d, Y', $querydata['date']).')';
// Put any other variables here.
// Add a product and parse all of the above variables. Add it to {$projects} variable.
eval("\$products .= \"".$templates->get(“your_product_template”).”\”;”);
}
// Evaluate final template, and load the full projects list {$products}
eval("\$product_container = \"".$templates->get(“your_product_container_template).”\”;”);
// In further templates, use {$project_container} to display the full project list.
// The container is not technically necessary. You could use {$projects} instead, but using a container is good practice,
// as it allows you to put your product list within a <div> and add additional styling and structure.
When your plugin code looks like the above (which variables replaced with the ones corresponding to your database and code), you will need two templates. The first template will be a product template, which will be for each individual product. You can use your variables ($productID, $productName, $productDate) like normal in this template.
The second template is your product container template. In this template, you will
need to have a {$products} variable. This variable stores the full list of parsed products that were generated by your code. You do not need to use any of your other variables in this template. The full parsed list of products is already stored by the {$products} variable.
Last, to display the list of products, your page must have the template variable {$product_container} in place. If, for example, you are displaying these on the index page, you will need to modify your index template for your theme to have this variable.
Modify this code to serve your needs, but this should give you the general idea. This is how I’ve always approached this functionality in my plugins, so it has always worked well for me.
Regards,
-Darth Apple