Changing the filenames in Flex Builder html templates

Suppose you want the main HTML file that wraps your Flash app to be deployed as index.aspx instead of index.html. It’s a little trickier than you might think.

If you look in the html-template directory of your project, you will see that the main file is called index.template.html. This is a special name — Flex Builder recognizes filenameit, and says “Oh I know what that is,” and uses it to create MyApp.html and MyApp-debug.html in the bin folder.

But if you just rename it to index.template.aspx, you’ll have problems. Notice that after doing that, and then cleaning your project, you now have only index.aspx in the bin folder. There are two issues with that: For one thing, the app name isn’t part of the , which cause trouble if you have more than one app in the project; and also, it doesn’t differentiate between the debug and release versions of the file. If you examine it, you’ll see that it contains a reference to MyApp-debug.swf. Where did the release one go?

The answer is that since the file no longer has the special index.template.html name, Flex Builder doesn’t know what to do with it. It does see the “.template” part of the name, and it uses that to decide that it should do macro-substutition within the file; but beyond that, it does nothing more.

And since there are two SWFs (release and debug), Flex Builder ends up processing your file twice. First, it creates the release version of index.aspx, then it creates the debug version of the same file, overwriting the release version. (It probably should report an error in this case, but it doesn’t.)

But the solution, it turns out, is very easy (although not at all obvious): Give the file the rather cryptic name of…

${application}${build_suffix}.template.aspx

That does the trick. The ${application} and ${build_suffix} macros are substituted when creating the filename that goes into the bin directory (build_suffix is “” for the release build and “-debug” for the debug build); and “.template” causes Flex Builder to do macro substitution inside the file.

To wrap up:

  • Any file in the html-template directory can have macros in the filename. Substitution will be performed when using those template files to the create the files in the bin directory.
  • In addition, if any filename contains .template (either at the very end, or followed by a punctuation character such as “.”), then the contents of that file will have macro substitution performed, and the .template part will be removed from the filename when copying to the bin directory.
  • Finally, the name index.template.html is a special case, and is essentially equivalent to ${application}${build_suffix}.template.html.

So, what macros are available? ${project}, ${application}, ${version_major}, ${version_minor}, ${version_revision}, ${build_suffix}, ${swf}, ${bgcolor}, ${width}, ${height}, ${title}.

