TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Template Class Issues (http://www.talkphp.com/advanced-php-programming/3551-template-class-issues.html)

WinSrev 11-01-2008 12:54 PM

Template Class Issues
 
Hey,

Okay, so, we have a template class (wrote from scratch) but the issue lies in the if statement parsing, the issue there is that if there's an if statement within an if statement (html if statement as in {if name='blah'}content{/endif} etc..) then the second if statement will get prematurely cut off, i think it's due to the regex on line 143. Any ideas anybody?

Code: [nvm]

Thanks!

Tanax 11-01-2008 01:24 PM

First thing that crosses my mind, is why you mix PHP5 with PHP4?
PHP Code:

//PHP4
var $output;

//PHP5
public function newTemplate($templateFile) {
// ...



WinSrev 11-01-2008 01:27 PM

Well, var is in PHP5 aswell you know? that's not the issue at all and what you said doesn't make sense.

sketchMedia 11-01-2008 02:13 PM

Quote:

Note: The PHP 4 method of declaring a variable with the var keyword is still supported for compatibility reasons (as a synonym for the public keyword). In PHP 5 before 5.1.3, its usage would generate an E_STRICT warning.

From PHP: Visibility - Manual

var is just an alias for public, but it would make more sense to use visibility keywords to keep your code consistent, as you have used them elsewhere.

WinSrev 11-01-2008 02:27 PM

Hmm, okay, i admit that's a flaw with the script but, not that severe.

Enfernikus 11-01-2008 03:09 PM

I'm not addressing the if statement issue because I feel that the template class needs to be rewritten. it needs a cache system, especially if the site will be large and have lots of forums/topics. Parsing LARGE quantities of data repeatedly would put a strain on the site's loading time.

Tanax 11-01-2008 03:14 PM

I didn't say that it was a FLAW with that part. It was just a question, because TO ME it's weird to see them mixed. Sure it WORKS, but it's weird.

And I mean, if you use public function, then why not public $var ?
And about that, in this case you should use private $output which would actually be better.

WinSrev 11-01-2008 03:19 PM

Okay, Enfernikus: So, whenever you write any system, the first lines of code you write are for a cache system? That's pretty weird.

Tanax: Okay, yes, i agree with what you're saying and i will make sure i go through all the files and make changes as appropriate. Thanks.

Any ideas on the if statement issue though? Thanks

Enfernikus 11-01-2008 03:41 PM

I do write my first lines of code with caching in mind, when ever doing any parsing or database related things.

WinSrev 11-01-2008 05:20 PM

It's quite hard to cache something that doesn't work properly anyway, so, anychance of going back on topic please? thanks.

kjarli 11-02-2008 01:08 PM

Why do you want a template class if php is a template engine on it's own?

WinSrev 11-02-2008 02:17 PM

What are you even talking about?

codefreek 11-02-2008 04:40 PM

Quote:

Originally Posted by kjarli (Post 19308)
Why do you want a template class if php is a template engine on it's own?

mate, you are totally of topic ;P

sketchMedia 11-02-2008 09:42 PM

Quote:

Originally Posted by codefreek (Post 19310)
mate, you are totally of topic ;P

Not really, whats wrong with something like this:

PHP Code:

<?php
class template
{
    private 
$aVarStack = array();
    private 
$szTplFile;
    
    public function 
__construct($szTplFile)
    {
        
$this->szTplFile $szTplFile;
    }
    public function 
__set($szKey$mValue)
    {
        
$this->aVarStack[$szKey] = $mValue;
    }
    public function 
__get($szKey)
    {
        if(!
array_key_exists($szKey$this->aVarStack))
        {
            throw new 
Exception($szKey 'is an invalid template variable');
        }
        return 
$this->aVarStack[$szKey];
    }
    public function 
display()
    {
        include 
$this->szTplFile '.phtml';
    }
}

Now the tpl file:
PHP Code:

<h1><?php echo $this->header?></h1>
<p><?php echo $this->content?></p>

And now the driver:
PHP Code:

<?php
require('template.php');
try
{
    
$tmp = new template('template');
    
$tmp->header 'this is a header';
    
$tmp->content 'This is some content';
    
$tmp->display();
}
catch(
Exception $e)
{
    echo 
'Error in template: '$e->getMessage(); 
}

Its not perfect but at least you don't need to waste the time parsing tpl files for special tokens and then injecting data into them, after all ZendFramework seems to be ok with it.

For if statements/loops etc just use PHP:

PHP Code:

<html>
...
<?php if($name == 'Sam'):?>
<p>Do something here for Sam?</p>
<?php endif;?>

...
</html>


WinSrev 11-03-2008 06:55 AM

Okay, you're an idiot, it's offical, the reason people use template classes it to keep php and html seperate, with open source software someone could download a template with malicious php code in and they wouldn't know any different. All everyone in this topic has done is overlooked the problem, you're finding ways around it instead of finding a solution.

Can a moderator please close this topic? the people on this forum are incredibly stupid.

Salathe 11-03-2008 10:34 AM

I'll agree that the folks posting in this topic haven't directly commented on the original question. Some good points have been raised but they're not really related to what WinSrev's looking for, feel free to discuss those ideas (code consistency, caching, templating appropaches) in other topics but not here.

If anyone does have any comments on parsing the {if} blocks the they're very much welcome.

P.S. WinSrev, please don't get upset if people can't stick to the topic at hand or take wider viewpoints than asked for. That's no reason to label the members of an entire forum as "incredibly stupid", however frustrating it is to have a question repeatedly ignored.

sketchMedia 11-03-2008 10:45 AM

Ok heres your solution: Smarty : Template Engine

I dont suppose you know of the old phrase: JFGI. I'll let you work that out O' clever one.

Quote:

the reason people use template classes it to keep php and html seperate
And to add yet another language adding confusion, Im a PHP developer not a Smarty developer. It also adds more unnecessary parsing for the PHP engine to do and dont bring caching into the equasion because its easy enough without a templating language.

Quote:

with open source software someone could download a template with malicious php code in and they wouldn't know any different
If that is the real reason for templating languages, go tell WordPress, ZendFramework and many more systems that use more or less this approach of templates

If you had been a little nicer and less actually flaming me like a petty forum troll then I may have had a friendly debate with you on the subject, but currently I'd rather you grow up first.

Ok, I'm done with you, I suggest you learn some manners before posting anything related to this forum, this isn't behavior people have in community and it isn't the attitude that I am willing to tolerate.

Tanax 11-03-2008 07:23 PM

JFGI = Just Fucking Google It.

If you don't find your answers here, just search for it yourself. I mean, if you're so lazy you can't even search for it, then don't get angry and upset when you don't get any response to the problem in here.

WinSrev 11-04-2008 11:59 AM

Of course, because i had no idea google exist, i'm not sure how to use a search engine, can someone please advise me? thanks. And Smarty isn't the answer, what a fucking stupid thing to say.

(that was sarcasm)

Can a moderator lock this topic please? thanks.

sketchMedia 11-04-2008 04:12 PM

Quote:

Of course, because i had no idea google exist, i'm not sure how to use a search engine, can someone please advise me? thanks. And Smarty isn't the answer, what a fucking stupid thing to say.

(that was sarcasm)
So smarty is the answer then, and not a 'f**king' stupid thing to say, I'm glad I was able to help.

Would be a good idea to read: TalkPHP - Rules before making any further posts, specifically these rules perhaps:
Quote:

  • No profanities to be used in your posts. Although these will be starred out, it does look rather immature nonetheless.
  • Debates are welcomed, however, do not reduce the debate to a personal level where personal insults are thrown about.

I regard 'Okay, you're an idiot, it's offical' as a personal attack.

Stop being so infantile.


All times are GMT. The time now is 08:23 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0