Skip to content

Instantly share code, notes, and snippets.

@konstantinos-tsatsarounos
Last active November 21, 2020 10:17
Show Gist options
  • Select an option

  • Save konstantinos-tsatsarounos/321b19d4523c331f903429e378f55cf0 to your computer and use it in GitHub Desktop.

Select an option

Save konstantinos-tsatsarounos/321b19d4523c331f903429e378f55cf0 to your computer and use it in GitHub Desktop.
Simple template loop with PHP
<?php
/**
* My home file
*/
$test_data = [
[
'title' => 'hello world',
'paragraphs' => [
'Candy canes gingerbread soufflé. Danish powder donut marshmallow candy cheesecake. Jelly beans cake dessert apple pie lemon drops. Jujubes sugar plum danish ice cream. Dessert soufflé oat cake. Caramels bonbon marzipan fruitcake gummies toffee. Liquorice chocolate cake sweet roll danish brownie pie icing gingerbread. Powder tootsie roll liquorice caramels brownie pudding cake. Icing gummi bears powder tootsie roll icing. Soufflé muffin cake. Gummies chocolate cake jelly beans. Croissant wafer fruitcake tootsie roll biscuit.',
'Marshmallow liquorice wafer wafer jujubes. Chocolate cake cotton candy caramels bonbon cupcake soufflé jelly beans cake. Cheesecake pudding croissant toffee sweet roll. Toffee liquorice danish topping lemon drops dragée caramels cake toffee. Cookie sweet roll pastry sugar plum jelly beans. Pie macaroon cotton candy jujubes dessert topping dessert soufflé chupa chups. Apple pie lollipop bear claw tart cookie sesame snaps jelly beans. Pudding oat cake topping. Donut fruitcake jelly-o pastry chocolate bar cookie biscuit. Marzipan cotton candy biscuit. Lemon drops danish liquorice bear claw. Toffee croissant gummi bears tart caramels. Bonbon gummies halvah pudding soufflé dessert soufflé pudding carrot cake. Topping brownie dessert tart sugar plum pudding jujubes.'
]
],
[
'title' => 'hello world',
'paragraphs' => [
'Lorizzle owned dolizzle sit go to hizzle, consectetizzle adipiscing elit. Nullam i saw beyonces tizzles and my pizzle went crizzle crazy, izzle volutpat, suscipit quizzle, its fo rizzle own yo, arcu. Pellentesque eget tortor. Sizzle eros. The bizzle own yo ass dapibizzle turpis tempizzle tempizzle. Mauris shit nibh funky fresh turpizzle. Pimpin izzle tortor. Pellentesque rizzle shiznit nisi. In ass go to hizzle crunk dictumst. Bling bling dapibizzle. Fizzle yippiyo urna, shit, mattizzle ac, eleifend vitae, nunc. Gangster gangster. Integizzle sempizzle fo shizzle my nizzle sizzle shizznit.'
]
]
];
function loadTemplateContent($template_name, $params = []){
$output = '';
if(\file_exists( $template = __DIR__ . "/$template_name.template.php")){
ob_start();
extract($params);
include $template;
$output = ob_get_contents();
ob_end_clean();
}
return $output;
}
$result = [];
foreach ($test_data as $index => $article){
array_push($result, loadTemplateContent('my-html', $article));
}
echo implode("\n\n", $result);
<?php
/**
* Template: My cool example
*/
/**
* @var $title string,
* @var $paragraphs string[]
*/
?>
<article>
<header>
<h1><?php echo $title ?></h1>
</header>
<section class="content">
<?php foreach ($paragraphs as $paragraph): ?>
<?php echo $paragraph; ?>
<?php endforeach; ?>
</section>
</article>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment