views:

78

answers:

2

OK, I'm working with converting some very basic PHP to port to a dev server where the client only has CF. Ive never worked with it, and I just need to know how to port a couple things:

<?php
      $pageTitle = 'The City That Works';
      $mainCSSURL = 'header_url=../images/banner-home.jpg&amp;second_color=484848&amp;primary_color=333&amp;link_color=09c&amp;sidebar_color=f2f2f2';
      require('includes/header-inc.php');
?>

I know:

<cfinclude template="includes/header-inc.cfm">

but how to i get the var to be passed to the include and then how do I use it on the subsequent included file?

Also in my CSS (main.php) I have (at the top):

<?php
    header('Content-type: text/css');
    foreach($_GET as $css_property => $css_value) {define(strtoupper($css_property),$css_value);}
?>

and im using those constants like this:

#main-content a {color:#<?= LINK_COLOR ?>;}

How can I get that to work also with CF?

Never thought I'd be working with CF :)

+3  A: 

Coldfusion has a number of variable scopes that have different levels of visibility.

I'm not too familiar with PHP, but I'm guessing those variable declarations are available to any code in the request?

The equivalent of this is the 'request' scope.

Any variables set in the request scope are available to any code in the processing of the request.

To set a variable in the request scope, you simply do:

<cfset request.myVariable = myValue>

Or, in cfscript:

request.myVariable = myValue;

The other most commonly used scope is the 'variables' scope. This is the default scope if you don't specify a scope, so:

<cfset myVariable = myValue>

is equivalent to

<cfset variables.myVariable = myValue>

The variables scope is visible to code included with CFInclude, so in your specific case, you could use the variables scope or the request scope.

Here's a reference to the Scopes in CF:

http://livedocs.adobe.com/coldfusion/8/htmldocs/Variables_30.html

Edward M Smith
So far, i think thats working, but how do I then use the variable? $var = 'cat'; echo $var; would spit out cat. How do I get "pageTitle" to print out?
Oscar Godson
inside includes/header-inc.cfm: <cfoutput>#pageTitle#</cfoutput>
Henry
<cfset pageTitle = 'The City That Works'>
Henry
or, in cfscript:writeOutput(pageTitle);
Edward M Smith
Awesome you rock, last questions though, how do i do a dynamic CSS file like the one above? How do I change the header to CSS? I think I can use Nick's example below for the looping and settings vars, right?
Oscar Godson
Output a text/css mime type? <cfcontent type="text/css"> sets the appropriate mime type header.This might be helpful: http://ajlcom.instantspot.com/blog/2006/8/1/Dynamic-CSS-with-ColdFusion
Edward M Smith
+2  A: 

In CF the $_GET array becomes the url struct. To loop through it you use cfloop:

http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_j-l_16.html#2393950

<cfoutput>
<ul>
<cfloop collection = #url# item = "key">
 <li>#key# = #url[key]#</li>
</cfloop>
</ul>
</cfoutput>
Nick
Perfect, working, but one issue, why is it outputting? I just have this: <cfloop collection=#url# item="key"> #key# = #url[key]#</cfloop> (which works except its outputting #key# = ... etc) for each GET param
Oscar Godson
Also, is there an easier way to output <cfoutput>#link_color#</cfoutput>? Like a shorter way?
Oscar Godson
wait, nvm, it automatically takes the URL?! attr? Seems dangerous ;) but im just using it for CSS
Oscar Godson
<cfouput> can wrap around the whole page, and #link_color# #var2# #var3# will be evaluated.Yes they'll all be in URL struct, dangerous how? :) Take care of what you output, see htmlEditFormat().
Henry
That code is for outputting them. If you need to do manipulation instead, use them inside, like <cfset url[key] = ucase(url[key])> , for example.
Henry