Jump to content


Photo

conditionals in a templating system


  • Please log in to reply
36 replies to this topic

#21 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 04 August 2006 - 05:13 PM

I really don't see the point of conditionals in a template, but, that's for you to decide :|

#22 dEcade

dEcade

    P2L Staff

  • P2L Staff
  • PipPipPipPip
  • 1,850 posts
  • Gender:Male
  • Location:Saskatoon, Saskatchewan
  • Interests:Guitar, Programming, Storm Chasing, Games (Designing and playing), Hockey, Photography

Posted 04 August 2006 - 06:20 PM

Well for like forums and stuff you could use that type of stuff to show different stuff if the user is logged in or out.

dEcade

#23 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 04 August 2006 - 06:43 PM

It would be alot much easier to just do that in PHP, like a simple find & replace function could cover that

#24 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 04 August 2006 - 07:01 PM

yes but it allows me to be more flexible in my templating without the need for extra php code

#25 cheerio

cheerio

    Young Padawan

  • Members
  • Pip
  • 246 posts
  • Gender:Male

Posted 04 August 2006 - 07:09 PM

yes but it allows me to be more flexible in my templating without the need for extra php code

Oh well, it's just my opinion. It's just that making something like this takes quite some time and research.

#26 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 05 August 2006 - 07:26 AM

I really don't see the point of conditionals in a template, but, that's for you to decide :|



It would be alot much easier to just do that in PHP, like a simple find & replace function could cover that


Think of this,

<if $mod_rewrite equals "on">
<a href="/article/{id}/">link name</a>
<else />
<a href="?article={id}">link name</a>
</else>

Now if you can do that, whats the point of doing it other ways?

Conditionals in a template class are very handy. If you did that using a normal if (assuming there is more in the template file) then you would have to check if mod_rewrite is on, then parse one template, if not parse another which is pointless as well as time consuming ;)

+ it allows greater customisation / change to be made to a site and its design without editing the core code.

#27 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 05 August 2006 - 09:24 AM

yep, but i wanted a good conditional system and a loop system (that one still stumps me to the tree core lol)

#28 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 05 August 2006 - 09:31 AM

Aha...

OK, my previous problem with eval producing an error has been solved. It seems eval starts in "php mode" if that makes sense (it starts with <?php) so all you do is...

eval('?>' . $data);

Now the only problem is that because eval evaluates the code it will also output.

And itunes, It's something you will have to work on. As rc69 said, it can be done but it takes a while so you cant expect someone t just hand out a code ;)

Edited by matthewJ, 23 December 2006 - 07:31 PM.


#29 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 05 August 2006 - 09:35 AM

i know that i was just hoping i would find some luck here and i think the php dev team should change the way eval works then

#30 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 05 August 2006 - 09:41 AM

What you have to remember is eval is a function you have to be very careful with, recent phpbb and vB exploits have derived from the fact both of them use eval in there template class. I'm only going down the eval root as i know no other way ;)

edit: To put eval data into a string, use this

ob_start();

eval('?>'.$temp_data);

$temp_data = ob_get_clean();

Sitepoint guys helped me out with that, i never thought of it. Works great :D I got basic conditionals heh.

Edited by matthewJ, 23 December 2006 - 07:29 PM.


#31 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 21 December 2006 - 02:17 PM

i have blocks working (took a while) and now i am working on the conditionals, then i will work on switch blocks, then the loops, or mabye loops then switch blocks, because switch is not that important right now, and i do not intend to use eval anywhere now, i have not yet and i will not :lol:, i don't want to open up security holes in my website. well thanks for all the help, and my regex has gotten alot better (although i could still use some good explanations and examples and stuff)

so thanks for all your help. when i do get it implemented i might do a tutorial on it so. that will be good

#32 Koncept

Koncept

    Young Padawan

  • Members
  • Pip
  • 41 posts

Posted 23 December 2006 - 03:49 PM

Does scope matter to you?

Also for conditional maybe you can poke around this method
function parse(&$context, &$renderer, &$node) 
	{
		$this->requireAttributes('var');
		$op = $this->requireOne('eq', 'neq', 'gt', 'lt', 'geq', 'leq', 'eqvar', 'neqvar', 'geqvar', 'leqvar');
		
		$operations = array(
			'eq' => '==',
			'eqvar' => '==',
			'neq' => '!=',
			'neqvar' => '!=',
			'gt' => '>',
			'lt' => '<',
			'geq' => '>=',
			'geqvar' => '>=',
			'leq' => '<=',
			'leqvar' => '<=',
			);
		
		$a = $context->get($this->getAttribute('var'));
		$b = $this->getAttribute($op);
		
		if ($b == 'NULL') {
			$b = NULL;
		}
		
		// TODO:{[this is ugly[parse[FAIfTag}
		if(strpos($op, 'var') !== FALSE) 
		{
			$b = $context->get($b);
		}
		
		$op = $operations[$op];
		
		eval("\$ret = (bool)('$a' $op '$b');");

		if ($ret) 
		{
			return $renderer->doNode($node);
		}
	}


