Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2005-10-23 14:20:27

Chris
Member
From: Bristol, UK
Registered: 2004-02-26
Posts: 22
Website

manage javascript files with TXP

This is not so much a ‘How Do I…?’, more of a ‘How I’m Doing It…’ but I’d like to know if anyone out there has a better method of managing/editing their javascript files through the TXP interface. Anyway, to the nitty gritty…

TXP handles CSS beautifully and I wanted something similar for javascript. The simplest method I could come up with was to make a copy of <code>/textpattern/css.php</code> and rename it to <code>/textpattern/js.php</code>. Then I opened up <code>/textpattern/js.php</code> and changed:

<pre><code>header(‘Content-type: text/css’);</code></pre>

to:

<pre><code>header(‘Content-type: text/javascript’);</code></pre>

Next I went to the ‘Presentation > Style’ section in the TXP interface and added a new stylesheet called ‘behavior’. Now by going to <code>http://mydomain.tld/textpattern/js.php?n=behavior</code> I can see my stylesheet and have it served up as <code>text/javascript</code>.

One last bit of mod_rewrite trickery in my <code>.htacces</code> file allows nicer looking URLs for my stylesheets and javascript files…

<pre><code>
RewriteRule ^style/(.*)\.css$ /textpattern/css.php?n=$1 [NC,L]
RewriteRule ^scripts/(.*)\.js$ /textpattern/js.php?n=$1 [NC,L]
</code></pre>

Now I can get to CSS and script files with nicer looking URLs like <code>http://mydomain.tld/style/screen.css</code> and <code>http://mydomain.tld/scripts/behavior.js</code> – nice and neat.

It’s a little bit of extra work though. I’ve tried doing it without the extra <code>/textpattern/js.php</code> file by forcing a mime-type on <code>/textpattern/css.php</code> in my rewrite rule with <code>[T=text/javascript]</code>. Something along the lines of…

<pre><code>
RewriteRule ^scripts/(.*)\.js$ /textpattern/css.php?n=$1 [T=text/javascript,NC,L]
</code></pre>

…but that just gets overridden by the php file. Does anyone know of a better way to accomplish this or is there a plugin to take care of js files that I’ve missed when searching the forum?

Offline

#2 2005-10-23 14:41:45

Mary
Sock Enthusiast
Registered: 2004-06-27
Posts: 6,236

Re: manage javascript files with TXP

Nice work. :)

A plugin is certainly possible, but I don’t believe one exists.

Offline

#3 2005-10-25 17:42:41

LeeStewart
Plugin Author
From: Boston, MA, US
Registered: 2005-07-25
Posts: 81
Website

Re: manage javascript files with TXP

Hey Chris,

I hacked something into my hit tracking plugins in an effort to serve arbitrary files from a database. I store files like .js, .gif, etc in a database table along with it’s mime type. To get the file I use a “virtual section” called “lxs_db_file”, so things like http://www.randomoddness.com/lxs_db_file/search.png serves the image that’s used on the Google Maps page that my plugin serves.

People can easily replace these images, in fact it’s the method I use to “skin” my plugin to allow for a custom look. The CSS, and images are all packaged up and downloaded automatically. This technique is also handy to update things like http://www.randomoddness.com/lxs_db_file/lxs_client_info.js the JavaScript file that my plugin uses to get details on the web client.

Basically, I just look at every incoming URL for my “section”, then pull out the filename and get the data from the database. Set the request headers with the mime type, run base64_decode() on the data, and send it down. Creating a user interface to maintain the files is a bit of work, but the core isn’t too complicated:
<pre> $url = parse_url($_SERVER[‘REQUEST_URI’]); $path = split(‘[\\/]’,$url[‘path’]); if ($path1==‘lxs_db_file’) { if ($a = safe_row(‘data,mime_type’, ‘txp_lxs_db_file’, ‘filename=\’‘.$path2.’\’‘)) { while (@ob_end_clean()); header(‘Content-Type: ‘.$a[‘mime_type’]); echo base64_decode($a[‘data’]); exit; } }
</pre>

[Lee]


Monkeys could have written a better post..

Offline

#4 2006-10-10 03:12:04

mwillse
Member
From: brooklyn, ny
Registered: 2006-06-26
Posts: 83
Website

Re: manage javascript files with TXP

this looks like a smart and fairly reasonable work-around. wish i had the skills to turn it into a plugin! thnx chris.

Last edited by mwillse (2006-10-10 03:12:59)

Offline

Board footer

Powered by FluxBB