SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 51
  1. #26
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey, can you post example code of all of the classes you are talking about?

    -matt

  2. #27
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It isn't all that complicated. The code for a basic Reporter is straight from the PHP.net docs, and the Writer just looks after adding all the text into a string. I'm going to expand the Writer class so that I can send it "real" XML fragments. My app is still in development, so I've not needed caching, though it should be easy enough to add to either the Reporter or the Writer class.

    Usage
    PHP Code:
    $Writer = new Writer;
    $Writer.writeObject($Page);
    $Writer.run(new Reporter); 
    In my application, there is a $State object passed to the Reporter constructor which tells it which template to use. XSL can include other XSL files in the XSL itself, so you don't need to worry about things like that in the PHP code.

    Writer
    PHP Code:
    class Writer {
        
        protected 
    $output;
        
        public function 
    __construct() {
            
    $this->output '';
        }
        
        public function 
    run($Reporter) {
            
            
    $xml  '<root>' $this->output '</root>';
            
            
    $xmlDoc = new DomDocument('1.0');
            
    $xmlDoc->LoadXML($xml);
            
            echo 
    $Reporter->report($xmlDoc);
        }
        
        public function 
    writeString($string) {
            
    $this->output .= $string;
        }
        
        public function 
    writeObject(Writeable $object) {
            
    $object->write($this);
        }

    Reporter

    PHP Code:
    class Reporter {
        
        function 
    report($xml) {
            
    $xsl = new DomDocument;
            
    $xsl->load('template.xsl');
            
            
    $proc = new XSLTProcessor;
            
    $proc->importStyleSheet($xsl);
            
            return 
    $proc->transformToXML($xml);
        }
        

    The advantage of splitting the reporter and the writer, is that I have a second reporter which simply outputs the raw XML for debugging, then I can swap to the "real" reporter when working on the templates themselves.

    (I'm not actually correct calling XSL a templating system, it is a "Transform View" instead of a "Template View". I find the "Transform View" pattern to be the more powerful solution.)

    Douglas
    Hello World

  3. #28
    Anonymous
    SitePoint Community Guest
    I am having a hard time figuring out how to do something like this:

    There will always be a navigation block called "Community" that all users will see. If a user is logged in, I have to show a "Community" navigation block, and a "My Account" navigation block.

    I'm trying to find an efficient way to add navigation blocks.

  4. #29
    tree
    SitePoint Community Guest
    what's wrong with

    <code>
    <?php
    $pv= array(
    'name'=> 'John Doe',
    'email' => 'john@doe.com',
    'password' => 'donottell',
    )
    include('page.tpl.php');


    ?>

    /// this is what page.tpl.php looks like
    <table cellpadding="3" border="0" cellspacing="1">
    <tr>
    <td>Name</td>
    <td><?=$pv['name'];?></td>
    </tr>
    <tr>
    <td>Email</td>
    <td><?=$pv['email'];?></td>
    </tr>
    <tr>
    <td>Password</td>
    <td><?=$pv['password'];?></td>
    </tr>
    </table>




    </code>

  5. #30
    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 DougBTX
    It isn't all that complicated.
    Ha ha, I wrote that!
    Hello World

  6. #31
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    its first time i am here.....
    now days i am looking for some good template engine.

    http://mannan.zabvision.edu.pk

  7. #32
    John Kostaras
    SitePoint Community Guest
    Hi,

    your template is very interesting and useful. However, can it handle urls like this: index.php?param=1 ?

    fetch() method crashes because it expects a file. How to overcome this? Use something like this?

    $param=1;
    $tpl->set_vars($param);

    Thanks for your prompt reply.

    Regards,

    John.

  8. #33
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have one question on the cache time out. In the example file caching.php it is set to 900. What unit is it - seconds, minutes?

    Here´s the code:

    PHP Code:
    $cache_id $file $_SERVER['REQUEST_URI'];
    $tpl = & new CachedTemplate($path$cache_id900); 

  9. #34
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Adam E
    Can anyone help with the error I am getting?
    from the article:
    This script assumes your cache files will go in ./cache/, so you must create that directory and chmod it so the Web server can write to it.

  10. #35
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oerdec
    I have one question on the cache time out. In the example file caching.php it is set to 900. What unit is it - seconds, minutes?

    Here´s the code:

    PHP Code:
    $cache_id $file $_SERVER['REQUEST_URI'];
    $tpl = & new CachedTemplate($path$cache_id900); 
    Its 900 Seconds which is equivalent to 15 minutes

  11. #36
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm, I think I´m far away of separation of business and presentation logic. It´s much faster and easier to do something like the following code directly in the template file.

    PHP Code:
      <select name="category">
        <option value="0">bitte auswählen</option>
    <?php 
    while ($row mysql_fetch_array($categoriesMYSQL_ASSOC)) {
      if (isset(
    $category) && $category == $row['catid']) {
        echo 
    "    <option value=\"".$row['catid']."\" selected=\"selected\">".$row['title']."</option>\n";
      }
      else {
        echo 
    "    <option value=\"".$row['catid']."\">".$row['title']."</option>\n";
      }
    }
    ?>    
      </select>

  12. #37
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    On caching: I don´t understand the concept of this approach. I think it´s better to cache pages requested by users not pages changed by the coder.

  13. #38
    uellue
    SitePoint Community Guest
    Caching is something that should be done by proxy servers or browsers I think. If the delivered content only depends on GET paramters every HTTP cache will work just fine. The HTTP protocol offers means to control the behaviour of caches, e.g. with the "Expires:", "Cache-Control:" or "Last-Modified:" headers. Squid (http://www.squid-cache.org/) is quite some times faster than every PHP script you could think up and it offers a special "http-accelerator" mode which is designed exactly for setups like this (see http://squid-docs.sourceforge.net/latest/html/c2416.html).
    In order to let squid cache URLs with a "?" sign, one has to remove the line in the default configuration which forbids this.
    It took me some hours to find out that I have to uncomment this line:

    acl QUERY urlpath_regex cgi-bin \?

  14. #39
    uellue
    SitePoint Community Guest
    Sorry, I wanted to say "... <em>comment out</em> this line: ..."

  15. #40
    chris
    SitePoint Community Guest
    Hi, great article! I actually came up with something very similar myself, and have a suggestion to make.

    Instead of:

    $contents = ob_get_contents();
    ob_end_clean();
    return $contents;

    I find it neater (and possibly faster?) to use:

    return ob_get_clean();

    I hope this helps.

    Kind Regards,

    Chris
    chris SPOT wydra FUNKYASYMBOL gmail SPOT com

  16. #41
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Location
    Lyon, France
    Posts
    43
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by oerdec
    hmm, I think I´m far away of separation of business and presentation logic. It´s much faster and easier to do something like the following code directly in the template file.
    You're right, faster and easier, because separating logics is not at all about that. It's about improving your code and design. The coder is happy because he doesn't have to deal with HTML code, it's not his job afterall. The designer is happy because he doesn't have to deal with PHP code, business logic. You can't seriously ask designers to fetch data from a database using "mysql_fetch_array"-like functions nor to "encrypt" their beautiful HTML codes into "echos". It's not a life for a designer, I think it would even kill "it".

    Quote Originally Posted by oerdec
    PHP Code:
      <select name="category">
        <option value="0">bitte auswählen</option>
    <?php 
    while ($row mysql_fetch_array($categoriesMYSQL_ASSOC)) {
      if (isset(
    $category) && $category == $row['catid']) {
        echo 
    "    <option value=\"".$row['catid']."\" selected=\"selected\">".$row['title']."</option>\n";
      }
      else {
        echo 
    "    <option value=\"".$row['catid']."\">".$row['title']."</option>\n";
      }
    }
    ?>    
      </select>
    So let's clean up your code to see how designer-friendly it could be :
    • Replace the while loop by a good old foreach. That can be achieved by using Data Access Objects that implement the PHP 5 Iterator interface.
    • Avoid if and long conditions. They can be replaced by ternary operators for example.
    • Don't "encrypt" plain HTML code in a echo call. You have to escape quotes and concatenate PHP variables... Why suffer when you can use the short tags <?= ?> ? From the Introduction chapter of the manual we can read that PHP is a « scripting language that is especially suited for Web development and can be embedded into HTML ». That's why so many people claim that PHP itself can be used as a template engine, because it's meant for that ! In some way I think Smarty-like templates and many tutorials published on the Web lead developers in the wrong direction. I also strongly recommand you to read the Basic syntax chapter. I think most developers just forget about how simple things should be, because PHP is all about making us happy...ly ever after.
    • Avoid copying and pasting your code at all cost. Make your code reusable. Use functions, group them into classes as methods... The strings that your echo output are pretty much the same, making the if useless.


    Here's your code rewritten following these principles :

    PHP Code:
      <select name="category">
        <option value="0">bitte auswählen</option>
    <?php foreach ($categories as $row) : ?>
        <option value="<?= $row['catid'?>"<?= ($category == $row['catid']) ? ' selected' '' ?>><?= $row['title'?></option>
    <?php endforeach; ?>
      </select>

  17. #42
    SitePoint Member
    Join Date
    Jun 2006
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by jmmolina
    You're right, faster and easier, because separating logics is not at all about that. It's about improving your code and design. The coder is happy because he doesn't have to deal with HTML code, it's not his job afterall. The designer is happy because he doesn't have to deal with PHP code, business logic. You can't seriously ask designers to fetch data from a database using "mysql_fetch_array"-like functions nor to "encrypt" their beautiful HTML codes into "echos". It's not a life for a designer, I think it would even kill "it".
    The problem is, people still think way to much in terms of PHP & HTML.

    I'm rather disgusted to see how so many people fall back to template "solutions", because they think the designer needs access to ALL of the html.

    Jmmolina, your referring how a coder is happy not to deal with HTML code, and a designer doesnt want to deal with PHP code. All good & well, but that is one of the biggest contractions in terms i've seen a while.

    Coder -> Code -> ... <- HTML < - Designer

    Notice a problem here? Somebody needs to be the sucker who writes the basic HTML ( tables etc ) or PHP ( loops, variables ). And in general, the Designer are the group who have little to no know how about programing. Even worse, sinds they have no know how of the variables, arrays names etc used, they can't do much but make a site look good. Its the Coder who ends up writing most of the html for the modules/functions/etc blocks anyway.

    Now, to fall back to the basics. Sinds its our job most of the time to include almost all the basic HTML anyways, we also can include something called .. CSS definitions.

    Now all of a sudden, that developer does NOT need to do any HTML writing, sinds the basic HTML is there ( we coders need to put it in anyway ), and everything is equip with CSS. So whats left? Of course, the basic "layout" of the site.

    Now, where i work, they did a rather simple solution to that problem. The layout of the site is in the database, split up in code blocks. So, you have Row1: Your site top html. Row2: your search module etc, Row3: Start of your left page block. Row4: Module 1, Row5: Module 2, Row6: Close Html code of the left block ... And your site is rendered by sequentially reading & displaying the html or module blocks.

    All of a sudden, all the designer needs to do is, make a copy of the HTML output ( what is nothing more then a simple SQL Query loop, that any coder can make in 1 min time ), and a simple Admin interface to update the code blocks ( and put back the "updated" html ).

    What do we see now ... The designer hardly touches anymore HTML, the basic layout, and that's it. All the rest he/she does with CSS.

    Now, all that's needed is a basic call to render the page on a request from the user. Takes only 1 simple sql call. And that's all the overhead you end up with.

    Instead of placing all the overhead on your rendering ability by splitting, you limit it to a very small fraction. And its even more secure, because at most the designers can **** up the layout themselves, and not do anything wrong to your code. Hell, with one added inner join, the query can be made to server 1000 of different pages, with so little to no overhead.

    I'm amazed to read all these topics here, about one after another contraption to separate code logic from layout logic, when in most cases, there is no need for it at all thanks to css, and a bit of clever programing.

    Sure, at times a designer will need to call upon a coder to add a few 'missing' css tags, but that's as far as the interaction needs to go.

    Use the KISS system people. Think before you implement things. Don't fall back on templates just because everybody before you did. You save more time writing a simple admin page for the developers to alter the layout, move your modules/functions around & attach the css files to that layout ( all end up in the DB ), then almost double writing your loops, increasing your memory consumption, running replace patterns etc ...

  18. #43
    monitormensch oerdec's Avatar
    Join Date
    Sep 2004
    Location
    Hamburg
    Posts
    706
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    @jmmolina: Thanks for your hints.

    At first I have to say I`m the designer and coder in one person. In the case other designers are involved in my projects they never have any idea of HTML.

    Quote Originally Posted by jmmolina
    Replace the while loop by a good old foreach. That can be achieved by using Data Access Objects that implement the PHP 5 Iterator interface.
    And what about PHP 4? I have no idea how to achieve that. I really would like to see a solution.

    Quote Originally Posted by jmmolina
    Why suffer when you can use the short tags <?= ?>
    I don´t use them because I heard that they might be switched off by the ISP in php.ini.

    Quote Originally Posted by jmmolina
    foreach ... endforeach
    I don't like it because my text editor doesn´t recognize that both parts belong together.

    oerdec

  19. #44
    Yello
    SitePoint Community Guest
    Just a merge of set() and set_vars() methods.
    Useful for lousy coders like me ;-).

    /**
    * Set a template variable.
    *
    * If value is not set and $name is an array, this
    * sets the key-value pairs in the array to $this->vars.
    *
    * @param string $name name of the variable to set
    * @param mixed $value the value of the variable
    *
    * @return void
    */
    function set($name, $value = null) {
    if (is_array($name) and $value = null) {
    $this->vars = array_merge($this->vars, $name);
    } else {
    $this->vars[$name] = $value;
    }
    }

  20. #45
    SitePoint Guru
    Join Date
    Sep 2008
    Location
    Dubai
    Posts
    971
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    very good article

  21. #46
    kL
    SitePoint Community Guest
    You've got "<?=$user['name'];?>" in your template. What if user puts HTML in there? A malicious script?

    That's why I prefer full-featured template engines, like PHPTAL or OPT2, which handle all escaping _automatically_ (that's the key).

  22. #47
    BelizeNut
    SitePoint Community Guest
    Thank you for this great explanation of template classes. I have been looking for a tutorial like this forever. I am new to PHP and SQL, but this has helped me tons in my quest to implement my own template class.

    One question about the user_list file. In the function, fetch_user_list, you hard code in the names, id's and emails for the list.

    However, I am trying to pull this information in from a database and can't figure out how to populate the data array.

    If I use the code below, it seems that I can't access the data in the array because there are no identifiers associated with the sub-arrays.

    while($row = mysql_fetch_array($result))
    {
    $arr[]=$row;
    }

    I realize this might be a stupid question, but any help would be appreciated.

    Thanks,
    Belize Nut

  23. #48
    SitePoint Member
    Join Date
    Oct 2006
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kL View Post
    You've got "<?=$user['name'];?>" in your template. What if user puts HTML in there? A malicious script?

    That's why I prefer full-featured template engines, like PHPTAL or OPT2, which handle all escaping _automatically_ (that's the key).
    Shouldn't you be detecting usernames for invalid characters by registration?

  24. #49
    SitePoint Member
    Join Date
    Jan 2010
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, to revive and old thread. I was searching around the forum and I couldn't find a better place. I'm new to using php as a template engine. I really like the idea and want to implement a system based on Brian's article but have a few questions.

    I noticed the article was rewritten on 03, has there been any updates to the code for the new versions of php?

    If you guys know of any articles or examples I would love to see them.

    Thanks!

  25. #50
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    4,904
    Mentioned
    93 Post(s)
    Tagged
    0 Thread(s)
    Version 3 of Smarty is out in Beta forum, it's a complete re-write. I've never used any version of Smarty myself so can't say how good it is.
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator


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
  •