4images Forum & Community

4images Issues / Ausgaben => Discussion & Troubleshooting => Topic started by: troubadix on August 19, 2009, 04:59:24 PM

Title: smarty + 4images - proof-of-concept
Post by: troubadix on August 19, 2009, 04:59:24 PM
OK, damit der Wunschthread nicht zugepflastert wird, hier mal an zentraler Stelle ein "proof-of-concept" zum Thema 4images mit smarty.

Die folgenden Schritte wurden mit 4images 1.7.7 durchgeführt, funktionieren aber prinzipiell auch mit anderen Versionen.

1.) Smarty installieren
Die jeweils aktuelle Version gibts hier: http://smarty.net (http://smarty.net)
Einfach Zip-Archiv herunterladen und aus dem Archiv den Ordner "libs" nach "/4images/includes" entpacken.
Nun noch fix umbenennen von "libs" nach "smarty" -> Fertig!
Smarty befindet sich nun in "/4images/includes/smarty".

2.) Schalter einbauen
Damit wir (mehr oder weniger komfortabel) einstellen können, ob Smarty verwendet werden soll oder nicht, fügen wir in unser Template-Verzeichnis eine Datei "settings.php" ein, die genau eine Zeile enthält:
Code: [Select]
$smarty_active = true;
Nun ändern wir noch die "global.php".
Die Zeilen
Code: [Select]
include_once(ROOT_PATH.'includes/template.php');
$site_template = new Template(TEMPLATE_PATH);
ändern wir wie folgt:
Code: [Select]
if (file_exists(TEMPLATE_PATH.'/settings.php')) {
  include_once(TEMPLATE_PATH.'/settings.php');
} else {
  $smarty_active = false;
}
if ($smarty_active) {
  include_once(ROOT_PATH.'includes/smarty.class.php');
} else {
  include_once(ROOT_PATH.'includes/template.php');
}
$site_template = new Template(TEMPLATE_PATH);

3.) Smarty-Klasse
Die im Folgenden dargestellte Template-Klasse 'includes/smarty.class.php' stellt nur eine wirklich rudimetäre Implementierung dar und ist keineswegs komplett.
Sie sollte also auf keinen Fall in Produktivumgebungen eingesetzt werden.
 
Code: [Select]
<?php
if (!defined('ROOT_PATH')) {
  die(
"Security violation");
}

require_once 
'includes/smarty/Smarty.class.php';

class 
Template extends Smarty {

  var 
$template_extension 'html';
  var 
$start '{';
  var 
$end '}';
  
  function 
__construct($template_path "") {
    if (!@
is_dir($template_path)) {
      
$this->error("Couldn't open Template-Pack ".$template_path1);
    }
    
parent::__construct();
    
$this->template_dir $template_path;
    
$this->compile_dir  ROOT_PATH.'temp/compiled';
  }

  function 
register_vars($var_name$value "") {
    if (
is_array($var_name)) {
      foreach (
$var_name as $key=>$val) {
        
$this->assign($key$val);
      }
    } else {
      
$this->assign($var_name$value);
    }
  }

  function 
un_register_vars($var_list) {
    
$vars explode(","$var_list);
    foreach (
$vars as $val) {
      
$this->clear_assign($val);
    }
  }

  function 
parse_template($template) {
    
$template $template.'.'.$this->template_extension;
    return 
$this->fetch($template);
  }

  function 
parse_array($array) {
    return 
$array;
  }

  function 
print_template($template) {
    
$this->assign('body'$template);
    
$this->display('smarty.html');
  }
  
}

?>


4.) Templates anpassen
Das Anpassen der Template-Dateien ist die eigentliche Arbeit, auch wenn vieles mit Suchen+Ersetzen (z.B. mit PSPad) zu schaffen ist.
- aus "{if variable}" wird "{if $variable}"
- aus "{endif variable}" wird "{/if}"
- aus "{header}" wird {include file='header.html'}
- aus "{footer}" wird {include file='footer.html'}
- JavaScript-Bereiche werden in "{literal} ... {/literal}" eingeschlossen

