Page 1 of 1

Going all Zend

Posted: Wed Sep 03, 2008 7:50 am
by shiznatix
Alrighty. I have my companies website. When I first took this job the website was garbage 9000. I quickly moved it to my own framework and got something decent going. Shortly after that, my boss was like "but if you die or quit or whatever, nobody will be able to pick up on your framework very quickly" which is a valid argument. So I moved to Zend with a few custom things thrown in most notably the usage of PHPtal as the templating engine. This is causing things to get a bit messy, especially with translations.

I have been thinking, maybe I should move over to an "All Zend" situation. The problem with this is that I really don't like the way it handles form input and its messy way of adding things in. Basically when someone submits a form I want a different class to be called, not the view class. I want an Action class to be called which processes the data then goes to the view. Zend does not seam to do this and expects form handling to be done in the view class. Also, I want template variable retrieval to be somewhat centralized because I often want the same variables across many views, so why should I code them separately? I can get around all this my own way but it gets sloppy around Zend.

This is all being said about a rather old version of Zend. I have not upgraded in like a year and don't know if the problems I face have been solved. Also, I don't know if there was already solutions to these problems because I can't really find help or even helpful documentation that addresses these issues.

Last, the all Zend thing seams to favor throwing PHP into templates. I am not totally against this though, the only problem being for the designer that we have at my company, I think he might mind. Plus, its always a good idea to keep the 2 separate. But, on the other hand that might clear up some of my messy view problems.

I am currently looking into symfony, but I don't know so much about this whole symfony from the command line thing.

So, from what I have said about problems I have been having with Zend and everything, what do you guys think? Would an all Zend approach with a lot of reading up on it do the trick or should I look more into symfony/cake? Why do I hate every framework?

Re: Going all Zend

Posted: Mon Sep 08, 2008 1:03 am
by supermike
I believe in rolling my own framework. That way, I know everything works just the way I want it and I don't have to fight with shoddy docs or beg people in chat rooms and forums for the answers, and that's especially true with Zend. Here's a quick and dirty template class that replaces Smarty for me.

Code: Select all

<?php

class Template {
 
const ENCODE_DEFAULT = 0;
const ENCODE_HTML = 1;
const ENCODE_URL = 2;

public static function Assign($sVarName, $sVal, $nType = self::ENCODE_DEFAULT) {
    switch ($nType) {
        case self::ENCODE_HTML: // show the html
            $sVal = htmlentities($sVal);
            break;
        case self::ENCODE_URL: // prepare for urls
            $sVal = urlencode($sVal);
            break;
        default: // 0, default, as is, unaltered
            break;
        
    }
    $sVarName = strtoupper($sVarName);
    $oVar = array( $sVarName => $sVal );
    $oAssigned = & $GLOBALS['ASSIGNED'];
    if (empty($oAssigned)) {
        $oAssigned = array();
    }
    array_push($oAssigned, $oVar);
}
 
public static function Display($sTemplateFile) {
    $oAssigned = & $GLOBALS['ASSIGNED'];
    if (!empty($oAssigned)) {
        foreach($oAssigned as $oArray) {
            foreach($oArray as $sVarName => $sVal) {
                $sVal = addslashes($sVal);
                $sVal = str_replace("\'","'",$sVal);
                eval("\$$sVarName = \"$sVal\";");
            }
        }
    }
    require_once('templates/' . $sTemplateFile);
}
 
} //end class
 
 
/*
USAGE:
Template::Assign('NAME', $sFullName);
Template::Display('thome.php');
 
Inside templates/thome.php, I put:
 
<?php ?>
html goes here
Name: <?= $NAME ?>
 
*/

Re: Going all Zend

Posted: Mon Sep 08, 2008 3:30 pm
by alex.barylski
I am not totally against this though, the only problem being for the designer that we have at my company, I think he might mind.
I was of this mind set and researched/implemented a crude template system which attempted to remove PHP completely from templates using a pattern recognition approach. The problem with this approach is that sometimes conditional logic is just needed.

For example consider a recordset displayed in a table like so:

Code: Select all

<table>  <tr>    <td>First Name</td>    <td>Last Name</td>    <td>Email</td>  </tr>  <tr>    <td>George</td>    <td>Simpson</td>    <td>george@email.com</td>  </tr>  <tr>    <td>Sam</td>    <td>Steele</td>    <td>sam@email.com</td>  </tr></table>
The above is a nice consistent data representation so my idea for a nice PHP-less template system basically involved parsing the template like so and using a pattern recognition type approach:

Code: Select all

<table>  <tr>    <td>First Name</td>    <td>Last Name</td>    <td>Email</td>  </tr>  <!-- name="start:blah" action="repeat" data="someobject" -->  <tr>    <td>%FNAME%</td>    <td>%LNAME%</td>    <td>%EMAIL%</td>  </tr>  <!-- name="end:blah" action="repeat" data="someobject" --> </table>
Basically the idea was to make the template avoid any Smarty style syntaxes or similar constructs which basically would cause the template to break inside a WYSIWYG.

The syntax above is not accurate to what I eventually implemented but the idea is the same. Makes sure the template can be safely edited in a WYSIWYG. The problem is (some issues I managed to work around -- such as alternative row colors, etc) but in situations where precise control is required my template system choked.

Consider this:

Code: Select all

<table>  <tr>    <td>First Name</td>    <td>Last Name</td>    <td>Email</td>  </tr>  <tr>    <td>George</td>    <td>Simpson</td>    <td>george@email.com</td>  </tr>  <tr>    <td colspan="3">      These are extended details for George Simpson displayed inline when George is selected as opposed to showing these details on a separate page. Can be avoided by designing the details on a separate page but not all clients like that so this template system proved inadequate.    </td>  </tr>  <tr>    <td>Sam</td>    <td>Steele</td>    <td>sam@email.com</td>  </tr></table>
Ultimately I decided that a designer would just hand off the template/design to me and I would then integrate the required PHP code into the template myself.

Basically the design will give me the table as he saved in DreamWeaver and I would take that design and add the PHP associate with dataset and render the exact display dynamically. I found this approach to be more efficient both in redering times and in workflow.

Translations are a PITA there are various approaches but the best I have found is to use your native language in a wrapper function or use PHP's _() function see below:

Code: Select all

<?php echo _('This is english text that is easily translated into Spanich or whatever'); ?>

Re: Going all Zend

Posted: Mon Sep 08, 2008 11:01 pm
by supermike
You are exactly right about stuff breaking in a WYSIWYG template editor when we use Smarty, XLST, or even PHP Alternative Syntax type encoding, or something like that. There's just no easy way around it, and this problem is not just with PHP, but every other language and framework out there. So, my fix was to break up the layout of the page into gadgets. So, instead of iterating a recordset inside the templating engine, I draw the HTML in code as I iterate the recordset, and dump that out as something like <?= $RECORDS ?>. By doing this, I make the WYSIWYG editor load my page a lot nicer. And, at that, I try to keep the HTML minimal in my non-template files, keep it predictable, and style it with CSS.

Re: Going all Zend

Posted: Tue Sep 09, 2008 5:47 am
by panic!
I like codeigniter.:)