0

I am passing a variable to a view script from a controller with Zend Framework. I want to know if there is anyway I can pre-filter the view so that I can change how the variable is retrieved in the view script.

For example. In the controller I have:

$this->view->name = 'Bob';

And in the view I have:

echo $this->name;

Which works fine no question! What I want is to occationally have the ability to change it so I can just use:

echo $name;

So basically, removing the $this statement. Is this possible? I am making a template and have other designers using the template system and want to make it easier for them then to type $this->array->name all the time.

I know in the view script I can simply add:

$name = $this->name;

But I would like to do that in the controller somewhere.

Thanks for your advice!

1
  • If you want to sign messages with your name, change your username, do not include a line in the actual posts. :) Commented Dec 11, 2011 at 13:20

2 Answers 2

2

I'll say right up front that I think this is not a good idea to monkey around with the scope of your view's output variables. This works against the framework's MVC pattern by exposing the view's internals all over the global scope. But if you really insist on doing this, in the controller, you can probably assign them globally like this:

$GLOBALS['name'] = $this->view->name;

And access them later as $name or $GLOBALS['name'].

Note: depending on when the view's variables get populated versus when you assign them in the controller, you might need to assign references.

$GLOBALS['name'] = &$this->view->name;
Sign up to request clarification or add additional context in comments.

4 Comments

Frightening! Technically speaking, what you write might make sense. And it seems to me like it would work in practice. But, at that point, I'd start asking myself... why bothering using an MVC framework, when I'm screwing it up entirely ...and creating some not so good spaghetti code? ;-)
@maraspin I consider this one of those "enough rope with which to hang oneself" answers...Hopefully my disclaimer to the OP was clear enough.
Got your point. ;-) Comment of mine was actually more to scare novices against "strange" practices, more than anything else.
Hmm yeah, I wasn't really looking to use globals at all. I was hoping for some pre-rendering function I could use in the controller to pass the correct variable to the view. If I need to modify the core in any way or the structure it's probably not worth doing.
1

Your idea is reasonable and seems quite easy to achieve. Just add this line to the beginning of your template:

<?php extract(get_object_vars($this)) ?>

Passing state to view by the means of global variables might not be considered a good idea by some, but this way is safe, as extract creates local variables, not global ones.

If you would like to reuse this method and prefer not to copy&paste, you could easily inherit from Zend_View, introduce this line to method (if I recall correctly) _run, and then in your bootstrap:

  • create an instance of the controller,
  • get the ViewRenderer action helper (using static method of Zend_Controller_Action_HelperBroker)
  • inject your view instance into ViewRenderer

This way all your views will present their public properties as variables.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.