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):

  1. Download von http://smarty.php.net
  2. Entpacken des Smarty Ordners nach C:/xampp/libs/
  3. Anlage des Ordners „Smarty“ in htdocs, mit zwei Unterordnern „configs“ und „templates“
  4. Anlage des Ordners „Smarty“ in c:/xampp, mit zwei Unterordnern „cache“ und „templates_c“
  5. Anlage der Datei index.tpl in templates, mit folgendem Inhalt:
    <html>
    <body>
    Hello, {$name}!
    </body>
    </html>
  6. 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');
    ?>
  7. 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.

  1. 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/>
  2. 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');
    ?>

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website