Use RapidWeaver's %pathto()% syntax in javascript files

UPDATE (2009-07-17): We have updated the code to account for those who were having trouble understanding the reg-ex nature of rTrim. The new code and instructions should make it harder to mess up your path.

filetype_jsIt may take me a while sometimes, but there is one simple truth about my workflow, I always strive to have the most efficient way of getting things done. This is especially true in RapidWeaver theme development. Developing themes for RapidWeaver is not like standard web design. You have no way of knowing how each user will structure their site, what plugins they’ll use, what they’ll add to their pages… All of these uncertanties leave us as developers scratching our heads sometimes, trying to sort out ways of dynamically including things and stuff without the end user being able to break it.

PHP would be the obvious choice here, but we don’t have the luxury of knowing what the end users hosting environment will be and whether they could, or even would be willing to support a host of PHP functions. So the next best thing is javascript; yes it’s a little slower and it does depending on the end user having javascript enabled, but the ubiquity of javascript has never been as great as it is today.

One thing that has plagued RapidWeaver developers for some time is how to utilize RapidWeaver’s %pathto()% syntax, the bit of code that allows RapidWeaver to imprint the end users chosen common file path, in any other file other than the index.html template file. For any javascript file that has required the inclusion of another file, we’ve previously had to fill our html with scripted variables for each file, each using the %pathto()% syntax and so forth until we have built a separate path to each file from outside our javascript that actually requires it. Since RapidWeaver will only write the path to files it can actually find, you need to do this for each and every file.

Today I had an AH-HA moment while in need of multiple paths to be defined for use in various javascript files. I decide to write a small bit of code that grabs RapidWeaver’s %pathto()% syntax while it chases a real file. Then I strip out the actual file name, and KABLOW! you have a common file path in the the form of a javascript variable, ready to be used in any javascript file you like.

Get the source code.
Get the code.
Get the minimized code.