TplBlock/sample/sample.php

76 lines
1.7 KiB
PHP
Raw Normal View History

2018-03-19 22:32:58 +01:00
<?php
/**
* Gniearks TplBlock sample.
*
* PHP version 5
*
* @category Template
* @package TplBlock
* @author gnieark <gnieark@tinad.fr>
* @license GNU General Public License V3
* @link https://github.com/gnieark/tplBlock/
*/
require_once __DIR__."/../autoload.php";
2018-03-19 22:32:58 +01:00
const PRIMES = [ 1, 2, 3, 5, 7, 11 ];
2018-03-19 22:32:58 +01:00
/**
* Find divisors of a number.
*
* It works as long as the number is less than the last PRIMES number squared.
*
* @param int $number The number to find divisors for.
*
* @return array An array of divisors.
*/
function findDivisors(int $number)
{
$divisors = [];
$index = 1;
while ($number > 1 and $index < count(PRIMES)) {
if ($number % PRIMES[$index] != 0) {
$index++;
continue;
}
$number = $number / PRIMES[$index];
$divisors[] = PRIMES[$index];
}
2018-03-19 22:32:58 +01:00
return $divisors;
}
2018-03-19 22:32:58 +01:00
$variables = [
"name" => "Gnieark",
"title" => "Monsieur",
"firstname" => "Grouik",
];
2018-03-19 22:32:58 +01:00
// Simples vars.
$template = (new TplBlock())->addVars($variables);
2018-03-19 22:32:58 +01:00
// A sub bloc.
foreach (PRIMES as $prime) {
$template->addSubBlock(
(new TplBlock("primes"))->addVars([ "number" => $prime ])
);
2018-03-19 22:32:58 +01:00
}
// Find highest number for which we can find divisors.
$lastNumber = pow(PRIMES[count(PRIMES) - 1], 2);
// Test sub - sub blocs.
for ($i = 2; $i <= $lastNumber; $i++) {
$templateNumber = (new TplBlock("number"))->addVars([ "value" => $i ]);
2018-03-19 22:32:58 +01:00
foreach (findDivisors($i) as $divisor) {
$templateNumber->addSubBlock(
(new TplBlock("divisor"))->addVars([ "value" => $divisor ])
);
}
$template->addSubBlock($templateNumber);
}
2018-03-19 22:32:58 +01:00
echo $template->applyTplFile("tpl.txt");