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() ?>Thanks!
Moderator: General Moderators
Code: Select all
<?php skip() ?>
<div> some html </div>
<div> <?php echo $var ?></div>
<?php resume() ?>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).desperado wrote:http://php.net/manual/en/control-structures.goto.php
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>
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.)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:
EDIT: Please do not ever use goto.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>
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; ?>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.desperado wrote:I don't know if this helps, but i've done something like below recently:$printview only contains what I need.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; ?>
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.wonderoid wrote: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.)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:
EDIT: Please do not ever use goto.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>
Code: Select all
<html>
<head><title>Foo</title></head>
<body>
One
Two
Three
<?php /*
Four
Five
*/ ?>
Six
Seven
Eight
</body>
</html>
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.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.