Anti-Moronic Posted August 10, 2010 Share Posted August 10, 2010 Ok, here's what I'm trying to do. In Zend, you can modify a template title for a page within the actual template file even though the title has already been set. So, say I have this in a common header file: <?php echo $title; ?> ..but I then want to maybe change that within my main template page, say my 'about' page. How would I go about that? Currently I do this: include($commonheader); include($content); include($footer); It's in the content where I want to be able to change the title in the header, should I need to. You can do this with Zend but it's beyond me how they do it. Should note, the above is for simplicity sake. I am doing this using OOP methods. Any help appreciated. Maybe ob_flush? Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/ Share on other sites More sharing options...
shlumph Posted August 10, 2010 Share Posted August 10, 2010 Zend is a company, are you referring to Zend Framework? Are you using MVC? Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/#findComment-1097656 Share on other sites More sharing options...
Anti-Moronic Posted August 10, 2010 Author Share Posted August 10, 2010 Well I'm certainly not referring to the company :-) Yes, I'm using MVC. But I'm new to OOP and I'm having trouble on this point. If I include the different template partials, then they are outputted immediately and I cannot modify any output variables in one of the partials which come after. Like now, I can't modify the title in my content partial which is outputted in my header partial because it's been 'included' and so already output. How should I go about this? Thanks for the reply. Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/#findComment-1097660 Share on other sites More sharing options...
shlumph Posted August 10, 2010 Share Posted August 10, 2010 In most of my setups, I have this in my layout.phtml: <title><?php echo $this->title; ?></title> And then, this in each of my view scripts: <?php $this->title = 'Test Page Name'; ?> Alternatively, I could set $this->view->title in my controllers and it'd still get outputted in my layout.phtml script. I'm almost sure partial view scripts would act the same, but I've never tried it. Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/#findComment-1097668 Share on other sites More sharing options...
Anti-Moronic Posted August 10, 2010 Author Share Posted August 10, 2010 I see, thanks! Now do you have any idea how I do this without Zend Framework? To clarify my problem: I have 2 files like this: header>> <?php echo $title; ?> content>> <?php $title = 'something else'; ?> ..and then the template: include(header) include(content) ==== That is very simplified and not at all like what I actually have. But it demonstrates my confusion. How do I put together a template whereby I can edit variables in the header, from within the content before output? Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/#findComment-1097669 Share on other sites More sharing options...
shlumph Posted August 10, 2010 Share Posted August 10, 2010 Yeah, that won't work because you're initializing $title after you're trying to echo it out. That's why ZF made it possible to do what you want Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/#findComment-1097679 Share on other sites More sharing options...
Anti-Moronic Posted August 10, 2010 Author Share Posted August 10, 2010 sorry - $title is already initialized. I am trying to modify it. $title = 'default title'; include(header) include(content) Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/#findComment-1097681 Share on other sites More sharing options...
PFMaBiSmAd Posted August 10, 2010 Share Posted August 10, 2010 The cases where you have seen something like this WORK are those cases where there are not echo statements in the code. This works when you build/assemble/concatenate content and then echo that content at the end of the code on the page. Quote Link to comment https://forums.phpfreaks.com/topic/210346-how-to-modify-already-echoed-variable/#findComment-1097695 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.