Content last modified Tuesday 24 August 2021
hop to #bottom
Categories:
Author:
About It:
Rate it:
  • Helpful?
  • 1 Yes
  • 0 No
Flag it:

If you'd like to provide updated information and do not have access to directly edit, please contact the site admin; thanks!

External Javascript In TXP

Okay, per recommendation, I’m writing this up as an official tip. I don’t know that I’m the first one to do this, but we can argue the etymology later. It’s possible to use the CSS database in Textpattern to store external javascript so that it doesn’t have to be embedded in the page template.

Anyhow, good practice suggests that javascript not be embedded into the HTML file. However with Textpattern, everything is in the database. It seems a shame to have to put extra files on the server. There is a way however.

As it turns out, the <txp:css /> tag has an attribute that lets you specify the name of the sheet you want to pull out. According to the Unofficial Textpattern Tag Manual this is useful for pulling out additional stylesheets, or switching styles on the fly. However we can also use it to pull ANY text source out of the database.

Okay, on to the good stuff. Here’s how to do it.

  1. Copy the javascript source into a TXP CSS stylesheet. Don’t worry that it’s not CSS. TXP doesn’t care. Remember the name of what you called it.
  2. In the page HTML template where you would like to call your Javascript source put the following:


<script type="text/javascript" src="<txp:css n="script-name" />" ></script>

And that’s all there is to it. The reason why it works is that TXP doesn’t really care if the text stored in the database is CSS or anything else. The <txp:css /> tag simply creates a URL to the css.php file and appends a ?n= tag to it. The css.php will use this to retrieve the page and serve it up. The script type text/javascript is what gives context for what the browser is retrieving.

Article Request Count:
Posted here:
07 Apr 2005

You know you want to visit the Archives.
Published with Textpattern