Vanilla 1 is no longer supported or maintained. If you need a copy, you can get it here.
HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Vanilla's PHP Coding style

edited June 2008 in Vanilla 1.0 Help
Hi there,

Firstly, I'd like to say I'm really pleased to have found Vanilla. I've been a member on a forum that uses Vanilla for a while, and I didn't know it was available! So, that's great.

In general, the code looks excellent, and it's nice to see a good OOP approach (looking forward to complete documentation!).

However, I'm quite surprised (a bit shocked, actually!) at the way Vanilla mixes HTML within PHP, building up complex strings then echoing the results.
Can someone explain the reasoning behind not just escaping out of HTML templates using standard PHP tags?

For example:

VANILLA - HTML within a PHP string (inflexible):
if ($this->Context->Mode == MODE_DEBUG && $AllowDebugInfo) {
echo '<div class="DebugBar" id="DebugBar">
<b>Debug Options</b> | Resize: <a href="javascript:window.resizeTo(800,600);">800x600</a>, <a href="javascript:window.resizeTo(1024, 768);">1024x768</a> | <a href="'

NORMAL - PHP expressions escaped out of HTML (flexible):
<?php if ($this->Context->Mode == MODE_DEBUG && $AllowDebugInfo):?>
<div class="DebugBar" id="DebugBar">
<b>Debug Options</b> | Resize: <a href="javascript:window.resizeTo(800,600);">800x600</a>, <a href="javascript:window.resizeTo(1024, 768);">1024x768</a> | <a href="
<?php endif; ?>

It just seems like a really inflexible way to store markup, especially when the themes folder should allow you to just update the layout, and the styles folder allows you to style said markup. I don't understand why you would want to do this - it makes the code unreadable, and worse, really fragile.

Apologies if this question has been answered before.



  • Options
    I've found Vanilla to be really solid, with bugs rising only when many extensions are installed. You'll find that the only time HTML is mixed with PHP is within files located in the themes/ folder. There is nothing really "fragile" about it except that when working with strings in an editor you lose the syntax highlighting.

    It all comes down to programming style, Mark chose his own technique and stuck with it. The strongest point of any open-source application is consistency, and Vanilla certainly has that :)

    Some people will argue that Porting Vanilla to Smarty would make it easier for template designers, but I thought it would just add another layer of confusion, extra overhead, and not enough benefit.

    Mark asked for input on Naming Conventions for Vanilla 2.
  • Options
    edited June 2008
    Hi Matthew,

    That's a fair point about consistency.

    To clarify on fragility - once you're having to chop up strings with quotes, and then have to \"escape\" quotes inside of other quotes, code becomes (in my mind) "fragile".

    And as there's no way to have the parser properly tell you what you've done wrong with the code inside of the string, ie, you know the problem is the string, but WHICH bit of the string, it's another horrid level of debugging. Then you end up copying your new string back to a blank file, running that to get the parser to tell you where you missed a quote or such like, then back again to re-intergrate the change... I think that makes development fragile.

    And what when you update your template? You have to start the horrid process again. (I can now see why the forum IS so simple! To keep one's sanity)

    At the end of the day what Mark has achieved is quite a feat, and all credit to him - it's a truly, remarkable, well-thought-out and excellent job!

    I wouldn't argue for Smarty, nor would I argue for using a framework as someone else suggested in your second link.

    But I'm super-surprised that such an excellent engine doesn't take advantage of PHP's escaping in and out of HTML. That's one of [any server-side language's] core strengths?

    Anyway, as Mark mentioned in the Smarty post, it may go that way. Fingers crossed!


    PS. From doing a couple of tests, it looks like I may be able to get what I want anyway, by escaping, and the Vanilla classes will happily include.
  • Options
    You know, this style may have performance implications also--building a string by appending additional strings piece by piece forces PHP to reallocate memory for the string every time you append, which is pretty expensive.

    It may not make that much of a difference, but if its significant, then it should be something that is changed.
  • Options
    It certainly has inherent "human" performance implications! ;)
This discussion has been closed.