Het idee was in eerste instantie dan ook om alleen variabelen in een .phtml bestand af te kunnen drukken, dit is inmiddels uitgebreid met een aantal functies.
Ik kom nu op een lastig punt waar ik al lang mee bezig ben en eigenlijk niet uit kom. Mijn wens is om een set resultaten af te kunnen drukken.
public function setLoop ($sKey, $aValues)
{
if (is_string($sKey) && is_array($aValues))
{
$this->aLoop[] = $sKey;
$this->aLoopVar[$sKey] = $aValues;
}
else
{
errors::setError('De eigenschappen voor de functie setLoop zijn niet correct.');
}
}
public function regexLoop ()
{
preg_replace_callback ('/(\[loop:(\w+)\])(.*?)(\[\/loop:(\w+)\])/i', array($this, 'replaceLoop'), $this->sContent);
}
public function replaceLoop ($matches)
{
if (in_array($matches[2], $this->aLoop))
{
$this->aLoopSubstr[$matches[2]] = $matches[3];
$this->sContent = str_replace ($matches[0], '<?php for ($i = 0; $i < count($this->aLoopVar[\'test\'][\'id\']); $i++)
{
print(preg_replace("/(\[loopvar:(.*?)\])/", $this->aLoopVar[\'test\'][\'id\'][$i], $this->aLoopSubstr[\'test\']));
}
?>', $this->sContent);
}
else
{
errors::setError('De loop: ' . $matches[2] . ' is niet bekend.');
}
}
test.php
<?php
error_reporting(E_ALL);
include ('../config.php');
include (FRAMEWORK . 'autoload.php');
// Verbinding maken
$sql = new MySQL('SQL.ini');
// Bestand inladen
$tpl = new parser(HTML . 'test.html');
// Vars, blocks, loops
$tpl->setVar('pagina_titel', 'Kluswerk.NL');
$tpl->setVar('url', ROOT);
$tpl->setBlock('menu');
$tpl->setVar('gebruiker', 'Bas');
$tpl->setVar('is_admin', true);
$aEen = array(1, 2, 3, 4 ,5, 6);
$aTwee = array('eerste titel', 'tweede titel', 'derde titel', 'vierde titel', 'vijfde titel', 'vierde titel', 'vijfde titel');
$tpl->setLoop('test', array('id' => $aEen, 'titel' => $aTwee));
$tpl->parse();
?>
test.html
<html>
<head>
<title>{{pagina_titel}}</title>
</head>
Welkom {{gebruiker}}, <br /><br />
[block:menu]Beginpagina | Over mij | Contact<br /><br />[/block:menu]
[if:istrue:is_admin]Bas je bent een admin!<br /><br />[/endif:istrue:is_admin]
[loop:test]De id: [loopvar:id] met de titel [loopvar:titel]!<br /><hr>[/loop:test]<br /><br />
[if:isset:foutieve_prijs]{{foutieve_prijs}}[/endif:isset:foutieve_prijs]
[block:footer]Copyright 2017.[/block:footer]
</html>
en het resultaat.
Welkom Bas,
Beginpagina | Over mij | Contact
Bas je bent een admin!
De id: 1 met de titel 1!
De id: 2 met de titel 2!
De id: 3 met de titel 3!
De id: 4 met de titel 4!
De id: 5 met de titel 5!
De id: 6 met de titel 6!
Ik heb al ontzettend veel manieren geprobeerd om de variabelen in de loop op te slaan etc. dat deze niet wordt overschreven, maar het is mij nog niet gelukt.
ik gebruik nu: $this->aLoopVar[\'test\'][\'id\'][$i] omdat er anders helemaal geen resultaat is.