36 Comments so far

  1. Aaron Leavitt on December 11th, 2006

    Great information, and extremely useful. Do you have any more info on the macro keywords and their use. I may be dense but I’ve had a terrible time trying to sort out where the ${bgcolor} info that is placed in the HTML file comes from.

  2. mike on December 11th, 2006

    ${bgcolor} comes from the “backgroundColor” attribute of the <mx:Application> tag. Or, if it is a pure-ActionScript project, from the [SWF] attribute immediately before the main application class, e.g.

    [SWF(backgroundColor="#ff0000")]
    public class MyApp extends Sprite.

  3. Ben on December 11th, 2006

    Hi Mike, great post. I was actually wondering about this topic just the other day. How do we set the ${version_major}, ${version_minor}, and ${version_revision} values?

    Thanks.

  4. mike on December 11th, 2006

    You set the ${version_…} values by going to the Flex Compiler or ActionScript Compiler tab of the Project Properties dialog.

  5. David Coletta on December 12th, 2006

    A bit off topic, but your post reminded me that I was wondering if there were a way to do the wrapper page processing from the command line instead of from within Flex Builder. I needed this recently when writing a daily build script for our app. One of the things I had to do to get it to work was check in the generated wrapper page, because I didn’t know of any way to build it from the command line.

  6. mike on December 12th, 2006

    Sorry David, I don’t know of a way to do that. But check if this might help — it appears to show how to make an Ant script tell Eclipse to do its normal Eclipse-style build. If that works for you, then great. Note that that would do the whole build, not just the HTML-template part.

  7. Raymond Camden on January 22nd, 2007

    Where does the {$title} value get set?

  8. mike on January 22nd, 2007

    ${title} gets set to the value of the "pageTitle" attribute of the <mx:Application> tag — or, if that is not set, then to the filename of the main application file (minus the ".mxml" part).

  9. Raymond Camden on January 24th, 2007

    Thanks Mike. Worked like a charm. May I suggest you make a blog entry _just_ on these tokens? Would be useful.

  10. mike on January 24th, 2007

    Good idea Raymond.

  11. [...] In a post last month, I briefly mentioned the list of macro substitutions that are available in the files in the html-template folder. Here, I’ll offer a little more information on those. I’ll try to make sure this makes it into the next version of the documentation. [...]

  12. Matt Horn on January 24th, 2007

    In response to Dave Coletta’s question: There is now an html-wrapper custom Ant task that will generate wrappers and supporting files. It’s available on labs:

    http://labs.adobe.com/wiki/index.php/Flex_Ant_Tasks

    hth,
    matt horn
    flex docs

  13. mike on January 24th, 2007

    Whoa, cool, I didn’t know about that. Thanks for the link, Matt!

  14. [...] As I recently blogged, one of my goals for the year is to learn how to use Ant with Flex. I had asked a question on Mike Morearty’s blog some time back about how to build the index.html wrapper page for a Flex application from the command line, since I was working on a nightly build script. He just pinged me to let me know that there’s an Ant task to do that. Cool! And there’s also a wiki page at Adobe Labs that documents how to use Ant with Flex. Extra cool! Now if I can just find some time to read it… [...]

  15. hua on February 7th, 2007

    Mike, your method works great.
    however, after i change index.template.html to ${application}${build_suffix}.template.cfm (i am using coldfusion), Flex Builder still launches index.html file in brower, which gets me 404 error. any way to make it launch index.cfm? thanks

  16. mike on February 7th, 2007

    hua,

    You need to change the URL that is used to launch. See http://www.morearty.com/blog/2006/05/23/flex-builder-ctrl-click-to-access-a-lanch-configs-properties/ for instructions on how to get to the dialog. Then, un-check the box for using the default URLs, and then change both the "Debug" and "Run" launch URLs.

  17. hua on February 8th, 2007

    thanks Mike, that works great.

  18. Todd Clare on March 7th, 2007

    Hey Mike,

    Related to your reply to Hua… For startup time and screen real estate, I find myself changing the "URL or path to launch" from the defaults (running the [MyApp].html and [MyApp]-debug.html) to just run the associated SWF files in the standalone player/debug player (so I change the values to [MyApp].swf and [MyApp]-debug.swf)

    Any idea if there is a configuration file or preference I can change (using the Eclipse plugin) to set these AS the defaults? I’d LIKE to be able to set it globally so that a NEW project already has these as the defaults when I create it. Right now I RUN the app, kill it, edit the values and then rerun / debug it. It’s getting to be a small but irritating step.

    Thanks!

    – Todd

    – TC

  19. mike on March 7th, 2007

    Hi Todd,

    Sorry, but there is no built-in way to automatically change the default launch URLs as you suggest.

    One way to do this, if you have sufficient knowledge of Eclipse, would be to write your own plugin that listens for the creation of new launch configurations, and then modifies them. But that would take quite a bit of work.

  20. Todd Clare on March 7th, 2007

    Yep, that’s what I figured. Nope to both knowledge and time… think I can spare the 10 seconds pre new app and do it manually : )

  21. [...] general info go here, and to see what macros are available go [...]

  22. Ruben Swieringa on June 3rd, 2007

    Thanks man, I’ve got the feeling that someday I’m going to be thankful for bookmarking this one..

  23. [...] a post by Neil Webb I bumped into a post by Mike Morearty in which he talked about changing the index.template.html file that Flex Builder automatically [...]

  24. Ben on November 23rd, 2007

    This is quick a bit off topic, but I’m working with the new Flex Builder plug-in for Eclipse, and am creating an ActionScript project, which means I don’t have a MXML file to set all the fancy attributes of the application, such as the background color, page title, etc.

    I’m lost, and have no idea where and how I can do this with an ActionScript only project. Any ideas?

  25. mike on November 26th, 2007
  26. Ben on November 28th, 2007

    Thanks Mike, my confusion was with where to put the [swf()] declaration. The link in your response made that clear, and I got everything working. Thanks!

  27. [...] is again poorly documented by Adobe, but Renaun Erickson’s posting together with Mike Morearty who nicely documents the marco’s. If you’ve got this far, you need to consider added [...]

  28. [...] morearty.com/blog … First Post [...]

  29. [...] can also read this post by Mike Morearty on “ Changing the filenames in Flex Builder html templates” which will provide some more insight into the [...]

  30. Nikhil Lanjewar on June 8th, 2008

    Hi mike..
    really nice post about the index-html.template! :)
    Lately, I’ve been facing problems running my application in full screen mode in a web browser. It works like a charm in the Adobe Flash player 9 but I get an Error 2152: SecurityError: Full sreen mode not allowed when I try to change the .displayStageSet to FULL_SCREEN.I’ve been trying to figure out a solution to my problem and finally I reached this blog of yours. But I still can’t make out if is there any way of solving it. What exactly do I change in the index-html.template file to get rid of it?

  31. mike on June 8th, 2008

    Nikhil, I’m not sure, please try asking on the "flexcoders" mailing list on Yahoo Groups.

  32. Dabbo on June 12th, 2008

    What if you want your generated template to be named index.html?

  33. Sajain Geevar on May 15th, 2009

    Post as well as comments, very informative.

  34. jurito on September 7th, 2009

    hello there, I just moved from flashdevelop to flex builder and i’m finding some things a little bit rigid actually.
    Though I found this post (as others on your blog) really useful, i still couldn’t manage to do what i need.
    Specifically, i would need to create a custon html template with swfaddress, swfobject and swffit.
    I did that, but i can’t find a way to:

    - choose a custom name (and path, let’s say "/swf/") for the exported swf
    - avoiding that flex exports an *application*.html, as i’d need just an "index.html", customized with the wildcards.

    I suppose these are quite simple tasks but i actually couldn’t find any info on that.

    anyone can help?

  35. mike on September 7th, 2009

    jurito,

    > choose a custom name (and path, let’s say "/swf/") for the exported swf

    You can set the directory into which all generated files are written, by going to Project > Properties, Flex Build Path, and setting "Output folder"; put any directory name you want there. But I don’t think that’s what you are asking for — it sounds like you want a different directory hierarchy under bin-debug, e.g.

    ..bin-debug/
    ….swf/
    ……myapp.swf
    ….index.html

    I don’t think we provide a way to put the swf under a subdirectory of the output directory. If that’s what you mean, please log an enhancement request at http://bugs.adobe.com/flex — thanks!

    > avoiding that flex exports an *application*.html, as i’d need just an "index.html", customized with the wildcards.

    We don’t provide a way to do that for "index.html", but you can do it for any other filename, such as "index.htm". E.g. rename index.template.html to index.template.htm. Again, please file an enhancement request if you’d like this changed.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word