What is in the context object?
- That easy: everything is there and it become the global context in your .ejs view files.
Very important is access to
- the Page object
- the (request) Params
- the global Cody object
- All strings that are added in the CMS system
- The root page
- The children pages of this page
- An array with all content entered through the CMS system
function Context(path, page, app, req, res) {
this.version = (app) ? app.version : "v0.0";
this.page = page;
this.app = app;
this.req = req;
this.res = res;
this.path = path;
// copy query params and body params into this.params and .param
this.params = {};
for(var q in req.query) {
if (req.query.hasOwnProperty(q)) {
this.params[q] = req.query[q];
}
}
for(var b in req.body) {
if (req.body.hasOwnProperty(b)) {
this.params[b] = req.body[b];
}
}
this.request = this.params.request || path.request || page.item.defaultrequest || "";
this.status = "success";
this.message = "";
this.host = req.headers.host;
this.dateFormat = "dd-mm-yyyy";
this.min = ""; // ".min"
this.static = "/static";
this.dynamic = "/data";
this.cstatic = "/cody/static";
var stringPage = app.getPage(page.language, "strings");
this.strings = [];
this.addStrings(stringPage);
this.fn = (page) ? page.getView() : "index.ejs";
this.session = req.session;
this.setLogin((typeof this.session === "undefined") ? {} : this.session.login);
// make global cody lib available
this.cody = cody;
}
The context API
- The render function
Context.prototype.render = function(params) {
//
// Render content queried by name or kind.
//
// { kind: String,
// not_kind: String,
// name: String,
// not_name: String,
// intro: Y/N, Show intro part?
// page: Page } Optional Page to get the content from instead of current
... }
// kind = content types:
return (theKind === "I") ? "Image" : (theKind === "T") ? "Text" : (theKind === "M") ? "Form" : (theKind === "P") ? "Params" : (theKind === "S") ? "String" : (theKind === "F") ? "File" : "Block";
Other functions
- formatDate()
- formatTime()
- optionList()
- isLoggedIn()
A similar version exists "optionListF" using function for rendering id's and names giving more control.
// Creates html options from a given list // // 1. optionList([String], String) // Creates an option for each string and marks theId as string. // 2. optionList([Object], String, String, String) // Creates an option for each object, using theIdName and theNameName properties // of each object to set the id and html resp.
Ultimatly: look at the file models/context.js