LaTeX mit PHP und Smarty erzeugen
In einem früheren Artikel habe ich beschrieben, wie man mit Python und der Template-Engine Cheetah LaTeX Dokumente erzeugt. Heute die grundlegenden Schritte, um mit PHP und der Template-Engine Smarty LaTeX Dokumente zu erzeugen. Ich folge dabei mehr oder weniger der Anleitung von http://smarty.incutio.com/?page=SmartyInstallationWindows.
Installation (unter der Windows Installation von Xampp):
- Download von http://smarty.php.net
- Entpacken des Smarty Ordners nach C:/xampp/libs/
- Anlage des Ordners „Smarty“ in htdocs, mit zwei Unterordnern „configs“ und „templates“
- Anlage des Ordners „Smarty“ in c:/xampp, mit zwei Unterordnern „cache“ und „templates_c“
- Anlage der Datei index.tpl in templates, mit folgendem Inhalt:
<html> <body> Hello, {$name}! </body> </html>
- im htdocs Verzeichnis Anlage der Datei index.php, die folgendes enthält:
<?php define('SMARTY_DIR', 'C:/xampp/libs/Smarty-3.1.11/libs/'); require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty; $smarty->template_dir = 'C:/xampp/htdocs/smarty/templates'; $smarty->config_dir = ' C:/xampp/htdocs/smarty/config'; $smarty->cache_dir = 'C:/xampp/smarty/cache'; $smarty->compile_dir = 'C:/xampp/smarty/templates_c'; $smarty->assign('name','Uwe'); $smarty->display('index.tpl'); ?>
- jetzt die index.php im Webbrowser aufrufen
Damit ist die initiale Konfiguration von Smarty erledigt, jetzt müssen wir ihm die richtige Behandlung von LaTeX Code beibringen. Da Smarty zum Escapen geschweifte Klammern nutzt, müssen diese umdefiniert werden.
- Anlage der Datei tex1.tpl im templates Verzeichnis
\documentclass[12pt,ngerman]{scrartcl}<br/> <br/> \author{<@$author@>}<br/> \title{<@$title@>}<br/> <br/> \begin{document}<br/> \maketitle<br/> <br/> \end{document}<br/>
- Anlage der tex1.php in htdocs:
<?php define('SMARTY_DIR', 'C:/xampp/libs/Smarty-3.1.11/libs/'); require_once(SMARTY_DIR . 'Smarty.class.php'); $smarty = new Smarty; $smarty->template_dir = 'C:/xampp/htdocs/smarty/templates'; $smarty->config_dir = ' C:/xampp/htdocs/smarty/config'; $smarty->cache_dir = 'C:/xampp/smarty/cache'; $smarty->compile_dir = 'C:/xampp/smarty/templates_c'; $smarty->caching =false; //disable caching $smarty->left_delimiter = '<@'; $smarty->right_delimiter = '@>'; $smarty->assign ('author', 'Uwe Ziegenhagen'); $smarty->assign ('title', 'Hello Smarty!'); $smarty->display ('tex1.tpl'); ?>