Author Topic: smarty + 4images - proof-of-concept  (Read 10760 times)

0 Members and 1 Guest are viewing this topic.

Offline troubadix

  • Pre-Newbie
  • Posts: 8
    • View Profile
    • OASIS-SVN
smarty + 4images - proof-of-concept
« 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
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
Cheat-Sheet: http://www.phpxperts.net/SmartyCheatSheetVersion2.pdf


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

 :lol:
« Last Edit: August 24, 2009, 08:24:31 AM by troubadix »
adopt, adapt, improve!
Bernd
---
Access goes Subversion: OASIS-SVN
Access-Projekte mit SVN verwalten ... und mehr ...

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: smarty + 4images - proof-of-concept
« Reply #1 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...
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Sunny C.

  • Addicted member
  • ******
  • Posts: 1.805
  • I ♥ 4I
    • View Profile
Re: smarty + 4images - proof-of-concept
« Reply #2 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:

Offline troubadix

  • Pre-Newbie
  • Posts: 8
    • View Profile
    • OASIS-SVN
Re: smarty + 4images - proof-of-concept
« Reply #3 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?
  • just replace "{variable}" with "{$variable}" to get the content of a variable
  • just replace "{if variable}...{endif variable}" with "{if $variable} ... {/if}" to test conditions
... 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):
  • you can use "{else}"
  • you can use modifiers to format a variables content
  • you can use loops in your template
etc, etc, etc, ...

3.) Conclusion (just my humble opinion)
  • with smarty, you can completely separate code from presentation. The script-developers don't have to handle with html and design-questions.
  • the 4images developers do not have to spent time for templating - others will do (and provide updates etc.).
  • 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


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.
« Last Edit: August 20, 2009, 03:20:26 PM by troubadix »
adopt, adapt, improve!
Bernd
---
Access goes Subversion: OASIS-SVN
Access-Projekte mit SVN verwalten ... und mehr ...

Offline mawenzi

  • 4images Moderator
  • 4images Guru
  • *****
  • Posts: 4.500
    • View Profile
Re: smarty + 4images - proof-of-concept
« Reply #4 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" ...
Your first three "must do" before you ask a question ! ( © by V@no )
- please read the Forum Rules ...
- please study the FAQ ...
- please try to Search for your answer ...

You are on search for top 4images MOD's ?
- then please search here ... Mawenzi's Top 100+ MOD List (unsorted sorted) ...

Offline troubadix

  • Pre-Newbie
  • Posts: 8
    • View Profile
    • OASIS-SVN
Re: smarty + 4images - proof-of-concept
« Reply #5 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:
  • first of all: no loose of performance!!!
  • keep the code clean - really no html in the php-scripts
  • reducing amount of templates: why use two templates (xyz_bit + xyz) when you can do it (very clean) with only one
  • lots of very usable modifiers and functions - and if the desired one ist not available: write a few lines of code and make a new one
  • extend 4images with smarty-functions - instead of 'hacking' into code

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!

adopt, adapt, improve!
Bernd
---
Access goes Subversion: OASIS-SVN
Access-Projekte mit SVN verwalten ... und mehr ...

Offline miealex

  • Pre-Newbie
  • Posts: 1
    • View Profile
Re: smarty + 4images - proof-of-concept
« Reply #6 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 ?