Skip to content

Context

javarome edited this page Jun 9, 2024 · 5 revisions

A SsgContext is provided to Ssg methods (SsgStep.execute(content) and ReplaceCommand.execute(context)) to carry information about :

  • the current locale
  • the current file that has been read and is about to be written
  • variables
  • a logger to customize logging.
  • an optional name to customize logging.

It also has a clone() method.

Variables

A SsgContext holds context variables that can be updated or queried by any code (typically Steps or ContentStep's Replacements).

A context is with a locale and a set of variables, which can be initially empty:

const context = new SsgContextImpl("fr")

or non-empty, even with a specified type if needed:

const myVars: MyVars = {myVar: "myValue"}
const context = new SsgContextImpl<MyVars>("fr", myVars)

Variables can be:

  • read using context.getVar(myVar)
  • set using context.setVar(myVar, myValue) in your steps/replacements code, or among tags, SSI style (for instance <!--#set var="myVar" value="myValue" --> thanks to the predefined SsiSetVarReplaceCommand)
  • inserted into files using:
    • <!--#echo var="varName" --> among HTML tags (thanks to the predefiend SsiEchoVarReplaceCommand)
    • ${varName} in strings (for instance <a href="mailto:${varName}">) thanks to the predefiend StringEchoVarReplaceCommand

Predefined variables

Attributes of the context.file (including as a HtmlFileContents) are implicitly available as variables, so that you can include <!--#echo var="title" --> in your files where you want the HTML title to be inserted for instance.

Logger

A Context is also a Logger, with log()/warn()/error()/debug() methods. A DefaultLogger will be used unless a custom logger is provided at construction.

Name

You can optionally set a context name, that will be reflecting in logging. For instance:

const context = new SsgContextImpl<MyVars>("fr", {}, "My Ssg")

The default context name is "Ssg".

Custom context

You can design your own context by implementing the SsgContext interface (typically to provide custom info to custom steps). A convenient way to do so is to extend the default SsgContextImpl implementation.

Clone this wiki locally