2

I'm trying to add some custom html tags to my head script in my layout via. the controller. My ultimate goal is to add the following inside my head tags:

<noscript><meta http-equiv="refresh" content="5"></noscript>

I'm able to add the refresh meta tag itself using $headMeta->appendHttpEquiv(), but I have no idea how I can wrap that in the <noscript></noscript> tags. This only needs to be added to one page, but I don't want a separate layout file for this. I also want to use whatever methods and functions ZF2 have on offer (if any fit the bill). I've gone through the documented view helpers, but I can't find one that will help.

Any ideas?

1 Answer 1

4

You should be able to do this with the placeholder helper.

In your layout:

<html>
    <head>
        <?=$this->placeholder('customHead')?>
     [etc.]

Then in the view for the the page you want it on:

$this->placeholder('customHead')->set('<noscript><meta http-equiv="refresh" content="5"></noscript>');

change customHead to whatever name you want.

Edit: Yes, you can do this in a controller action instead:

public function someAction()
{
    $viewHelperManager = $this->getServiceLocator()->get('viewhelpermanager');
    $placeholder = $viewHelperManager->get('placeholder');
    $placeholder->getContainer('customHead')->set('<noscript><meta http-equiv="refresh" content="5"></noscript>');
}

if it's something you need to do in more than one place you might wish to inject the placeholder helper into the controller as a dependency.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Tim, works a charm. Marked as correct. Is there any way I can do this from the controller instead of the view though?

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.