SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 62

Thread: templated if's

  1. #1
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    templated if's

    I have a pretty basic (but powerful enough) template class which I use, the only thing that would make it easier for me (and other people who have to edit the front end code) would be templated <if> statements.

    ie <if="foo==1">Foo is 1!</if>

    What are some of the methods that you use for this sort of thing? I have a working example, it just uses a preg_replace_all and a series of if statements...

    Now, I _know_ it works, but this code just seems a bit dirty for some reason anyone got a superior method? (ignore the fact I'm only using the == operator at the moment for testing!)

    PHP Code:
    preg_match_all('/<if="(.*)">(.*)<\/if>/i'$string$out);
                
                
    //print_r($out);
                
                
    if(count($out[1]) > 0)
                {
                    for(
    $i=0$i count($out[1]); $i++)
                    {
                        if(
    substr_count($out[1][$i], '=='))
                        {
                            
    $parts explode('=='$out[1][$i]);

                            if(
    $this->known_markers[$parts[0]] == $parts[1])
                                
    $string str_replace($out[0][$i], $out[2][$i], $string);
                            else
                                
    $string str_replace($out[0][$i], ''$string);
                        }
                        else
                            
    $string str_replace($out[0][$i], ''$string);
                    }
                } 

  2. #2
    SitePoint Member
    Join Date
    Oct 2005
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    is something like this what you're looking for?


    PHP Code:
    <?php if ($a == 5): ?>
    A is equal to 5
    <?php endif; ?>

  3. #3
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no - not really, in the templates we use it should just be plain HTML for the sake of making things easy.

    <if></if> would be a 'custom' html tag (in a way) which is interpreted by the template class.

  4. #4
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    <if="foo==1">
    That's not even valid xml. Perhaps you mean <if test="foo==1"> ? You could do :
    PHP Code:
    $html = preg_replace("~<if test=\"([^\"]*)\">~", "<?php if (\$1) : ?>", $html);
    $html = preg_replace("~</if>~", "<?php endif; ?>", $html);
    But I fail to see that it's so much easier anyway. At least when the conditional logic grows more complex, beyond $foo==1.

    xslt already has this feature btw.
    http://www.w3schools.com/xsl/xsl_if.asp

  5. #5
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's ok, the browser will never see that "xml" so it's a little irrelvent, but yes technically I should alter it to be in that form.

    The conditional logic shouldnt really matter tooooo much, at the moment I'm just concentrating on simple (single) comparitive statements, and to further your example, the template files I'm using are just plain HTML, they are never parsed as PHP code.

  6. #6
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's ok, the browser will never see that "xml" so it's a little irrelvent
    That propably depends on you audience. If they use some sort of syntax-aware editor, it may cause troubles.

    The conditional logic shouldnt really matter tooooo much, at the moment I'm just concentrating on simple (single) comparitive statements
    Some would argue that conditionals are logic, and thus have no place in a template. You could consider replacing them with subtemplates, and control the logic from the controller. Eg :
    Code:
    blah blah
    <block id="is5">
    A is equal to 5 
    </block>
    and
    PHP Code:
    if ($a != 5) {
        
    $template->hideBlock('is5');

    and to further your example, the template files I'm using are just plain HTML, they are never parsed as PHP code.
    It could be though, with eval.

  7. #7
    + platinum's Avatar
    Join Date
    Jun 2001
    Location
    Adelaide, Australia
    Posts
    6,441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good points, the templates already work in the way of having "subtemplates", but I guess this is sort of an alternative syntax to quickly turn elements on and off, without having a million seperate templates.

    And was kind of trying to stay away from eval

  8. #8
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by platinum
    Good points, the templates already work in the way of having "subtemplates", but I guess this is sort of an alternative syntax to quickly turn elements on and off, without having a million seperate templates.
    Yes, I don't fancy hundreds of small fragments, scattered over equally many files either. Having them in the same document, makes it so much easier for designers, since they can see it in a context.

    Quote Originally Posted by platinum
    And was kind of trying to stay away from eval
    I can anticipate that, but the problem is, that if you want syntax in your template-engine, you really can't do it with regex alone - you have to use a lexer, and then it quickly becomes unwieldy (and slow), so I really suggest you stay away from that. Using eval (or just, include) is a shortcut, since you then rely on php's parser to do the work for you. There are obvious drawbacks though.
    Another option, as I hinted at, is to use xslt. Don't let the namespace in front of every tag, intimidate you - xslt doesn't need to be that hard to use for someone, who already knows the basics of html. The biggest plus (or minus, depending on who you ask) is that xslt is (mostly) declarative, which html and css also are. So for someone, who isn't used to think as a programmer, it may be easier to comprehend.

  9. #9
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I personally prefer display logic (OMG you mean there is more logic than just business logic? who would have thought? apparently some people..) in HTML comments, as it makes the templates much cleaner and easier to read, especially with an editor that does syntax highlighting.

    Code:
    <!-- If foo = 1 -->
      <p>Foo is 1</p>
    <!-- EndIf foo -->

  10. #10
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    I personally prefer display logic (...) in HTML comments, as it makes the templates much cleaner and easier to read
    Easier than what ? You're effectively inventing your own language. I don't see the need. Who's your target audience ?
    Quote Originally Posted by dreamscape
    especially with an editor that does syntax highlighting.
    My editor will just show the whole comment in one color.

    Quote Originally Posted by dreamscape
    <!-- EndIf foo -->
    What sort of syntax is that ?

  11. #11
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Easier than what?
    Easier than smarty's method, which disrupts the actual design. And IMO, easier than <if ... >. As with an editor a comment is usually a single color, and lighter than the document in alot of cases, so it does not visually disrupt the document.


    Quote Originally Posted by kyberfabrikken
    You're effectively inventing your own language. I don't see the need. Who's your target audience ?
    My target audience is people who are not programmers, that is who. It might not make sense to you as a programmers, but I could give a **** less because programmers are not the end users... they almost never are.

    Not to mention that view logic belongs in the view, i.e. template, not in the business logic. And having thousands of tiny little fragments is far more ridiculous than a simple & easy to use display logic engine. Splitting into fragments is one of those things devs do to make their lives easier; it doesn't make the lives of users easier (Image you're a regular user or designer and want to change something on a page... you're going to have one hell of a time tracing down where it is at if the template for that page is split into god knows how many fragments)

  12. #12
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    What sort of syntax is that ?
    I makes keeping track of any statements easier. Like I said it doesn't have to make sense to us as programmers. It has to make sense to users. Which is why you can also do just <!-- EndIf --> if you like

  13. #13
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    As with an editor a comment is usually a single color, and lighter than the document in alot of cases, so it does not visually disrupt the document.
    So your meassure of quality, in this case, is that the syntax should be invisible to the template-programmer (~graphic designer) ?
    I agree to that, and that is precisely why I wouldn't have such stuff in the template in the first place. I'd rather keep the template declarative and let the controller do the work.

    Quote Originally Posted by dreamscape
    My target audience is people who are not programmers, that is who.
    In that case, why would you expose them to imperative programming ?

    Quote Originally Posted by dreamscape
    Not to mention that view logic belongs in the view, i.e. template, not in the business logic.
    I presuppose that you put business-logic in the model-layer, which leaves mainly presentational logic for the controller-layer. If you push logic into the template-view, you loose the de-coupling which is the purpose of MVC.

    Quote Originally Posted by dreamscape
    And having thousands of tiny little fragments is far more ridiculous than ...
    Certainly. As I suggested, subtemplates solve that problem.

  14. #14
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by platinum
    It's ok, the browser will never see that "xml" so it's a little irrelvent, but yes technically I should alter it to be in that form.

    The conditional logic shouldnt really matter tooooo much, at the moment I'm just concentrating on simple (single) comparitive statements, and to further your example, the template files I'm using are just plain HTML, they are never parsed as PHP code.
    Xml syntax allows you to use one of the many xml parsers out there (e.g. html_sax) instead of writing your own. If conditions are going to be simple, it would be practical to express them in xml as well, so that you won't need eval.

    Code:
    <ifEqual one="foo" two="bar">
    ...
    </ifEqual>

  15. #15
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Some would argue that conditionals are logic, and thus have no place in a template. You could consider replacing them with subtemplates, and control the logic from the controller.
    Definitely. I wouldn't worry too much about having the template fragmented, provided that you have a relatively well thought out file structure, it shouldn't be too much of a problem for you?

    On the other hand, if you store your template fragments in a database for example, you could pull out the root template, and all of it's assocciated template fragments, at the same time.

    Then you simply pop each fragment off, of an array as you need to, this method is more suitable to a Composite View though...

  16. #16
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    In that case, why would you expose them to imperative programming ?
    It is certainly not imperative. It is there so that users can alter their site how they want without messing around in the core. The primary purpose of template syntax is for basic switches and conditionals and repeating regions. It is not an entire new language, since there are just some basic parts, and it certainly is not difficult to understand. I have yet to have any real user not understand the system once I give them a simple explanation. It also very flexible as well, and very forgiving (the syntax is not strict). I have had several occasions where a user asked if the templates could do something like ___ and what they asked about was perfectly acceptable by the engine (with maybe one or two minor syntax changes), so the system is also very intuitive.

    If you want to talk about de-coupling, then it de-couples the templates from the core application, thus making the users life easier (they don't have to mess with programming code), and making the upgrade path much simpler (when a core update is issued, they do not have to worry about messing up their custom install because public view templates are not part of the core, and anything they want to change can be changed without altering the core).


    Perhaps you can elaborate on what you mean by "subtemplates"... as far as I can tell, it is just another way of saying a template fragment.

  17. #17
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dreamscape
    It is certainly not imperative.
    I was refering to the syntax. Imperative programming, as opposed to declarative programming.

    Quote Originally Posted by dreamscape
    Perhaps you can elaborate on what you mean by "subtemplates"... as far as I can tell, it is just another way of saying a template fragment.
    Yeah, I kind of invented that word. I think I picked it up from patTemplate, which has a <patTemplate:sub>. It works like I sketched out in post #6. Basically, by putting a named region (block) in the template, which can be switched on/off from the controller.
    It's like fragments, but kept within the context.

    Quote Originally Posted by Dr Livingston
    I wouldn't worry too much about having the template fragmented, provided that you have a relatively well thought out file structure, it shouldn't be too much of a problem for you?
    When working in WYSIWYG-enviroments, it actually matters. But at some point, you probably need to chop it up anyway. Still - keeping relatively large blocks, makes the graphic-designers life a lot easier.

  18. #18
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    -BEEP- the Graphic Designers... They're at the very bottom of the food chain, but you are right about WYSIWYG editors...

    Some can be an absolute bast... to work with

  19. #19
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    -BEEP- the Graphic Designers... They're at the very bottom of the food chain, but you are right about WYSIWYG editors...
    hehe. I used to think that way, until it backfired. When the designers can't find their way in the templates, they mess things up, and then guess who have to sort it out ?

  20. #20
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Basically, by putting a named region (block) in the template, which can be switched on/off from the controller.
    Well this is just where we'll have to disagree. I don't like presentation logic in the controller [or anywhere other than the templates really] for a few key reasons. First, if a user wants to make some logical presentation change, they are forced to muck around in the programming language. And Secondly, the templates are too strongly tied to the core application for my liking, again because the core application contains the presentation logic.

  21. #21
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Some would argue that conditionals are logic, and thus have no place in a template.
    Those people would be wrong. Business logic has no place in a template, but it's impossible to template effectively without any logic whatsoever. However, the amount of logic needed is quite minimal; you can do everything you need to do with a simple conditional and a looping construct.

  22. #22
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I get the impression from the various systems around that once you move past a simple template system that the best way to implement more complex templates (with logic) is to compile templates into PHP. This usually means some sort of XML compliant template because you will have the most tools available for parsing. Doing this gives designers something reasonable to work with and keeps them away from PHP (which is also a potential security risk). Since the complied templates are PHP the will be reasonably fast.
    Christopher

  23. #23
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees
    it's impossible to template effectively without any logic whatsoever.
    I didn't say that. I said that the logic shouldn't be in the template. Not that it shouldn't exist.

    You can put presentational logic in viewhelpers. If you put it in the template, where's the distinction between template-view and serverpage ?

  24. #24
    SitePoint Wizard
    Join Date
    Aug 2004
    Location
    California
    Posts
    1,672
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by 33degrees
    Those people would be wrong. Business logic has no place in a template, but it's impossible to template effectively without any logic whatsoever. However, the amount of logic needed is quite minimal; you can do everything you need to do with a simple conditional and a looping construct.
    Quote Originally Posted by kyberfabrikken
    I didn't say that. I said that the logic shouldn't be in the template. Not that it shouldn't exist.

    You can put presentational logic in viewhelpers. If you put it in the template, where's the distinction between template-view and serverpage ?
    This is an interesting discussion. I am torn between these two views. While I tend toward kyberfabrikken's method I think that 33degrees makes an excellent point that a little bit of logic in the templates simplifies/eliminates a lot of other code. ViewHelpers are the prime example. When things are complex they are great at adding clear structure to the data, but most of the time they are a lot of overhead when a trivial amount of logic in the template would do the same thing.

    In many ways I think a tag library (like JSP) is what I really want. There are many cases where I want to define things (like selects, checkboxes, radiobuttons that are reset to a value) or have optional things displayed (based on user account type for example). But I really want to have the definition in the template rather than in the View, ViewHelper or Controller code because it just makes sense and would be simpler to keep it there. I think my goal is to simplify mantenance and reduce the number of files.
    Christopher

  25. #25
    SitePoint Zealot
    Join Date
    Feb 2004
    Location
    Boston, MA
    Posts
    188
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    I didn't say that. I said that the logic shouldn't be in the template. Not that it shouldn't exist.
    This is how I see it, too. In my template class the template variables are set after the logic has already been worked out, the IFs would be in the PHP and if it were true, that variable would be given a value, if not it would just be empty. The way I see it, people editing the templates will want to do things like move around output, not change conditional statements. If they want to mess with the business logic they can edit the appropriate classes.


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
  •