5.) Status
Was haben wir bis jetzt gewonnen?
Noch nicht soooo viel - insbesondere für "parse_array()" und die parametrisierten Sprachkonstanten habe ich auf die Schnelle noch keine zufriedenstellende Lösung gefunden.

Ein paar schöne Dinge sind aber bereits möglich:
- Statt "{if variable}{endif variable}" können wir "{if $variable}{else}{/if}" einsetzen.
- Statt nur "if" können wir auch "{if $userlevel == 9}" verwenden
- Wir können mit "{include file='template.html'}" ganz einfach Dateien in ein Template einbinden.
- Die Smarty-Modifier funktionieren :-D
  "{$datumsvariable|date_format:'%d.%m.%Y'}"
  "{$smarty.now|date_format:'%d.%m.%Y %H:%M:%S'}" -> aktuelle Uhrzeit
  "{$textvariable|capitalize}"
  Das können auch PHP-Funktionen sein: "{$number|pow:2}"
- Durch einfügen von "{debug}" in ein Template erhalten wir ein Popup-Fenster mit allen Variablen.

Um die Fähigkeiten von Smarty jetzt wirklich ausschöpfen zu können, müssten allerdings tiefere Eingriffe in den Quelltext von 4images vorgenommen werden.
Statt z.B. jeden Kommentar einzeln mit einem Template ("comment_bit.html") zu verarbeiten, könnte man die komplette Liste der Kommentare als Array (oder Objekt) an Smarty übergeben und dann im Haupt-Template mit den Schleifen ("foreach" oder "section") arbeiten.
Sollte hierzu Informationsbedarf bestehen, können wir das gerne vertiefen.

6.) Links
Projektseite: http://smarty.net (http://smarty.net)
Cheat-Sheet: http://www.phpxperts.net/SmartyCheatSheetVersion2.pdf (http://www.phpxperts.net/SmartyCheatSheetVersion2.pdf)


*** EDIT ***
Was hier natürlich noch fehlte, war die neue Template-Datei "smarty.html":
Code: [Select]
{$body}

 :lol:
Title: Re: smarty + 4images - proof-of-concept
Post by: V@no on August 19, 2009, 05:05:45 PM
Can you provide benchmarks? how much smarty will affect on performance?
I'm still skeptical about smarty and not only because of it's performance but mainly because it's so complex that you'll need learn how to use it and what you learned can only be used in smarty environment.
Giving that and the fact that you can use PHP in 4images template engine, it would be much more practical learning a little PHP itself and use it in the templates...
Title: Re: smarty + 4images - proof-of-concept
Post by: Sunny C. on August 19, 2009, 05:20:07 PM
Post by troubadix in German.
translate by Google:

Quote
Incidentally, I've at times tried the Fast:
- smarty installiert -> 5 Minuten - Smarty installed -> 5 minutes
- Template-Klasse soweit aufgeräumt, dass alle Aufrufe einfach nur an smarty weitergereicht werden -> ca. 20 Minuten - Template-Class tidy extent that all views just to be passed smarty -> 20 minutes
- Templates (hauptsächlich per Grep-Search (PSPad)) geändert -> 20 Minuten - Templates (mainly by Grep search (PSPad)) changed -> 20 minutes
und siehe da: es funzt and lo and behold: it funzt Laughing
Einen eklatanten Performanceverlust konnte ich selbst in meiner ziemlich lahmen VM (völlig unoptimiert, ohne smarty-caching etc.) nicht feststellen. A blatant loss of performance, I could even pretty lame in my VM (unoptimiert completely without smarty-caching, etc.) can not be identified.

Dafür würde aber jetzt sowas hier funktionieren: That would be something but now here to work:
Title: Re: smarty + 4images - proof-of-concept
Post by: troubadix on August 20, 2009, 02:52:32 PM
Can you provide benchmarks? how much smarty will affect on performance?
I'm still skeptical about smarty and not only because of it's performance but mainly because it's so complex that you'll need learn how to use it and what you learned can only be used in smarty environment.
Giving that and the fact that you can use PHP in 4images template engine, it would be much more practical learning a little PHP itself and use it in the templates...

1.) Performance
OK, here are my stats, generated on my virtual-machine (Windows XP SP3, 1GB-Ram, XAMPP 1.6.3a)
Database contains 15.895 images with 167.471 comments but image-files are not present (test-environment with no media).

With smarty-rendering as figured in post #1 (modified template 'default_full', no smarty-caching):
#1: Page generated in 0.283400 seconds with 2 queries, spending 0.071000 seconds doing MySQL queries and 0.212400 doing PHP things. GZIP compression enable
#2: Page generated in 0.282538 seconds with 2 queries, spending 0.096000 seconds doing MySQL queries and 0.186538 doing PHP things. GZIP compression enabled
#3: Page generated in 0.310904 seconds with 2 queries, spending 0.128000 seconds doing MySQL queries and 0.182904 doing PHP things. GZIP compression enabled
#4: Page generated in 0.289150 seconds with 2 queries, spending 0.101000 seconds doing MySQL queries and 0.188150 doing PHP things. GZIP compression enabled
#5: Page generated in 0.271819 seconds with 2 queries, spending 0.094000 seconds doing MySQL queries and 0.177819 doing PHP things. GZIP compression enabled

Default-rendering with template 'default_full':
#1: Page generated in 0.302153 seconds with 2 queries, spending 0.108000 seconds doing MySQL queries and 0.194153 doing PHP things. GZIP compression enabled
#2: Page generated in 0.315841 seconds with 2 queries, spending 0.124000 seconds doing MySQL queries and 0.191841 doing PHP things. GZIP compression enabled
#3: Page generated in 0.321900 seconds with 2 queries, spending 0.106000 seconds doing MySQL queries and 0.215900 doing PHP things. GZIP compression enabled
#4: Page generated in 0.345551 seconds with 2 queries, spending 0.156000 seconds doing MySQL queries and 0.189551 doing PHP things. GZIP compression enabled
#5: Page generated in 0.255914 seconds with 2 queries, spending 0.089000 seconds doing MySQL queries and 0.166914 doing PHP things. GZIP compression enabled

As you can see, there is no significant difference between rendering with smarty or with default 4images-templating.

2.) Complexity
What's so complicated using smarty-syntax?
... and you're done - you are not forced to learn more smarty.

But even: you can - if you like (and ist's really not complicated, because smarty-syntax is very near at php-syntax):
etc, etc, etc, ...

3.) Conclusion (just my humble opinion)

think about it ... test it ...
...
and as i said in the wishlist-thread: It's not for the next update - but maybe (as an alternative) provided wiith 1.8 or 2.0


German
P.S.: Sollte jemand Probleme haben, den englischen Text zu verstehen, der melde sich bitte - ich werde dann nochmal übersetzen.
Title: Re: smarty + 4images - proof-of-concept
Post by: mawenzi on August 20, 2009, 03:33:00 PM
  • i think, a lot of 'hacks' and 'mods' for 4images can be made via smarty-plugins
    Example: if you need a value from the users-table thats not yet registered, you have to deal with the script itself to get your desired value.
    With smarty, you can write a little plugin that will get your desired value by quering the database itself.
    This will also make updating 4images very easy, because the 4images-php-files can stay original and no user has to alter the original files to get some extra functions


hi troubadix ...

... this is the first argument in the whole 4images / smarty discussion, where I would say : we should think about ...
... otherwise I can't see a significant advantage for a "php-smarty-html version" in relation to the existing "php-html version" ...
Title: Re: smarty + 4images - proof-of-concept
Post by: troubadix on August 22, 2009, 02:59:19 AM
... otherwise I can't see a significant advantage for a "php-smarty-html version" in relation to the existing "php-html version" ...

???????
... and where's the ultimate reason against smarty?

i only can see lots of benefits:

All my active websites are actually made with zikula (formerly postnuke).
They use their own smarty-implementation (pnRender) to render templates to html-output.
And i only can say: i love it - it's sooooooo easy to realize my design-ideas with only editing some templates - without any hack into code!

Title: Re: smarty + 4images - proof-of-concept
Post by: miealex on August 19, 2010, 08:45:18 PM
@troubadix: so if I would like to use smarties I would also have to modify the template ?