Jump to content

Recommended Posts

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

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! :)

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!

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.

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.

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.

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.

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.