Skip to main content
Participating Frequently
August 2, 2010
Answered

putting cf variables in non cf files...

  • August 2, 2010
  • 3 replies
  • 887 views

I had asked a similar question before, but this one is a bit different (and that one wasn't really answered completely anyway).  What I am trying to do is to read in a file that has cf variables in it (like '#VarName#'), and output it in the http response stream.

Basically, I am trying to create a themeable interface for my site.  So, in my CSS files, for things like colors and backgrounds, I want to place something where I can swap out that placeholder for a value.  This modified CSS content would be the only thing written to the stream, and altering the "content-type" header, it would be handled like a css file by the browser.

I though that if I used #'s to delimit variables, read the file, set the variables, and then use CFOUTPUT, I could do this.  Well, I guess I was wrong.  No errors were being thrown, and I was just getting the '#VarName#' directly in the output.

Then, I though about using a CFINCLUDE, and I used it in inline code, within a CFOUTPUT, and also a CFSAVECONTENT, but I was still just getting '#VarName#' in the output.

If possible, I want to stay away from having to do a global string replacement, but if that is the only way...

Does anyone have a way in which to do the task at hand?  I'll keep playing with it, and if I find something, I will let you all know...

    This topic has been closed for replies.
    Correct answer Reed_Powell-ttnmOb

    First, it would need to have a .CFM file extension and not .CSS, so that the CFML processor will look at it.  Second, the CFOUTPUT tags must be in the .CFM file itself - surrounding a CFINCLUDE with CFOUTPUTS doesn't mean that the CFOUTPUTs are in "effect" during the processing of the included file.  The included file must be a syntactically correct CFML file in its own right.

    Otherwise what you are doing should work.

    -reed

    3 replies

    Inspiring
    August 2, 2010

    Basically, I am trying to create a themeable interface for my site.  So, in my CSS files, for things like colors and backgrounds, I want to place something where I can swap out that placeholder for a value.  This modified CSS content would be the only thing written to the stream, and altering the "content-type" header, it would be handled like a css file by the browser.

    I though that if I used #'s to delimit variables, read the file, set the variables, and then use CFOUTPUT, I could do this.  Well, I guess I was wrong.  No errors were being thrown, and I was just getting the '#VarName#' directly in the output.

    The web server, by default, does not pass requests for CSS files to CF, so CF will never find out about said request, so none of the CFML in the CSS file will get processed by CF.  You can tell the webserver to pass requests for CSS files to CF, but then CF will need to process all requests for CSS files.  Not ideal.

    You could rename your dynamic CSS files to have .CFM extensions, but then your browser won't consider them cachable, so you will lose a lot of the benefit of using CSS files: that the browser caches them, therefore lowering the overhead of making a request.  I am fairly certain using a combination of headers and HTTP codes one could get get the browser to cache the file, though.  But then you have the reverse problem: you lose the dynamicism of the CF-driven CSS file.

    To be honest... I'd just have a bunch of themed CSS files and be done with it.  I don't imagine you will have so many combinations of themes that this becomes impractical?  Then CF handles the variable which identifies which CSS file to call in, given the current theme.

    --

    Adam

    Participating Frequently
    August 2, 2010

    thanks for the reply.  after playing around with a few simple examples, i kind of came across the same conclusion.  instead of using:

    #Variable#

    in the css file, i had to use:

    <CFOUTPUT>#Variable#</CFOUTPUT>

    i was hoping to keep this as simple as possible in the css file, but if using the <CFOUTPUT> tags makes it work, then i will just have to live with it.

    however, i was able to CFINCLUDE a css file.  it didn't give me any errors or anything.  now, it may not be good practice in including such files, but since it works, i don't see the reason in trying to make the task any more resource intensive.

    Inspiring
    August 2, 2010

    however, i was able to CFINCLUDE a css file.  it didn't give me any errors or anything.  now, it may not be good practice in including such files, but since it works, i don't see the reason in trying to make the task any more resource intensive.

    But then all you get is a bunch of CSS sitting in the middle of whatever other file included it.  How's that a good thing?  You want the CSS file to be served up via a <link>, not by embedding the CSS inline (like in a <style> block, which is how I guess you're doing it?)

    --

    Adam

    Participating Frequently
    August 2, 2010

    what i am doing is using a cf scipt to include the css file content (a query string variable is used to locate the file), do the variable replacement, and is being used as an @import in the pages main styling markup.  in the css, a line looks like:

    @import url('/cf/themer.cfm?css=/css/style.css');

    i was worried about the performance hit this might make, as i noticed via firebug's net utility that the css-containing files are grabbed each page refresh.  i might look into the headers to see if i can set one that makes the browser cache it.  for now, this isn't a huge deal, i am only making this for my personal site, and am really just trying to experiment with coldfusion.  i used to use asp.net (c#), and it allowed for theming of pages, so i was trying to recreate a system where i could have some sort of that functionality.

    i had thought about using style sheets for the themes that would override the ones for the 'default' theme, but ran into the problem of when i import custom tags.  some of my page components are made up into tags that i import, and they have their own css markup.  in order to make new style sheets, i would have to recreate a stylesheet for each of these tags, whereas i could just replace the parts of the originals that need changed.

    Reed_Powell-ttnmObCorrect answer
    Inspiring
    August 2, 2010

    First, it would need to have a .CFM file extension and not .CSS, so that the CFML processor will look at it.  Second, the CFOUTPUT tags must be in the .CFM file itself - surrounding a CFINCLUDE with CFOUTPUTS doesn't mean that the CFOUTPUTs are in "effect" during the processing of the included file.  The included file must be a syntactically correct CFML file in its own right.

    Otherwise what you are doing should work.

    -reed