Jump to content

Output CMS data to a .css file


a-ok
 Share

Recommended Posts

Pretty sure this is a daft question but thought I’d ask anyway!

Is it possible to include data from PW into a .css file? I have a client adding some webfonts via the admin but don’t want them to be added as <style></style> tags and rather within a separate .css file? I basically want to make finding the webfonts a little harder!

Any thoughts?

Link to comment
Share on other sites

2 hours ago, a-ok said:

I basically want to make finding the webfonts a little harder!

I have my doubts about how effective that strategy will be, but you have a few options:

1. Create a PW template and page that outputs the CSS. Use a "text/css" content-type header, either using the "Content-Type" select on the Files tab of the template, or by using header().

header('content-type: text/css; charset: UTF-8');

Add this page to your markup in a <link> tag:

<link rel="stylesheet" type="text/css" href="/path/to/page/">

2. Similar to the above, but rather than create a template and page instead create a PHP file in the site root (PHP files are blocked inside /site/) that bootstraps ProcessWire.

3. Use $files->filePutContents() to actually write a CSS file that you load in your markup.

In all of these cases you will probably want to use a cache-busting query string based on the time the CSS was last modified.

  • Like 4
Link to comment
Share on other sites

I added a module for this that hooks on page save.  Added an ace css text field for color coded markup and when I save, if that page has a css field, it creates the .css file on the server using a timestamp name. 

public function init() {
		$this->pages->addHookBefore('save', $this, 'updateThisPage'); 
		$this->pages->addHookAfter('save', $this, 'makeMergeFiles'); 
	}

and something like

$myfile = fopen('../assets/css/' . $newAssetFilename . '.css', "w");
fwrite($myfile, $allCSS);
fclose($myfile);

For some reason if I move a page it doesnt work right.  Have to save it to fix, but it hasnt bothered me enough to research it. lol

I usually add one to my template page, and the individual page and it finds all pages with this field and merges everything into one file.  I do one for js also.

  • Like 1
Link to comment
Share on other sites

11 hours ago, Robin S said:

In all of these cases you will probably want to use a cache-busting query string based on the time the CSS was last modified.

Thanks so much, @Robin S ? Can I ask what you mean by the quoted text? Are you saying that if it's updated regularly then it'll need to use a cache-busting query string?

Thanks @joer80 also! This is also a great idea!

Link to comment
Share on other sites

8 hours ago, a-ok said:

Can I ask what you mean by the quoted text?

You want browsers to reload the CSS any time that it changes, instead of going to the browser cache. Appending a query string to the CSS URL that changes every time the CSS changes is a reliable way to achieve this.

You can see an example of this approach in the PW admin, where the file modified timestamp is used in the query string:

2020-04-27_094331.png.410d15dd3378fc70863b80db8c183628.png

  • Like 3
Link to comment
Share on other sites

  • 2 weeks later...
On 4/26/2020 at 2:31 AM, Robin S said:

I have my doubts about how effective that strategy will be, but you have a few options:

1. Create a PW template and page that outputs the CSS. Use a "text/css" content-type header, either using the "Content-Type" select on the Files tab of the template, or by using header().


header('content-type: text/css; charset: UTF-8');

Add this page to your markup in a <link> tag:


<link rel="stylesheet" type="text/css" href="/path/to/page/">

2. Similar to the above, but rather than create a template and page instead create a PHP file in the site root (PHP files are blocked inside /site/) that bootstraps ProcessWire.

3. Use $files->filePutContents() to actually write a CSS file that you load in your markup.

In all of these cases you will probably want to use a cache-busting query string based on the time the CSS was last modified.

I'm sorry it's taken me ages to get back to this!

Thanks so much @Robin S – I went with the first option but I'm unsure whether the third option is the best. Any preference from your end?

 

 

Link to comment
Share on other sites

9 hours ago, a-ok said:

I went with the first option but I'm unsure whether the third option is the best. Any preference from your end?

Third option creates a static file so it can be cached by the browser, but the first option could also create a static file if you are using ProCache. Probably not much impact no matter which option you use.

Link to comment
Share on other sites

  • 3 weeks later...

Just back to this, sorry.

I've set up a template file which is outputting the CSS and all seems okay on that front BUT the file itself seems to take an age to load and thus makes the load time of the whole site super slow.

Any thoughts if I'm implementing this wrong?

Screenshot 2020-05-28 at 10.28.55.jpg

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...