topcat Posted February 4, 2013 Share Posted February 4, 2013 Hi folks, Strange problem I hope someone can help me with. I've written an MVC framework but am having a small issue witht he view code. I want it to work in a similar fashion to Zend Framework where you can just assign variables to the view from the controller and then echo them out in the view template which works perfectly well. This is achieved by magic methods. eg In controller: $this->view->welcomeMessage = "Hello World"; In the view template: echo $this->welcomeMessage; The controller instantiates a new view object, the view object in turn loads up the view template for the required controller action. The view obnect contains other features such as escaping output and various helpers for common tasks. I want to store all of the response code in the view object until the response is complete and then return it as a response but this is where I run into problems. The php in the view template only works when I use require to include the view template (.phtml) in the view class. If I use readfile() for example to read the contents of the view template into the class then the php code in the template is not executed when I finally return the response. I've tried looking to see how Zend achieves this but it gets quite complicated in ZF when you get to the view and layout code . Any ideas of how to get round this issue would be very welcome, it's the only issue left to resolve to get my MVC up and running!! Many thanks, Tony Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/ Share on other sites More sharing options...
trq Posted February 4, 2013 Share Posted February 4, 2013 I don't see an issue. Why can't you use include ? You might also want to take a look at how zf2 does this now, as what you describe above is a pretty dated method. There are better ways these days. Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1409999 Share on other sites More sharing options...
topcat Posted February 4, 2013 Author Share Posted February 4, 2013 Thanks for the reply!! The problem is the lack of control over the response. For example if I want to pull in a layout template that is consistant for all actions in a controller and then pull in the specific view template for each action and place it at a certain position in the layout template, I can't see a way of doing this without including the view templatedirectly fro m the layout template which is not ideal. Thanks for the comment about ZF2. I would just like to get this working first before I start redesigning it! Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1410003 Share on other sites More sharing options...
trq Posted February 4, 2013 Share Posted February 4, 2013 I still don't see the issue. You use placeholders to include files within other files or implement template inheritance. Can you be specific about the actual problem? Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1410006 Share on other sites More sharing options...
topcat Posted February 4, 2013 Author Share Posted February 4, 2013 OK, so if I have a view object that pulls in a view.phtml file and a layout.phtml file. How do I "inject" the view.phtml into the layout.phtml file? If I have the line <? echo $this->content; ?> in the layout file. Then in the view object I use something like: function (loadTemplates){ $viewCode = readfile('path/to/viewScript'); $this->content = $viewCode; } $this is in the same scope for the layout.phtml, view.phtml and the view object so should be ok but the php code in the view script does not work, the line <? echo $this->welcomeMessage; ?> just echos "orld< ?>" I guess I could have <? require $this->viewTemplate; ?> in the layout where I want the code inserted, and then set $this->viewTemplate = "path/to/view.phtml" in the view object but I would prefer to insert it from the view script and I think I should be able to. Hope that makes sense! Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1410007 Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 Only do the including when you're showing/parsing the view, not on load. Instead just register the filename and path, after verifying that it exists and is readable. That will fix the problem, and allow you to change things up as much as you want prior to actually displaying the content. Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1410008 Share on other sites More sharing options...
topcat Posted February 4, 2013 Author Share Posted February 4, 2013 Hi Christian, Thanks for the response. I get what you're saying but how would you actually implement the nested templates? Conceptually it's very simple but when I actually try to implement it is when I run into issues. What you suggest is exactly what I am trying to do, but the nesting of one template in to another without know ing where the template will be nested until runtime is where I am having a problem. If I know beforehand where the template will be nested I can use a placeholder as described above, but how can I pull in a template and inject it where I choose from the view object? A concrete example would be really useful! Many thanks. Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1410011 Share on other sites More sharing options...
Christian F. Posted February 4, 2013 Share Posted February 4, 2013 I've done things a bit differently than you, in that I'm not parsing PHP in my template files. Instead I'm doing simple string replacement, which makes this a whole lot easier to accomplish. Anyway, the simplest thing you could do, is to use the parsing method inside the views that has nested views. Seeing as the designer who edits the template files would know when a nested view is needed, it's easy enough to just call the method to show it. <h1>Nested view:</h1> <?php $this->show ('nested.phtml'); ?> or similar. Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1410013 Share on other sites More sharing options...
kicken Posted February 4, 2013 Share Posted February 4, 2013 If you want to have your template php file executed and get the result as a string rather than it being output to the browser, then you'll want to use output buffering. For example: ob_start(); include('template.php'); $content = ob_get_clean(); ///do whatever with $content. Quote Link to comment https://forums.phpfreaks.com/topic/274005-including-php-from-a-template-into-a-class/#findComment-1410064 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.