How can PHP efficiently loop through and render database content in a template using Smarty?
To efficiently loop through and render database content in a template using Smarty, you can fetch the data from the database using PHP, assign it to a Smarty variable, and then loop through the data in the Smarty template using a foreach loop.
// Fetch data from the database
$data = $db->query("SELECT * FROM table");
// Assign data to Smarty variable
$smarty->assign('data', $data);
// Display data in Smarty template
$smarty->display('template.tpl');
```
In your Smarty template (template.tpl), you can then loop through the data using a foreach loop like this:
```html
{foreach $data as $row}
<p>{$row.name}</p>
{/foreach}
Keywords
Related Questions
- What best practices should be followed when using absolute and relative paths for file inclusions in PHP code?
- What are some potential pitfalls when using the DOMDocument class in PHP for parsing XML feeds like in the provided code snippet?
- How can PHP functions be utilized effectively to organize and output data in a structured manner for HTML presentation?