#33 Chaos King

Chaos King

    Senior Programmer

  • P2L Staff
  • PipPipPip
  • 676 posts
  • Gender:Male
  • Location:Florida

Posted 23 December 2006 - 05:54 PM

This is quite an interesting thread in dead. I have never tried to do one myself, but in the possible future, I might write a tutorial. Very interesting read.

#34 Matthew.

Matthew.

    Official Spammer .Matt

  • Members
  • PipPipPipPip
  • 2,749 posts
  • Gender:Male
  • Location:England

Posted 23 December 2006 - 07:28 PM

This is quite an interesting thread in dead. I have never tried to do one myself, but in the possible future, I might write a tutorial. Very interesting read.


The only thing i found interesting in my posts is the vast amount of spelling mistakes that i know will fix and the incorrect information which i know have to correct ;)

Glad this thread is still active, i have a solution that i use as of now but i am not happy with it.

#35 Jordan Pittman

Jordan Pittman

    Young Padawan

  • Publishing Betazoids
  • Pip
  • 56 posts
  • Gender:Male

Posted 23 December 2006 - 08:06 PM

Does scope matter to you?

Also for conditional maybe you can poke around this method

function parse(&$context, &$renderer, &$node) 
	{
		$this->requireAttributes('var');
		$op = $this->requireOne('eq', 'neq', 'gt', 'lt', 'geq', 'leq', 'eqvar', 'neqvar', 'geqvar', 'leqvar');
		
		$operations = array(
			'eq' => '==',
			'eqvar' => '==',
			'neq' => '!=',
			'neqvar' => '!=',
			'gt' => '>',
			'lt' => '<',
			'geq' => '>=',
			'geqvar' => '>=',
			'leq' => '<=',
			'leqvar' => '<=',
			);
		
		$a = $context->get($this->getAttribute('var'));
		$b = $this->getAttribute($op);
		
		if ($b == 'NULL') {
			$b = NULL;
		}
		
		// TODO:{[this is ugly[parse[FAIfTag}
		if(strpos($op, 'var') !== FALSE) 
		{
			$b = $context->get($b);
		}
		
		$op = $operations[$op];
		
		eval("\$ret = (bool)('$a' $op '$b');");

		if ($ret) 
		{
			return $renderer->doNode($node);
		}
	}


Not sure i get the usage of that

Edited by itunes66, 23 December 2006 - 08:07 PM.


#36 Demonslay

Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 973 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 24 December 2006 - 09:51 AM

Ya, the code he supplied is just a tad over my head since I can't see the full class. :)

But I think I see that it is supposed to find any conditionals, such as <!-- if $this eq $that -->, then it will try to construct a conditional with that, and transform it into if($this == $that).
Which is basically what any template system I've seen does.

This is quite an interesting read, I agree lol.
And an interesting topic. Never quite understood this stuff before now though. :)

#37 rc69

rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 24 December 2006 - 05:38 PM

I really don't see the point of conditionals in a template, but, that's for you to decide :|

I second that...

Think of this,

<if $mod_rewrite equals "on">
<a href="/article/{id}/">link name</a>
<else />
<a href="?article={id}">link name</a>
</else>

Now if you can do that, whats the point of doing it other ways?

Conditionals in a template class are very handy. If you did that using a normal if (assuming there is more in the template file) then you would have to check if mod_rewrite is on, then parse one template, if not parse another which is pointless as well as time consuming :)

+ it allows greater customisation / change to be made to a site and its design without editing the core code.

That's why i like my template class. It can do this, but you'd have to use a block.
<!-- START block -->
<!-- SWITCH modRewrite_on -->
<a href="/article/{id}/">link name</a>
<!-- END modRewrite_on>
<!-- SWITCH modRewrite_off -->
<a href="?article={id}">link name</a>
<!-- END modRewrite_off -->
<!-- STOP block -->
Again, that's with my old class. My new one has a more efficient way of doing this. The only thing it lacks is the other comparasin operators (<, >, !=, etc...).

I believe if i (or even somebody who can figure out my class without an explanation) sit down, i could easily work the other operators in there, and even remove the requirement for it to be in a block. Since it's christmas break, i might just do that (shouldn't take more than a day after i actually sit down).

Aha...

OK, my previous problem with eval producing an error has been solved. It seems eval starts in "php mode" if that makes sense (it starts with <?php) so all you do is...

eval('?>' . $data);

Now the only problem is that because eval evaluates the code it will also output.

All i have to say is :)

It's true that eval will echo any data it's told to echo. So will include(). But, both include and eval have one feature that few appear to know about. You can call return within both and it will return a value just like any custom function.

// Foo.php
$bar = 2 + 2;
return $bar;

// include example
$foo = include 'Foo.php';
echo 'Foo equals: '.$foo;
The same thing is true for eval. Rather than echoing your data, store it in a variable, then return it at the end of the script.
Naturally, any html in the eval/included code will be printed, but's something you'll have to make sure you avoid.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users