Sto usando PHP come motore di template per questo suggerimento: https://stackoverflow.com/a/17870094/2081511
Io ho:
$title = "My Title";
ob_start();
include("page/to/template.php");
$page = ob_get_clean();
E sulla pagina / to / template.php ho:
<?php
echo <<<EOF
<!doctype html>
<html>
<title>{$title}</title>
...
EOF;
?>
Sto cercando di rimuovere parte della sintassi richiestadalle pagine del modello per rendere più facile per gli altri sviluppare i propri modelli. Quello che mi piacerebbe fare è mantenere la convenzione di denominazione delle variabili di {$ variabile}, ma rimuovere queste righe dal file di modello:
<?php
echo <<<EOF
EOF;
?>
Stavo pensando di metterli su entrambi i lati dell'istruzione include, ma poi mi mostrerebbe semplicemente quella frase come testo invece di includerla.
risposte:
0 per risposta № 1Beh, se vuoi una soluzione di template MOLTO semplice, questo potrebbe aiutare
<?php
$title = "My Title";
// Instead of including, we fetch the contents of the template file.
$contents = file_get_contents("template.php");
// Clone it, as we"ll work on it.
$compiled = $contents;
// We want to pluck out all the variable names and discard the braces
preg_match_all("/{$(w+)}/", $contents, $matches);
// Loop through all the matches and see if there is a variable set with that name. If so, simply replace the match with the variable value.
foreach ($matches[0] as $index => $tag) {
if (isset(${$matches[1][$index]})) {
$compiled = str_replace($tag, ${$matches[1][$index]}, $compiled);
}
}
echo $compiled;
Il file di modello sarebbe simile a questo
<html> <body> {$title} </body> </html>