Groups | Search | Server Info | Login | Register


Groups > de.comp.lang.php > #5003

Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern

From Andreas Borutta <borumat@gmx.de>
Newsgroups de.comp.lang.php
Subject Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern
Date 2025-12-04 14:21 +0100
Organization A noiseless patient Spider
Message-ID <re4vzdpu91ji.dlg@borumat.de> (permalink)
References (3 earlier) <1t6909e0efi34eabbn3e8%sfroehli@Froehlich.Priv.at> <tdnp2t2a2dux$.dlg@borumat.de> <2gsf4lr3fd6.dlg@borumat.de> <14269q1gcv61z$.dlg@borumat.de> <10gbrbh$29n7o$1@dont-email.me>

Show all headers | View raw


Arno Welzel:

Erstmal herzlichen Dank! : )

>> Ich habe das per AI erzeugte Plugin für Kirby in Form von PHP-Code
>> nochmal ändern lassen.
>> 
>> Mich reizte es, die Optionen in einer separaten Konfigurations-Datei
>> "tidy.ini" setzen zu können.
>> 
>> /site/plugins/tidy-html/index.php:
>> 
>> <?php
> 
> Hier fehlt die Angabe des Namespace (siehe auch
> <https://www.php.net/manual/de/language.namespaces.definition.php>).

Auch die offiziellen Plugins von Kirby haben keinen solchen Eintrag.

>> class TidyTemplate extends Template
>> {
>>     public function render(array $data = []): string
>>     {
>>         $kirby = App::instance();
>>         $html = parent::render($data);
>>         
>>         if (
>>             $kirby->option('debug') === true &&
>>             class_exists('tidy')
>>         ) {
> 
> Ich nehme an, es ist beabsichtigt, tidy nur zu nutzen, wenn man den
> Debug-Modus in Kirby aktiviert?

Nein. Das hatte ich übersehen. Den Modus habe ich jetzt deaktiviert
und die Zeile im Plugin entfernt.

> 
>>             $tidy = new tidy();
>>             $tidy->parseString($html, $config, 'utf8');
>>             $tidy->cleanRepair();
>>             return (string) $tidy;
> 
> Ist das wirklich so vorgesehen? Ich hätte eher sowas erwartet:
> 
>             return $tidy->html()->value;

Ich erhalte dazu:

| Beides funktioniert, aber es gibt Unterschiede:
| 
| (string)$tidy - Ruft die __toString() Methode auf, die standardmäßig das vollständige HTML-Dokument zurückgibt
| $tidy->html()->value - Gibt explizit nur den Inhalt des <html>-Tags zurück
| 
| Empfehlung: Verwende (string)$tidy, da:
| 
| Es idiomatischer für die Tidy-Extension ist
| Du das komplette Dokument inkl. DOCTYPE haben möchtest
| Es kürzer und lesbarer ist



| <?php
| use Kirby\Cms\App;
| use Kirby\Template\Template;
| 
| class TidyTemplate extends Template {
|     public function render(array $data = []): string {
|         $html = parent::render($data);
|         
|         // Prüfe ob Tidy verfügbar ist
|         if (!class_exists('tidy')) {
|             return $html;
|         }
|         
|         // Lade Config
|         $iniFile = __DIR__ . '/tidy.ini';
|         $config = file_exists($iniFile) 
|             ? parse_ini_file($iniFile, false, INI_SCANNER_TYPED) 
|             : [];
|         
|         // Fallback auf Default-Config wenn Parse fehlschlägt
|         if ($config === false) {
|             $config = [
|                 'indent' => true,
|                 'wrap' => 0,
|                 'output-html' => true,
|             ];
|         }
|         
|         try {
|             $tidy = new tidy();
|             $tidy->parseString($html, $config, 'utf8');
|             $tidy->cleanRepair();
|             
|             // Fehlerbehandlung
|             if ($tidy->errorBuffer) {
|                 // Optional: Logge Tidy-Warnungen
|                 // error_log('Tidy warnings: ' . $tidy->errorBuffer);
|             }
|             
|             return (string)$tidy;
|         } catch (\Exception $e) {
|             // Bei Fehler: Original-HTML zurückgeben
|             return $html;
|         }
|     }
| }
| 
| Kirby::plugin('andreas/html-tidy', [
|     'components' => [
|         'template' => function (App $kirby, string $name, string $contentType = null) {
|             return new TidyTemplate($name, $contentType);
|         }
|     ]
| ]);


Mich wundert es, dass überhaupt derart länglicher Code für das
Anwenden einer PHP-Erweiterung, die typischerweise auf ein ganzes
Dokument wirken soll, nötig ist.


Andreas
-- 
http://fahrradzukunft.de

Back to de.comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

"Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-04 00:36 +0100
  Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-04 07:57 +0000
    Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-04 09:17 +0100
      Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Claus Reibenstein <creibens@gmail.com> - 2025-11-04 11:33 +0100
      Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-04 11:26 +0000
        Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-04 20:36 +0100
          Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-19 20:17 +0100
            Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-19 21:24 +0100
              Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-20 01:16 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-21 17:59 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Markus Heinz <markus.heinz@uni-dortmund.de> - 2025-11-22 01:12 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-26 14:21 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-27 08:42 +0100
            Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-28 09:35 +0100
              Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Arno Welzel <usenet@arnowelzel.de> - 2025-11-28 10:51 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-12-04 14:21 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Arno Welzel <usenet@arnowelzel.de> - 2025-12-07 15:43 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-12-07 20:09 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-12-07 19:36 +0000
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-12-07 21:55 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-12-08 09:07 +0000
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-12-11 14:07 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-12-11 18:45 +0000
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Arno Welzel <usenet@arnowelzel.de> - 2025-12-15 12:23 +0100
    Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-08 07:20 +0100
      Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-08 10:05 +0000
        Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-08 18:10 +0100
          Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-08 19:50 +0000
        Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Arno Welzel <usenet@arnowelzel.de> - 2025-11-10 01:58 +0100
          Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-10 09:52 +0100
          Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Andreas Borutta <borumat@gmx.de> - 2025-11-10 09:53 +0100
          Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-10 09:20 +0000
            Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Arno Welzel <usenet@arnowelzel.de> - 2025-11-10 11:12 +0100
              Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-10 11:09 +0000
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Arno Welzel <usenet@arnowelzel.de> - 2025-11-10 17:51 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-11 08:09 +0000
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Arno Welzel <usenet@arnowelzel.de> - 2025-11-11 10:16 +0100
                Re: "Prettify" (Umbrüche, Leerzeichen) vor dem Ausliefern Stefan+Usenet@Froehlich.Priv.at (Stefan Froehlich) - 2025-11-11 10:03 +0000

csiph-web