SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 75
  1. #26
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    Wouldn't adding

    PHP Code:
    public function __get($variable)
    {
        if (
    $this->parent)
            return 
    $this->parent->{$variable};

    to TemplateView allow in alot of cases to just use the simpler syntax, unless a variable of local scope is masking a more global variable.
    Clever, realy nice - And by adding that you dont need to do the parent->parent->parent thingy if no other variable masks - I'll add it asap and test it


    Edit: Added it + rewrote the CachingTemplateView as a filter so you can cash any type of view or chain of views.
    Attached Files Attached Files

  2. #27
    SitePoint Zealot Overunner's Avatar
    Join Date
    Mar 2004
    Location
    Sweden
    Posts
    180
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I really like the stream wrapper I idea (http://www.mikenaberezny.com/archives/40), but I wonder if the method introduces any significant overhead? Has anyone done any benchmarks? Will the stream wrapper thing affect security in anyway?

  3. #28
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Overunner
    I really like the stream wrapper I idea (http://www.mikenaberezny.com/archives/40), but I wonder if the method introduces any significant overhead? Has anyone done any benchmarks? Will the stream wrapper thing affect security in anyway?
    A long time ago I did a stream wrapper for Smarty templates. It observed cache and compile ids and you could assign the interface through a passed context. That was under PHP4 and speed was most certainly impacted. I haven't tried under PHP5 but intend to revisit eventually. Fortunately, streaming isn't really needed in cases like these -- you want the whole file or nothing -- so certain optimizations are possible.

    I like the idea of using streams to queitly implement caching and various resource types -- but I think that a simplified API to wrap the streams API might be in order so that extending to custom types is simplified.

  4. #29
    There is no general chat z0s0's Avatar
    Join Date
    Aug 1998
    Location
    Melbourne
    Posts
    172
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    After doing the Smarty thing, I'm back to using native PHP templates. No evals, no regex, escaped-by-default. Smells pretty good to me, these days.

    That stream wrapper approach looks worthwhile.. I have to admit all the $this's bug me a little bit; though they are all setup in my text editor as macros.
    Wormly Server Performance Monitoring
    Don't wait for an SMS at 4am. Find out what's really
    going on and fix the problem. www.wormly.com/website-monitoring

  5. #30
    SitePoint Guru Nick Carlson's Avatar
    Join Date
    Aug 2003
    Location
    Denver
    Posts
    644
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cakephp will solve your problem.

    If you want to talk with someone about it, head over to #cakephp on irc.freenode.net IRC.
    ncarlson.net - a programmer's dystopia

  6. #31
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Cakephp is hardly templateing is it(I don't doubt you got some type of templateing in there tho... but it's not "pure" templates is it?)

  7. #32
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by z0s0
    I have to admit all the $this's bug me a little bit
    There is absolutely no need to use $this -- I use the engine described in http://www.massassi.com/php/articles/template_engines/ and it needs no $this->var -- just the plain old $var. As the array with values is extract()ed before including the template, all the variables are scoped locally to fetch() method.

  8. #33
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    There is absolutely no need to use $this -- I use the engine described in http://www.massassi.com/php/articles/template_engines/ and it needs no $this->var -- just the plain old $var. As the array with values is extract()ed before including the template, all the variables are scoped locally to fetch() method.
    Ah yes, but that gives you a nasty name-conflict-problem and not the possibility to refer to parent templates variables(if I'm not wrong?). And it's also a quite nasty $tpl->setVar("var","val"); which is quite cumbersome when you work with arrays, etc. ?

  9. #34
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac
    As the array with values is extract()ed before including the template, all the variables are scoped locally to fetch() method.
    Of course you could still use direct public variable assignment and extract((array)$this).

  10. #35
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Heres a prototype of the using XmlReader & XmlWriter objects for templating. Allows custom namespaces to implemented via NamespaceHandler, so can extended somewhat like jsp taglibs.
    Attached Files Attached Files
    Last edited by Ren; Feb 22, 2006 at 07:33.

  11. #36
    SitePoint Guru thr's Avatar
    Join Date
    Jun 2003
    Location
    Sweden
    Posts
    664
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    Heres a prototype of the using XmlReader & XmlWriter objects for templating. Allows custom namepaced to implemented via NamespaceHandler, so can extended somewhat like jsp taglibs.
    God, that's nice - but how fast/slow is it? (XML tends to be not quite so fast, or?)

  12. #37
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    God, that's nice - but how fast/slow is it? (XML tends to be not quite so fast, or?)
    Havent really tested it for speed. Though I don't think it'd be too difficult to compile to PHP.

    Also could have a cache tag lib, so subtrees of document can be cached, and so the reader can skip over with next().

    Everything is correctly encoded, so aslong as libxml2 is bugfree, shouldn't have any XSS exploits. The downside is not easy to have a variable holding a HTML/XML fragment, as there is no raw method to output with XmlWriter.

  13. #38
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    And it's also a quite nasty $tpl->setVar("var","val"); which is quite cumbersome when you work with arrays, etc. ?
    Not in PHP5, if you use __set().

  14. #39
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    which is quite cumbersome when you work with arrays, etc. ?
    Yes, that is pretty much true, but the manner in which I can get around it, is that the class which has the properties to be set, extends to the base class Dataspace, which implements IDataspace, an interface. In that way, to set properties for the class in question, I only ever need to do this,

    PHP Code:
    $class -> import( new Parameters( array( ... ) ) );
    // ... 
    Or, if the array of variables in question reside in their own class, such as an Active Record for example, I only need to use,

    PHP Code:
    $class -> import$ar );
    // where $class would import the variables, via an export on $ar 
    This approach is just about the cleanest method I can think of to prevent properties being set publically as in the ways being discussed today.

    Of course you could still use direct public variable assignment and extract((array)$this).
    No, you want to avoid giving public access to class properties where ever you can... I'm not in favour of PHP5s __set(), et al either, for that matter. To me, the more I think about it, and then think about the alternatives that are possible, I need to lie down for a while...

  15. #40
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Interesting post by Fowler today, http://martinfowler.com/bliki/GetterEradicator.html , suggests should use public access to class properties for as long as possible.

  16. #41
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, I'm going to have to disagree with some of the comments made in this article, regardless of who the author is

    I think the main gripe Fowler is making, is not so much about public access, but about the use of Getters, or more to the point, the abuse of Getters; Do you remember the article on Java World by one author or another, where Setters and Getters were described as being evil?

    Maybe they are to some degree I suppose - there has to be a trade off at some level; By all means remove the Getters but I wouldn't advocate the use of giving class properties public access.

    I posted an explamation of how I protect class properties, and in a previous post, another example, whereby you could still gain access to a classes properties when they were protected, ie

    PHP Code:
    error_reportingE_ALL E_STRICT );
        
        class 
    Testee {
            private 
    $id 1;
            private 
    $name 'Dr Livingston';
            
            public function 
    __construct() {
            }
            
            public function 
    fetch$obj /* visitor */ ) {
                
    $obj -> set( array( 'id' => $this -> id'name' => $this -> name ) );
            }
        }
        
        class 
    Tester {
            public function 
    __construct() {
            }
            
            public function 
    set$parameters ) {
                
    // do what you want with the parameter passed
                
    echo( '<p>'.$parameters['id'].', '.$parameters['name'].'</p>' );
                
            }
        }
        
        
    $testee = new Testee();
        
    $tester = new Tester();
        
    $testee -> fetch$tester );
        
        exit; 
    Still stand firmly on the side that class properties should be either protected or private myself; To me there isn't any excuse to expose them to the world

  17. #42
    SitePoint Guru 33degrees's Avatar
    Join Date
    May 2005
    Posts
    707
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Ren
    Interesting post by Fowler today, http://martinfowler.com/bliki/GetterEradicator.html , suggests should use public access to class properties for as long as possible.
    Umm, at no point does he advocate the use of public access to class properties over using getters and setters; what he's advocating is to use of methods on objects to manipulate its data, rather than pulling that data out and modifying it in the calling code. The point being that it's better to tell an object what to do than to treat it as a simple store of data.

  18. #43
    SitePoint Member
    Join Date
    Jun 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by thr
    Cakephp is hardly templateing is it(I don't doubt you got some type of templateing in there tho... but it's not "pure" templates is it?)
    CakePHP is a framework.

    However, using Cake - you push data to the view via the controller. Then you have access to "helpers" in the view.

    These helpers are pre-defined in the controller, so you can specify what helpers you want to use.

    The helpers just generate common/complex X/HTML.

    Cheers,
    Chris.

  19. #44
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    No, you want to avoid giving public access to class properties where ever you can...
    Of course you'll want to ensure a correct object state when modifying data most of the time. But for me this is a rule of thumb, it's not set in stone. Assiging template variables shouldn't cause an unexpected state. Of course you'll need PHP5 for this to work safely, as you'll need your private/protected vars to ensure state.

  20. #45
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doing templating in anything other than PHP is usually just a waste of time. Not a single argument about using other mechanisms really holds any water:

    1. Separation of business logic and presentational logic. Yes, do this. There's no reason why you can't use the same language for both. PHP class files, PHP template files. Yay.

    2. Restricting the toolset for template designers. If you have a template designer that you can't trust with the full power of PHP, FIRE THEM RIGHT NOW.

    3. Simpler variable substitution. Sorry, but if you really want to argue that

    <title><?php echo $Title; ?></title>

    and

    <title>{$Title}</title>

    make enough of a difference to justify the performance overhead and general pain-in-the-***-edness (sure, why not?), you've got strange priorities. Of course, you could always just do:

    php echo <<<EMULATE_SMARTY
    <html>
    <head>
    </head>
    <body>
    <h1>$Title</h1>
    <div id="body">$Body</div>
    </body>
    </html>
    EMULATE_SMARTY;

    and get the exact same level of simplicity.

    Here's a simple templating class that's served me quite well:

    PHP Code:
    class Skin
    {    
        const 
    SKINS_DIR "/path/to/skins/dir/";
        const 
    COMMON_SKIN "Common";

        private 
    $SkinName     'Default'
        private 
    $TemplateName 'Default'

        public function 
    __construct($Skin='Default',$Template='Default')
        {            
            
    $this->Skin $Skin;
            
    $this->TemplateName $Template;        
        }    
        
        private function 
    Inc($Template)
        {
            
    $File self::SKINS_DIR.$this->SkinName.'/'.$Template.'.php';
            if(
    file_exists($File))
            {
                return include(
    self::SKINS_DIR.$this->SkinName.'/'.$Template.'.php');
            }
            else
            {
                return 
    $this->IncCommon($Template);
            }
        }    

        
        private function 
    IncCommon($Template)
        {
            return include(
    self::SKINS_DIR.self::COMMON_SKIN.'/'.$Template.'.php');
        }

        
        public function 
    Render()
        {            
            if(
    file_exists(self::SKINS_DIR.$this->SkinName.'/'.$this->TemplateName.'.php'))
            {
                
    $this->Inc($this->TemplateName);
            }
            else
            {
                
    $this->IncCommon($this->TemplateName);
            }                
        }    
        public function 
    Assign($Variable,$Data=null)
        {
            if(
    is_array($Variable))
            {    
                
    $this->array_merge($this->V,$Variable);
            }
            else
            {
                
    $this->V[$Variable] = $Data;
            }        
        }
        
        public function 
    __set($Item,$Value)
        {
            
    $this->Assign($Item,$Value);        
        }

        public function 
    __get($Key)
        {
            if(isset(
    $this->V[$Key]))
            {
                return 
    $this->V[$Key];
            }

            return 
    null;
        }

    basic code:

    PHP Code:
    $Skin = new Skin('MyCoolSkin01');
    $Skin->Title 'My Page title';
    $Skin->Body 'Here\'s my body';
    $Skin->Render(); 
    and the Default.php file:

    PHP Code:
    <?php
    $this
    ->Inc('Header');
    echo 
    '<h1>'.$this->Title.'</h1>'.$this->Body;
    $this->Inc('Footer');
    You could use different models, too. Example:

    PHP Code:
    ...
    $Row $DB->GetRow('SELECT `Name`, `Price`, `Description` FROM `Items` WHERE `ItemID` = '.(int)$ItemID);
    $Skin->Assign($Row);
    ... 
    combined with:

    PHP Code:
    $this->Inc('Header');
    echo <<<TEMPLATE
    <h1>{$this->Name}</h1>
    <p>\$
    {$this->Price}</p>
    <p>
    {$this->Description}</p>
    TEMPLATE;
    $this->Inc('Footer'); 
    Clean, simple, effective.

  21. #46
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Etnu
    Doing templating in anything other than PHP is usually just a waste of time. Not a single argument about using other mechanisms really holds any water:

    1. Separation of business logic and presentational logic. Yes, do this. There's no reason why you can't use the same language for both. PHP class files, PHP template files. Yay.

    2. Restricting the toolset for template designers. If you have a template designer that you can't trust with the full power of PHP, FIRE THEM RIGHT NOW.

    3. Simpler variable substitution. Sorry, but if you really want to argue that

    <title><?php echo $Title; ?></title>

    ..snip...

    Clean, simple, effective.
    Except that seems ignore output escaping, so possibly vulnerable to XSS attacks. If was going to use PHP, you have to <h1>{htmlspecialchars($this->Name)}</h1> etc which then is not very nice templating, hence why I posted the stream based solution earlier, which can magically add output escaping and still retain the simpler syntax.
    Last edited by Ren; Feb 23, 2006 at 07:52. Reason: snipped the quote

  22. #47
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have an idea from reading the ViewStream class code. What about creating templates like:

    <?= $body; ?>
    <? foreach($products as $p){ ?>
    <? echor($p->description); ?><br/>
    <? } ?>

    The contents would be parsed (file_get_contents()) and short tags replaced, then replace all echo/prints etc with a "echo htmlentities($var)". If the user *really* wanted to print out raw data, he/she could just use a special function like echor($v){ echo $v; } - short for echo raw. But instead of including them (via stream), the templates would be compiled into php files (just like the source) but with better/valid php (no short tags, htmlentities etc.) Like this:

    <?php echo htmlentities($body); ?>
    <?php foreach($products as $p){ ?>
    <?php echo $p->description; ?><br/>
    <?php } ?>

    - m

    p.s. would anyone be willing to create the regex for this? I've been trying all morning and can't seem to NOT replace the echor functions with echo htmlentities.

  23. #48
    SitePoint Wizard Ren's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    1,060
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mwmitchell
    I have an idea from reading the ViewStream class code. What about creating templates like:

    <?= $body; ?>
    <? foreach($products as $p){ ?>
    <? echor($p->description); ?><br/>
    <? } ?>

    The contents would be parsed (file_get_contents()) and short tags replaced, then replace all echo/prints etc with a "echo htmlentities($var)". If the user *really* wanted to print out raw data, he/she could just use a special function like echor($v){ echo $v; } - short for echo raw. But instead of including them (via stream), the templates would be compiled into php files (just like the source) but with better/valid php (no short tags, htmlentities etc.) Like this:

    <?php echo htmlentities($body); ?>
    <?php foreach($products as $p){ ?>
    <?php echo $p->description; ?><br/>
    <?php } ?>

    - m

    p.s. would anyone be willing to create the regex for this? I've been trying all morning and can't seem to NOT replace the echor functions with echo htmlentities.
    Hmm, think maybe easier to introduce a new block <? ?>, then just use something like ob_start('htmlentities'); echo $p->description; ob_flush_end(); , but probably would perform slower :/

  24. #49
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Etnu
    Doing templating in anything other than PHP is usually just a waste of time. Not a single argument about using other mechanisms really holds any water
    Not too sure about that. Jeff provides a great template engine with a declarative syntax that builds on the powerful concept of custom tags, for example (think .NET templating).

  25. #50
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    > what he's advocating is to use of methods on objects to manipulate its data, rather than pulling that
    > data out and modifying it in the calling code.

    Yes, I did pick up on that as well, but to me there was more to it, than just that issue... To clarify the matter, here is a perfect example of the definition of Tell,... Don't Ask for those who don't know what is meant...

    http://www.butunclebob.com/ArticleS....sNotAesthetics


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
  •