Docs > Scripting

PostCMS provides a Javascript API which can be used to automate various aspects of website production. You can use the API in workflow triggers, in web services, and in web templates and components.

Retrieving content

You can retrieve a specific known page or content object by its address (relative URL) like this:
var mypage = Page.get('news/uk/index.php');
You can also retrieve sets of content assets that match filter conditions:
// Retrieve 10 most recent news stories
var myfinder = new Finder([
{template:'News story'},
{'order-by': 'creation-date', reverse:true},
{max: 10}
]);
var result = myfinder.find();
Search results are returned as an ItemSet. You can iterate through an ItemSet to examine the results ones by one.
for (var i=0; i < result.items.length; i++) {
var item = result.items[i];
out.write(item.path);
}
In a web template, the global variable page gives you access to the properties and fields of the current page.


Writing output

Use the out global variable to write to output.
out.write('Hello world');
By default, output is escaped to ensure it is valid XHTML. That means you can safely write code like:
out.write('You are here > ' + page.name);
If you want to write out XHTML, then you can supply a second argument to the write method to switch off escaping. For example:
out.write('<br/>', false);
will write out a valid linebreak tag.

You can also use E4X to write output. That lets you write code like this:
out.write(<br/>);
out.write(<h1>{page.name}</h1>);
out.write(<a href={page.path}>Permalink</a>);
(Note the omission of quote marks in that last example: that's not a mistake!)

E4X is pretty cool, but has two important limitations. First, your code must be well-formed XHTML. So you can't write:
out.write(<br>);
without the closing /. Second, each E4X expression must have a single top-level element. So you can't write:
out.write(<br/><br/>); 
You can split a statement like that in two, or use the string version of the method as above, with escaping turned off. E4X also provides a special (though rather ugly) syntax that looks like this:
out.write(<><br/><br/></>);

Updating content

You can update the content of a page using the setText() method, and save your changes using save().
var mypage = Page.get('news/story1.html');
mypage.setText('author', 'Michael');
mypage.save();
Creating a new page is very similar:
var p = new Page();
p.template = 'New story';
p.path = 'news/story2.html';
p.name = 'Story 2';
p.setText('author', 'Michael');
p.setText('summary', 'This is just a test');
p.save();

Housekeeping

There are a number of housekeeping methods for pages.
Page.get('news/story1.html').publish();
Page.get('news/story2.html').expire();
Page.get('news/story3.html').move('news/archive');

Embedding script in web templates

Use the <js? ... ?> tags to embed scriptlets in a web template. This indicates that the script should be evaluated by PostCMS, not the web browser.

For example:
<h1>
<?js out.writeln(page.name); ?>
</h1>
If you are just outputting values, you can use an abbreviated syntax like this:
<h1><?js=page.name?></h1>
If you are outputting values into tag attributes, you can use curly brackets like this:
<a href="{p.path}"><?js=p.name?></a>
You can still include ordinary javascript in your template using a <script> tag in the usual way.

Request parameters

In a web service, a global variable named request can be used to access information about the request such as query parameters.
var x = request.getParameterValue('x');



Can't find what you're looking for?

Let us know and we'll see if we can help.