SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 40
  1. #1
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Template if conditionals

    Ok well I'm starting to build the core of a CMS (custom one that I'll use to develop bespoke solutions for clients with quickly), and my database layer is done. Now I'm implementing a templating system. Templates are stored in the database.

    Simple use of eval() as you'd expect and placing $variables within the HTML code directly. It's all been done before by lots of people.

    However, I've been reading round quite a bit now about template if conditionals. I found two solutions here yesterday but neither support nested conditionals.

    http://www.sitepoint.com/forums/showthread.php?t=61633
    http://www.sitepoint.com/forums/showthread.php?t=140729

    I'm no expert by any means with regular expressions but I can see right away this is the only way to implement a solution. The only example I have actually seen of nested if conditionals working within HTML code is in vBulletin. They have two functions. One to check the actual condition tags are in the right format. And another to check they are nested correctly. Can anyone point me in the right direction of a solution (not a library that has to be compiled like PEAR), that may implement this.

    I'm not asking for someone to write some code here. Just trying to get a friendly pointer in the right direction of some existing code.

    Best Regards,
    - Dean

  2. #2
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is there a reason why you want to create you own template language? You could just use PHP and forget about things like this

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  3. #3
    SitePoint Wizard Dean C's Avatar
    Join Date
    Mar 2003
    Location
    England, UK
    Posts
    2,906
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you mean? I'm a big advocate of seperating all the HTML from my source code hence why I store all the HTML in the database and then parse the variables within the templates using eval within my PHP source. It's just useful to have if conditions within the templates. Like for example you may want to display things just to one person. Instead of writing in PHP:

    PHP Code:
    if($user == 'whoever')
    {
            
    $text 'omg';

    You could have within the template:
    Code:
    [if($user == 'whoever')]omg[/if]
    Kinda seems a better way IMO at least

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dean C
    Kinda seems a better way IMO at least
    Why? Why not use native PHP instead of creating and parsing your own template language? This is nothing to do with separating your application code from your HTML, just using PHP for presentational markup rather than something like your example

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you are doing templating, you should really use an existing solution, and evaluate if you need it. Any decent tempalting will use code generation (i.e. WACT, Smarty).

    Templates ideally should be easy for designers to use (i.e Dream/DumbWeaver friendly). The push for templates is if you have a very specialise skill division in your work environment (i.e. HTML layout geezers, Photoshop designer dudes, Programmers, Application Architects / Project managers). The other push for templates is when you deal with more advanced stuff (for instance requirement to use composite views possibly, and I we have uses such as for various XHTML / CSS tricks which would be messy in just pure PHP).

    To make a proper template system is a real skill. It takes a lot of time to do and you should weight this up against the advantage it gives, and wether you are able to produce something that really is better than existing template solutions. Remember that PHP is of close relation to JSP, Java's templating technology.

  6. #6
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    Why? Why not use native PHP instead of creating and parsing your own template language?
    Designers often like to use WYSIWYG editors and don't speak the foreign language which is php code. The following, I think, is easier to explain than php. Rather than a new language with obscure syntax errors, it's basically just using xml to structure a document - something they ought to be familiar with.

    Also, they don't have to run the page through a local/live server to view the end result. The designer can choose colours (account_balances tag), add some dummy content to pad out the layout (text in "jabber" tags will be discarded later), and php does the rest.

    Code:
    <account_balances=#f00|#000>
        <p style="#000">$balance</p>
    </account_balances>
    <jabber>
        <p style="#f00">2,000</p>
        <p style="#000">1,000</p>
        <p style="#f00">1,300</p>
    </jabber>
    PHP Code:
    <?php
        
    foreach($accounts as $account)
        {
            if(
    $account->isOverdrawn())
            {
                 echo 
    '<p style="#f00">' $account->getBalance() . '</p>';

            } else {

                 echo 
    '<p style="#000">' $account->getBalance '</p>'
            
    {
        }
    ?>
    How many designers would spot the errors in the php? And even if you do know php it's a nicer way to work, IMO.

  7. #7
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I agrees McGruff. PHP looks very messy as a template language as it is imperative by nature and therefore doesn't fit in with the way designers often think, espceially when HTML is declarative.

    However depending on the project, it is worth using PHP. The main thing to realise is to keep Presenation PHP files away from business logic. Templates do help achieve this disapline

  8. #8
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by McGruff
    How many designers would spot the errors in the php? And even if you do know php it's a nicer way to work, IMO.
    Or you could just write clearer PHP:

    PHP Code:
    <?php foreach ($accounts as $account): ?>
       
          <?php if ($account->isOverdrawn()):  ?>
       
             <p class="warning"><?php echo $account->getBalance() ?></p>
       
          <?php else: ?>
       
             <p><?php echo $account->getBalance() ?></p>
       
          <?php endif; ?>
       
     <?php endforeach; ?>
    Hand your designers some info on the PHP Control Structures and what data they can access on each template, and you're away.

    Here is a PHP templating engine:

    PHP Code:
    class template {
          function 
    phrase($data$template) {
                 
    ob_start();
                 
    extract($dataEXTR_SKIP);
                 include(
    $template);
                 return 
    ob_get_flush();
          }

    $data is an associative array of the data for the template, and $template is the URL of the template file. You could store this URL in the database instead of the whole template, the database would like you more

    Obviously there are lots of ways of expanding this, the most obvious would be to have the class handle the selection of the template given the user's selected theme. That's code you'd have to write anyway, but which you could write now rather than trying to re-solve "nested conditionals" and things like that.

    hth,
    Douglas

    PS: I'm not even sure how your example is supposed to work.

    Code:
    <account_balances=#f00|#000>
        <p style="#000">$balance</p>
    </account_balances>
    It isn't XML, <p style="#000"> isn't HTML, and there is no obvious relationship between #f00 being "overdrawn" and #000 being "OK", let alone how these interact with the <p style="#000"> to give one or the other, and then right in the middle there is something which looks like a PHP variable! What is going on??
    Hello World

  9. #9
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MiiJaySung
    as it is imperative by nature and therefore doesn't fit in with the way designers often think, espceially when HTML is declarative.
    Do you have any data on this or did it come out of thin air?

    One of the best known template engines, Smarty, looks quite imperative to me: from http://smarty.php.net/manual/en/lang...unction.if.php

    Code:
    {if $name eq "Fred"}
          Welcome Sir.
    {elseif $name eq "Wilma"}
          Welcome Ma'am.
    {else}
          Welcome, whatever you are.
    {/if}
    If it is declarative you want, why not just use XSL?

    If you want PHP-but-not-really-PHP, go on and use Smarty.

    For the rest, PHP is just fine, especially if you use the endif; syntax.

    Douglas
    Hello World

  10. #10
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use PHP as templates, but limit omyself on what to do. I use iterators so all loops have the same syntax. Furhter more, I dont worry about permissions or stuff in templates - only loops and basic IF statements. I also use the alternate syntax; endif; and endwhile; . I always thought this was easy to understand as opposed to the made up syntax. Also, theres no performance / frustration overhead as opposed to parsing your own syntax.

    The lack of DW support is an issue I guess though.

  11. #11
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    HTML is declaritive in any regards; the markup declares content and structure, doesn't it? XSL on the other hand is completely different, and in no way related to a declaritive language such as HTML

    I also would have to disagree with your view, ie

    For the rest, PHP is just fine, especially if you use the endif; syntax.
    The use of PHP at any level within a template file is dangerous. It's all fair enough to educate a web designer in the simplicity of PHP control functions, but there are any number of things that can and do go wrong.

    The danger is that a portion of PHP gets deleted or modified without either the web designer realising it, or understand the implications, the end result is of course, a broken page, possible giving errors of file locations, etc - a security risk yes?

    As for the use of Smarty, if this is an option for you, then great but Smarty is it's self not without it's own faults either in my view.

  12. #12
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not to mention the endif; style is about as ugly and as hard to follow as you can possible imagine... just my opinion of course.

  13. #13
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    Not to mention the endif; style is about as ugly and as hard to follow as you can possible imagine... just my opinion of course.
    It's not too bad if you dont nest, or at most nest one level. Its no worse then {loop params} {endloop}

  14. #14
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Super Phil
    It's not too bad if you dont nest, or at most nest one level. Its no worse then {loop params} {endloop}
    Yeah, it's nesting that's a problem. The "endif;" part seems unconvential to me too...

  15. #15
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    The danger is that a portion of PHP gets deleted or modified without either the web designer realising it, or understand the implications, the end result is of course, a broken page.
    A template designer can always write bad templates, the same as a PHP programmer can always write bad PHP. Errors in the HTML are just as likely to lead to broken pages as errors in the template language. The answer is testing, which applies just as much to the templates as to the application code.

    Does this example fit into your definition of "declaritive"?

    Code:
    <xsl:template match="title">
        <h1><xs:value-of /></h1>
    </xsl:template>
    Douglas
    Hello World

  16. #16
    SitePoint Zealot
    Join Date
    Oct 2004
    Location
    naperville
    Posts
    189
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    Yeah, it's nesting that's a problem. The "endif;" part seems unconvential to me too...
    I just dont get overboard. Keep it simple and its not a problem.

  17. #17
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by someonewhois
    The "endif;" part seems unconvential to me too...
    Well, it isn't C syntax

    You see "end if" in the more basic programming languages, and that's really the "request" you see for a templating language: keep it basic, so for me it seems quite natural

    Deep nesting is ugly with curly braces too!

    Douglas
    Hello World

  18. #18
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    Well, it isn't C syntax

    You see "end if" in the more basic programming languages, and that's really the "request" you see for a templating language: keep it basic, so for me it seems quite natural

    Deep nesting is ugly with curly braces too!

    Douglas
    Curly brackets look awesome nested

  19. #19
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A template designer can always write bad templates, the same as a PHP programmer can always write bad PHP.
    Sure, you have a point in this, but what I was thinking as well is that you have a tool, such as Dreamweaver for example (there are others as well though) that can be the cause?

    What happens when the tool doesn't recognise the PHP syntax for example... There is just too much that can go wrong

  20. #20
    SitePoint Addict mgkimsal's Avatar
    Join Date
    Sep 1999
    Posts
    209
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Sure, you have a point in this, but what I was thinking as well is that you have a tool, such as Dreamweaver for example (there are others as well though) that can be the cause?

    What happens when the tool doesn't recognise the PHP syntax for example... There is just too much that can go wrong

    The *cause* of the problem of broken pages isn't really the particular tool used, but the process by which that makes it into a 'live' environment. Without some defined processes for backups, development ,QA/testing and approval, you're at risk for broken stuff regardless of what approach is taken.

    Sure, it may seem like overkill for 'small' projects or one man shops (seemed like it to me 9 years ago when I was starting out with this stuff!), but it's definitely not. Any project you're working on where problems would arise by a 'designer' using dreamweaver and putting up something on a server that could cause damage to a project resulting in money/reputation loss is a project worth having procedures for backups/testing.

    So, perhaps that's getting a bit off topic. Back to the earlier posters - I agree that simply using PHP as the 'template language' is fine in most cases. One of the issues I rarely hear brought up in favor of 'template languages' is security. By giving the full PHP language to a 'designer' to use, you run the risk of them inserting code they shouldn't - not to break things but to perhaps mail sensitive data off site to someone they shouldn't, or to poke around in the database (they'd have passwords, after all). But even this issue gets back to review/testing/approval steps, and if you're just blindly trusting people to put code on live servers without reviewing it, you're going to have these problems whether you're using something like Smarty or not.
    Michael Kimsal
    =============================
    groovymag.com - for groovy/grails developers
    jsmag.com - for javascript developers

  21. #21
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    Do you have any data on this or did it come out of thin air?

    One of the best known template engines, Smarty, looks quite imperative to me: from http://smarty.php.net/manual/en/lang...unction.if.php

    Code:
    {if $name eq "Fred"}
          Welcome Sir.
    {elseif $name eq "Wilma"}
          Welcome Ma'am.
    {else}
          Welcome, whatever you are.
    {/if}
    If it is declarative you want, why not just use XSL?

    If you want PHP-but-not-really-PHP, go on and use Smarty.

    For the rest, PHP is just fine, especially if you use the endif; syntax.

    Douglas

    Well the point is, XSL is fine for people who have both web design and coding experience, such as myself. However, most designers don't have proper programming experience. XSL is an implementation of the Transform View. Designers don't think like programmers and therefore don't see operarations as a sequence of fine grained logic operations. Something like ASP / WACT's tempalte view is a much better solution imo. It gives you the declearative XML syntax that most XML / WYSIWYG (DreamWeaver) HTML Editors can easily understand. Likewise most template operations are reoccuring scenarios (for instance we Fathner Image replacement). Having tags for this in a tempalte system is a logical way forward.

    XSL though more declearative than Smarty, still has the odd imperative element to it, such as if / else / for each clauses. The WACT list system to me seem one of the more elegant ways around the this.

    Smarty is not a good choice. It does little or no abstraction. In fact to me Smarty just makes the whole situation more complex as it is PHP is a more stupid and usless syntax.

    Likewise another reason why a template language should be used is that raw access to PHP means a designer can make "hacks" due to the programmer failing to design his code properly and therefore do things that should be in the model or controller part of an application such as operate on a database. This is what mgkimsal clearly pointed out.

  22. #22
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    ... what I was thinking as well is that you have a tool, such as Dreamweaver for example (there are others as well though) that can be the cause?

    What happens when the tool doesn't recognise the PHP syntax for example...
    In design view Dreamweaver will show a little icon to show that that part of the page contains PHP code. What do you think DW will do with code like McGruff posted, guess it's some sort of template language? It will mark it as invalid (X)HTML

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  23. #23
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, I know Dreamweaver shows an icon Sean

    If you can personally live with embedded PHP then there is no problem I suppose, but I just get a bad feeling about it, that's all

  24. #24
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Yes, I know Dreamweaver shows an icon Sean

    If you can personally live with embedded PHP then there is no problem I suppose, but I just get a bad feeling about it, that's all
    Yeah, but the arguments for "The designer is going to screw up the code" apply for:

    Code:
      {if $name eq "Fred"}
           Welcome Sir.
     {elseif $name eq "Wilma"}
           Welcome Ma'am.
     {else}
           Welcome, whatever you are.
     {/if}
    As well. It's all the same. That's just an ugly version of PHP, that confuses Dreamweaver.

  25. #25
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by seanf
    What do you think DW will do with code like McGruff posted, guess it's some sort of template language? It will mark it as invalid (X)HTML
    Not on my PC. Dreamweaver simply ignores it - which is the whole point. Designers can work away same as usual then a compiler script can read the file and write a new, compiled template where php code is substituted for custom-tagged sections of html.

    I think it's a nicer way to work even if you do know how to program and it should be much easier for designers who don't.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •