tags:

views:

410

answers:

8

Does ColdFusion automatically generate the basic HTML tags like:

   <HTML> <head></head> <body></body> </HTML>

Or do you have to include that in your source code like ASP or PHP?

+1  A: 

Include it in your source. Even if it did add them automatically (which it doesn't) you nearly always need more control (ie, setting up your meta tags).

BTW, you don't even need those tags. The following snippet should display correctly in most browsers.

<div> HI </div>
SpliFF
Sorry for asking such a question but I'm quite confused with the bewildering amount of tags that CF supports, but is CF like PHP where you can simply write your HTML outside some opening tag like "<?php" or do you have to explicitly "flush()" it ??
Jenko
there is no need to flush CF unless you are trying to improve performance for certain pages (like flushing the headers so extrnal documents start loading sooner).
SpliFF
@jeremy: to answer you're question, yes, it is like PHP in that respect. CF tags are processed 'out' of the HTML before it is sent to the browser.
Ciaran Archer
No, you _do_ need html, head and body tags. When you walk across the street you do not need to pay attention to traffic lights, they certainly don't stop you from walking. See what I mean?
shylent
shylent, with HTML5 you can legally omit a lot of the tags - html, head and body in particular become optional (both opening and closing) once you specify <!DOCTYPE html> as the first content of the file.
Peter Boughton
+3  A: 

I personally would not want ColdFusion to include those tags, as I am not always outputting HTML.

Also, unlike PHP, there is no <?coldfusion> tag, or anything similar that you use to declare that CF should process your code. All ColdFusion tags are prefixed with 'cf', and can be interspersed with regular HTML code. Keep in mind that in order to render output to the page, you must use the '<cfoutput>' tag. ie.

<cfset variables.firstName = 'My Name' /><html><head><title>ColdFusion Sample</title><head><body>Hello <cfoutput>#variables.firstName#</cfoutput></body></html>
Ryan McIlmoyl
+2  A: 

Simplest way I can think of explaing it is:

CFML code retrieves, calculates and displays information inside an HTML document.

So, if you're using a framework of any kind, the header include file on each page would contain the..

<HTML> <head></head><body>

and the footer page would include the ..

</body> </HTML>

Of course, like anything, if for some reason you wanted to, you could program something to output these as well for you.

I've found CFML to be the easiest syntax to stay readable inside HTML, and it's not a big deal to leave it that way.

Hope that helps.

Jas Panesar
+1  A: 

You can create a custom tag to do this for you.

For example, in each page you wanted your standard template, you could use something like:

<cf_page
    title  = "MyTitle"
    styles = "base.css,form.css,theme.css"
    >

    Main Page Content

</cf_page>


And then to make that work, create a page.cfm which looks similar to this:

<cfswitch expression="#ThisTag.ExecutionMode#">

    <cfcase value="START">

     <cfcontent reset/><cfoutput><!DOCTYPE html>
<html>
    <head>
     <title>#Attributes.Title#</title>

     <cfif StructKeyExists(Attributes,'Styles')>
     <cfloop index="CurStyle" list="#Attributes.Styles#">
      <link rel="stylesheet" href="#CurStyle#"/>
     </cfloop>
     </cfif>

     <cfif StructKeyExists(Attributes,'Scripts')>
      <cfloop index="CurScript" list="#Attributes.Scripts#">
       <script type="text/javascript" src="#CurScript#"></script>
      </cfloop>
     </cfif>

     <cfif StructKeyExists(Attributes,'HeadContent')>#Attributes.HeadContent#</cfif>
    </head>
    <body>
</cfoutput>

    </cfcase>

    <cfcase value="END">
     <cfoutput>
     </body>
</html></cfoutput>
    </cfcase>

</cfswitch>
Peter Boughton
A: 

To answer your question, yes, you need to include the markup in the page.

As other posters have pointed out, it may be something other than HTML you'd like to output, such as a PDF document or JSON. CF makes that sort of stuff very simple which is part of it's attraction.

Ciaran Archer
+1  A: 

Alternatively, go for a basic templating setup and have (meta.cfm,) header.cfm, sidebar.cfm, footer.cfm and use the <cfinclude template=""> tag on each page. Even better than that, use a framework to manage all of this via the controller, which would then make the output of content in different formats simpler.

Alistair Knock
+1  A: 

No, Coldfusion will not include anything that you don't tell it to.

For a general page that needs an HTML/BODY tag (i.e. not a chunk of code to be pasted in) you could do any number of custom functions/tags to output HTML/BODY tags, but overall the simplest is most likely to just type them in.

Now, you could use some of the header/footer code to stick them in automatically, but that can lead to the need for more logic to exclude them under certain circumstances. Which may be overkill if you are looking for something simple.

Note, there are some widgets the CF provides that will generate SOME html. So, for example CFTABLE will result in a TABLE tag at some level. But this is primarily a way to simplify table creation whereas HTML/BODY can't get much simpler.

Tom Hubbard
+1  A: 

If... and this is a big IF... if you don't mind every request having the opening and closing HTML tags, you can add them to your Application.cfm/OnRequestEnd.cfm or Application.cfc.

Application.cfm:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

<html>
<head>
    <title>Page title here</title>
</head>
<body>

OnRequestEnd.cfm:

</body>
</html>

Or Application.cfc - OnRequest():

<cffunction name="OnRequest" output="true" returntype="void">

    <cfargument name="targetPage" type="string" required="true">

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;

    <html>
    <head>
     <title>Page title here</title>
    </head>
    <body>
     <cfinclude template="#ARGUMENTS.targetPage#">
    </body>
    </html>

</cffunction>

No, it's not the best way because it's very limiting, but it is another way! :OD

Adrian Lynch