4

I'm almost done writing a very simple module for zf2. One thing I'd like my module to do is to inject some css to the layout so that the HTML it generates displays in a nicer way.

Is this possible to do from within a module? If so, how?

EDIT: Thank you all for the prompt responses. However I think I probably didn't explain myself very clearly. When I say "inject some css" I mean taking a string of css and having it actually rendered INSIDE the layout. I didn't mean linking to an external css file or having an asset manager publish my files like the answers so far have suggested.

3
  • please provide us form code? Commented Nov 11, 2012 at 4:27
  • 1
    I'm not sure what you mean by form. I'm not trying to do a form at all. Commented Nov 11, 2012 at 4:36
  • Im on my phone right now, so I wont be able to give a Good anwer. But maybe an asset handler like assetic could be what you are looking for. Commented Nov 11, 2012 at 10:25

3 Answers 3

8

See Publishing assets from modules in Zend Framework 2 or How to merge Zend Framework 2 module public directories for discussion of the options you have for pushing public assets from a module.

And in addition to pushing your module assets to public, you could put the append into a triggered method like onBootstrap:

public function onBootstrap($e) {
    $sm = $e->getApplication()->getServiceManager();
    $headLink = $sm->get('viewhelpermanager')->get('headLink');
    $headLink->appendStylesheet('/assets/MyModule/css/mystylesheet.css');
}
Sign up to request clarification or add additional context in comments.

Comments

3

Try to use something like:

$sm = $this->getEvent()->getApplication()->getServiceManager();
$helper = $sm->get('viewhelpermanager')->get('headLink');
$helper->prependStylesheet('/css/mystylesheet.css');

in Your module controller.

EDIT:

If you want to store css style in module, You can either render it in Your layout file (head section) or, the better way, create another route in module, for example /get/style/[:name]. This route point to another action which returns only plain text/css document. More or less :)

Comments

1

Add a variable to your layout for where you'd like the CSS to be inserted:

<a href="#" style="<?php echo($this->CSS); ?>">Some Link</a>

Then in your Controller, load and assign it however you'd like:

$this->layout()->CSS = "CSS";
$this->layout()->CSS = $this->getRequest()->getPost('CSStoInject');
$this->layout()->CSS = fopen(), curl(), etc.

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.