Forum OpenACS Q&A: Arbitrary Template File Extensions

Collapse
Posted by Ralph Hempel on
I have been playing with OpenACS for about 2 weeks now and I'm very impressed so far. Stuff just works - and it's more or less well documented 😊

On my site, I have some .css files that are basically the same for each section, but with a few key values changed to allow for different colours in different sections.

Anyways, I figured that the templating system would allow me to somehow spec the .css extension for furhter parsing by the template system.

I know how to set the adp parser extensions in the site config.tcl file, but the ACS Templating seems to ignore this
setting.

I've also dug around in the ACS source in acs-templating and acs-tcl and found where the file-type/handler registration takes place. Adding css and restarting the server (and double checking the log to see that it happened) does not seem to do it either.

Is what I want to do even possible?

Ralph

Collapse
Posted by Ralph Hempel on
I realize now that I'm not being very clear in what I'm asking. Here's what I want to do...

  www.myserver.com/section/css/screen.css

  should be processed by the template system. There
  is a corresponding screen.tcl file that sets up the
  data sources. In other words, screen.css should be
  processed as if it was screen.adp

Yes, I could make a file called screen.adp and just ask for:

  www.myserver.com/section/css/screen

but that's not what I want to do.

Collapse
Posted by Dirk Gomez on
Why do you want to do this? Isn't it widely accept that a file with the extention .css is a css file, whereas (less widely accepted) an .adp file is an adp file?
Collapse
Posted by Ralph Hempel on
Well, if a user asks for the file:

  www.myserver.com/section/index

and the index file links to:

  www.myserver.com/section/css/screen.css

I want the css file to pass through the templating system so that it can have certain values for colour or formatting updated by the template system.

Maybe I'm missing something really obvious here, but how else can I do this besides forcing OpenACS to process my css file as if it was an adp file?

Collapse
Posted by Dirk Gomez on

Why don't you put a such a line into your respective adp file?:

<link rel="stylesheet" type="text/css" href="screen.css">:

That should do it for you

Collapse
Posted by Ralph Hempel on
I think the problem with your method is that the screen.css will not be processed by the template system...

I know how to link to stylesheets, what I want to do is have the stylesheet pass through the template system.

Am I missing something glaringly obvious?

Collapse
Posted by Vinod Kurup on
Hi Ralph,

I'm still not 100% clear what you're trying to do, but let me give it a shot.

You have <p class="foo"> in your index.adp file. You want the "foo" class to be blue in some circumstances and orange in other circumstances. So when index.adp links out to screen.css, you want screen.css to be built dynamically with the "foo" class set to the proper color.

I'm not sure this is the best way to handle it. Don't browsers cache css files? If so, then your sites might not look right. I think a better way would be to change the class name in the index file and have extra classes defined in your css file for the various sections. so <p class="foo-1"> for section 1 and <p class="foo-2"> for section 2, then define each of those classes in the css file.

Of course, I may be completely misinterpreting your question, in which case it might be best to re-explain and show the source of each of the files in question.

Collapse
Posted by Ralph Hempel on
Vinod,

I guess in general, what I want to have is a way of telling OpenACS to use extension "xyz" in exactly the same way as if it was an "adp" file.

The browser asks for "screen.css" and my (ideal) OpenACS server says, hmmm, I'd better run this through the template system and return screen.css with all of the variables set to the new values.

This part of your description is almost right...

You have <p class="foo"> in your index.adp file. You want the "foo" class to be blue in some circumstances and orange in other circumstances. So when index.adp links out to screen.css, you want screen.css to be built dynamically with the "foo" class set to the proper color.

In fact, I'll have one set of pages on my website where foo is blue, and another set of pages where foo is orange. They don;t change on a single page. Depending on where in the tree the index.adp file is, it will link in a different screen.css file with the colour attribute set appropriately.

Being a lazy programmer, and realizing that my css files are almost identical, and not wanting to synchronize them, I through that passing them through the template system might do what I want.

