Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

General Questions /

General questions about getting started with SilverStripe that don't fit in any of the categories above.

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Requirements::customCSS Not working [solved] (SS 3.1.5)


Go to End


9 Posts   2551 Views

Avatar
digibrains

Community Member, 130 Posts

9 November 2014 at 11:41am

Edited: 09/11/2014 11:43am

I have

class Page_Controller extends ContentController {

    public function init() {
        parent::init();
        Requirements::customCSS(<<<CSS
            #FaderCell1 {
                background-image: url($BannerImg1.URL);
            }
            #FaderCell2 {
                background-image: url($BannerImg2.URL);
            }
            #FaderCell3 {
                background-image: url($BannerImg3.URL);
            }
            CSS
        );
    }

}

I've even copied/pasted directly out of the docs example and this still results in this error:

Parse error: syntax error, unexpected end of file in /var/www/html/mydomain/mysite/code/Page.php

http://doc.silverstripe.org/framework/en/reference/requirements#custom-inline-scripts

Avatar
swaiba

Forum Moderator, 1899 Posts

11 November 2014 at 3:44am

Are you sure "CSS" is on a line by itself with no white space either side? That is all I can think of...

Avatar
digibrains

Community Member, 130 Posts

11 November 2014 at 6:36am

Edited: 11/11/2014 6:38am

Thanks, swaiba!

I didn't realize heredoc syntax required that. That solved that problem, but now I'm getting this:

[Notice] Undefined variable: BannerImg1.URL

Any ideas?

Thanks, again,
Chris

EDIT: And yes...I'm sure that variable exists. If I create a style block in my template it works fine.

Avatar
kinglozzer

Community Member, 187 Posts

14 November 2014 at 6:06am

Edited: 14/11/2014 6:07am

Hi Prawnstar,

The $Variable.Method syntax only works in templates, so that won’t work. You’ll need to do something like $this->BannerImg1()->getURL()

Loz

Avatar
digibrains

Community Member, 130 Posts

14 November 2014 at 6:25am

Loz,

Thank you so much! Makes perfect sense, I should have realized that.

Best,
Chris

Avatar
digibrains

Community Member, 130 Posts

14 November 2014 at 7:04am

Grrr...Spoke too soon.

That actually doesn't work. According to this:
http://doc.silverstripe.org/framework/en/reference/requirements#custom-inline-scripts

Even a direct copy paste of that example code and changing the vars to match my image doesn't work.

SilverStripe just throws me the middle finger

[Notice] Undefined variable: BannerImg1

The var alone should work, but doesn't. Does anybody have an example of working code?

Avatar
digibrains

Community Member, 130 Posts

15 November 2014 at 10:31am

Edited: 15/11/2014 10:33am

OK, I'm convinced this is a bug unless someone can show me their working code. Using SS 3.1

I just created a fresh install of SS and this is my Page.php:

<?php
class Page extends SiteTree {

	private static $db = array(
	);

	private static $has_one = array(
        'MyImg' => 'Image'
	);

	public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', new UploadField('MyImg'), 'Content');
        return $fields;
    }

}
class Page_Controller extends ContentController {

	private static $allowed_actions = array (
	);

	public function init() {
		parent::init();
Requirements::customCSS(<<<CSS
    .test {
        background-image: url($MyImg);
    }
CSS
);
	}

}

...according to the docs, this is correct.

After adding an image to my page, then viewing source the generated source looks like this:

<style type="text/css">
    .test {
        background-image: url();
    }
</style>

I've tried Loz's suggestion in my controller:

$this->$MyImg()->getURL()

which results in this, in my generated code:

background-image: url(Page_Controller->()->getURL());

I also tried this:

$this->$MyImg

Which resulted in this:

background-image: url(Page_Controller->);

Any thoughts?

Avatar
martimiz

Forum Moderator, 1391 Posts

18 November 2014 at 4:00am

That would be obvious, as $MyImg is never created:

   public function init() {
      parent::init();
Requirements::customCSS(<<<CSS
.test {
background-image: url($MyImg);
}
CSS
);
   } 

You'd at least need to add:

$MyImg = $this->BannerImg1()->getURL();

Go to Top