conditionals in a templating system
#21
Posted 04 August 2006 - 05:13 PM
#22
Posted 04 August 2006 - 06:20 PM
dEcade
#23
Posted 04 August 2006 - 06:43 PM
#24
Posted 04 August 2006 - 07:01 PM
#25
Posted 04 August 2006 - 07:09 PM
Oh well, it's just my opinion. It's just that making something like this takes quite some time and research.yes but it allows me to be more flexible in my templating without the need for extra php code
#26
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
Posted 05 August 2006 - 09:24 AM
#28
Posted 05 August 2006 - 09:31 AM
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
Posted 05 August 2006 - 09:35 AM
#30
Posted 05 August 2006 - 09:41 AM
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 I got basic conditionals heh.
Edited by matthewJ, 23 December 2006 - 07:29 PM.
#31
Posted 21 December 2006 - 02:17 PM
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
Posted 23 December 2006 - 03:49 PM
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
Posted 23 December 2006 - 05:54 PM
#34
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
Posted 23 December 2006 - 08:06 PM
Does scope matter to you?
Also for conditional maybe you can poke around this methodfunction 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
Posted 24 December 2006 - 09:51 AM
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
Posted 24 December 2006 - 05:38 PM
I second that...I really don't see the point of conditionals in a template, but, that's for you to decide :|
That's why i like my template class. It can do this, but you'd have to use a block.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.
<!-- 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).
All i have to say isAha...
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.
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