I _could_ just write up a tcl script that uses a master screen.css file and writes out the site dependent screen.css files as needed....

Collapse
Posted by Dave Bauer on
Ralph,

Still I don't see the complication here :)

The whole point of CSS is that a class would be universally defined for your site. So if you wanted eventhing in class="foo" to be bold and red, you would define it as so.

You can either define new CSS files for different sections or if the part that changes is very small, define it in an html <style> tag and put that in an adp include file.

I don't think having a dynamically processed css file is going to be the best solution here.

Collapse
Posted by Ralph Hempel on
Dave wrote:

"The whole point of CSS is that a class would be universally defined for your site."

Exactly - except I want blue paragraphs in one part of my site and orange in another. And if I move the source files from one part of my site to the other, I don't want to have to change the class names.

I think I'll succumb to the general weight of advice here and not try and process the css files through the template system. I'll generate them another way. They all have the same source, the same tags and attributes. Only some of the attribute values change between the css files in different parts of the site.

It was soooo tempting to make the changes happen automagically.....

Thanks for all the advice so far.

Collapse
Posted by Mat Kovach on
Of course, you could put something in the master templated that picks the css files based on site_id, or package_id, etc.

Add something like:

in the master .tcl

set package_id [ad_conn package_id]

if { css file doe snot exists for package_id }
  set css_file "default.css"
else
  set css_file "package_id.css"

then in master.adp

<link href="/templates/@css_file@" rel="sytlesheet" type="text/css">

You would just have to logic for the decieding on which css file to use.

Collapse
Posted by Ralph Hempel on
Yep, that will work just fine. And I could even generate the css file dynamically and make sure it's in the file system before the current page is returned.

That way the browser can fetch it (as needed) and the css file will have automatically generated values.

I could also just leave the CSS file as a static file....

Collapse
Posted by Jarkko Laine on
Maybe you can even make a vuh file that is being called in Mat's solution instead of static css files (in case you really want to create the css just-in-time).

It could then do whatever smart logic you want it to do.

Collapse
Posted by Raad Al-Rawi on
Ralph - just to confirm what has already been said - I looked at the same issue a while ago and came to the conclusion that it can't be done.

I decided to set up different css files and switch them in the master template.

I did have one thought though: you could redefine specific styles dynamically in the master template and include them in the html file. That way they *might* override the css defined ones. I'm not a css expert so this might not work, but it's a thought :)

<Raad>
Collapse
Posted by Dave Bauer on
Hmm, after having this in the back of my mind I have the ideal solution, but it will require some work.

Store the CSS attributes in the database. Then you can manage them in one place, but still use multiple CSS files with almos t the same contents. Using the content repository you could build up the CSS file and publish it to the filesystem.

This won't work without some code, and any CMS effort we work on should include at least some support for CSS files.

Collapse
Posted by Ralph Hempel on
This won't work without some code, and any CMS effort we work on should include at least some support for CSS files.

Dave, it's clear that leaving stuff in the back of your mind helps you understand the problem 😊

I think what you are suggesting will work, but I'm not sure that the support has to be specific to css files. The css is really just a special case of the general problem of generating page contents based on location in a file system.

That being said, it _would_ be nice to have a UI that allows one to browse attribute/value pairs and assign them based on location in the published file tree.

ie, if you're asking for a css file for a file being published to foo, then make the P tags orange. If the file is published to bar, make them blue, and so on.

Ralph

Collapse
Posted by xx xx on
Very interesting discussion. It is useful to the TIP on CSS for openACS 5.0 too. So if there are any suggestions that result from this discussion, please report them here: https://openacs.org/forums/message-view?message_id=116041

I think using "<style><include src=css-file-with variables></style>" remains the easiest way to go. It doesn't look good in the html-source, but is that bad?

I'm not sure how browsers handle  <link> or @import with CSS-files, but would caching of css-files become a problem if they were dynamically created with the same name, as Vinod suggested?