Page 1 of 1
Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 2:41 pm
by wonderoid
Hello, a php newbie here.
Anyway; here is my question: I want to skip over some html + php code. Is it possible? (elegantly or hack)
Here is an example:
Code: Select all
<?php skip() ?>
<div> some html </div>
<div> <?php echo $var ?></div>
<?php resume() ?>
Here I want to skip between the skip() function and resume(). However using output buffering and discarding the contents is not a solution, $var might be unset and there may be an error. I want that part completely discarded (or not interpreted as php so I can just buffer and discard it).
Thanks!
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 2:53 pm
by desperado
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:05 pm
by wonderoid
Thanks for the reply but goto doesn't seem to jump out of blocks. So it is not possible to jump from inside the skip() function. I want to achieve it by using those 2 functions (if possible ofc, if not i'll try something else).
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:09 pm
by Mirge
You could just put the actual condition for skipping/resuming within your page... or return true/false from the skip() function to say whether to skip or not and then eliminate the resume() function call.
For example:
Code: Select all
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php if(!skip()) { ?>
Four
Five
<?php } /* end if statement */ ?>
Six
Seven
Eight
</body>
</html>
EDIT: Please do not ever use goto.
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:17 pm
by desperado
well I can't actually use goto myself, I'm running 5.2.8, but it takes me back to my good old basic days...
...does anyone remember basic?
...anyone?
ok, what
he said....

Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:20 pm
by wonderoid
Mirge wrote:You could just put the actual condition for skipping/resuming within your page... or return true/false from the skip() function to say whether to skip or not and then eliminate the resume() function call.
For example:
Code: Select all
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php if(!skip()) { ?>
Four
Five
<?php } /* end if statement */ ?>
Six
Seven
Eight
</body>
</html>
EDIT: Please do not ever use goto.
Thanks for the reply Mirge, I think I was not clear on the first message. Of course there are many ways to jump over that part of the code. What I want to do is marking a point as the start and just skip anything until the end marker is found. Think of it like a markup or template syntax. Like ob_start() - end() pair (with the difference that the content will not be executed.)
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:29 pm
by JNettles
Is there some reason you can't just block off sections with an IF statement?
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:38 pm
by desperado
I don't know if this helps, but i've done something like below recently:
Code: Select all
<?php ob_start(); ?>
BLUE
<?php
$printoutput1 = ob_get_contents();
ob_end_clean();
ob_start();
?>
SKY
<?php
$printoutput2 = ob_get_contents();
ob_end_clean();
$printview = $printoutput1 . $printoutput2;
echo $printoutput1;
?>
GARBAGE
<?php echo $printoutput2; ?>
$printview only contains what I need.
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:46 pm
by wonderoid
desperado wrote:I don't know if this helps, but i've done something like below recently:
Code: Select all
<?php ob_start(); ?>
BLUE
<?php
$printoutput1 = ob_get_contents();
ob_end_clean();
ob_start();
?>
SKY
<?php
$printoutput2 = ob_get_contents();
ob_end_clean();
$printview = $printoutput1 . $printoutput2;
echo $printoutput1;
?>
GARBAGE
<?php echo $printoutput2; ?>
$printview only contains what I need.
Output buffering doesn't work for me though, as it executes the code(renders to the buffer). What I was looking for was skipping, which probably doesn't exist unless I use goto in a suitable scope.
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:48 pm
by Mirge
wonderoid wrote:Mirge wrote:You could just put the actual condition for skipping/resuming within your page... or return true/false from the skip() function to say whether to skip or not and then eliminate the resume() function call.
For example:
Code: Select all
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php if(!skip()) { ?>
Four
Five
<?php } /* end if statement */ ?>
Six
Seven
Eight
</body>
</html>
EDIT: Please do not ever use goto.
Thanks for the reply Mirge, I think I was not clear on the first message. Of course there are many ways to jump over that part of the code. What I want to do is marking a point as the start and just skip anything until the end marker is found. Think of it like a markup or template syntax. Like ob_start() - end() pair (with the difference that the content will not be executed.)
So... are you trying to use custom markers & parse the code yourself? I don't see why you can't just use an if() statement.
I repeat, do not use goto... it will only pollute your code and make it unreadable, and more difficult to maintain.. and debug.
Or, if you're just trying to skip it altogether, always...
Code: Select all
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php /*
Four
Five
*/ ?>
Six
Seven
Eight
</body>
</html>
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 3:53 pm
by desperado
well, not exactly. the code I pasted skips the GARBAGE part.
the php will be rendered no matter what when it's called.
Perhaps you should explain what you are trying to achieve, as there is certainly a way to do it.
Re: Is there any way to do this? (skipping code)
Posted: Tue Oct 13, 2009 4:31 pm
by wonderoid
desperado wrote:well, not exactly. the code I pasted skips the GARBAGE part.
the php will be rendered no matter what when it's called.
Perhaps you should explain what you are trying to achieve, as there is certainly a way to do it.
Ok; I am used to developing in Django, and what I was trying to achieve was simple template inheritance. In Django's template inheritance; you can mark a block of template code-html and give it a name, which you can on another template override or extend (like methods). Now I know there are such php frameworks too but it is done by parsing the page(as a text file). I was curious if I could make one without, but either my skills aren't really up to that point or there isn'T a way. What I was trying to achieve was for example; making a block by calling startBlock('name') - endblock(). Now when you extend this template, any code within the block is invalid in the new scope, you might have a variable there that exists in the base page but not in the extended one etc.