Files
context-mod/docs/actionTemplating.md
FoxxMD 0afd87ab1b Doc improvements
* Reorganize some main readme contents into separate docs
* Add full bot authentication guide
* Add screenshots and web interface section

Some link fixes and clarifications

Fix another link

Fix another link

More doc cleanup

More doc cleanup

More doc cleanup

Add link to docs in main readme summary
2021-07-28 13:47:24 -04:00

2.5 KiB

Actions that can submit text (Report, Comment) will have their content values run through a Mustache Template. This means you can insert data generated by Rules into your text before the Action is performed.

See here for a cheatsheet and here for a more thorough tutorial.

All Actions with content have access to this data:


{
    item: {
        kind: 'string', // the type of item (comment/submission)
        author: 'string', // name of the item author (reddit user)
        permalink: 'string', // a url to the item
        url: 'string', // if the item is a Submission then its URL (external for link type submission, reddit link for self-posts)
        title: 'string', // if the item is a Submission, then the title of the Submission,
        botLink: 'string' // a link to the bot's FAQ
    },
    rules: {
        // contains all rules that were run and are accessible using the name, lowercased, with all spaces/dashes/underscores removed
    }
}

The properties of rules are accessible using the name, lower-cased, with all spaces/dashes/underscores. If no name is given kind is used as name Example:


"rules": [
  {
    "name": "My Custom-Recent Activity Rule", // mycustomrecentactivityrule
    "kind": "recentActivity"
  },
  {
    // name = repeatsubmission
    "kind": "repeatActivity",
  }
]

To see what data is available for individual Rules consult the schema for each Rule.

Quick Templating Tutorial

As a quick example for how you will most likely be using templating -- wrapping a variable in curly brackets, {{variable}}, will cause the variable value to be rendered instead of the brackets:


myVariable = 50;
myOtherVariable = "a text fragment"
template = "This is my template, the variable is {{myVariable}}, my other variable is {{myOtherVariable}}, and that's it!";

console.log(Mustache.render(template, {myVariable});
// will render...
"This is my template, the variable is 50, my other variable is a text fragment, and that's it!";

Note: When accessing an object or its properties you must use dot notation


const item = {
    aProperty: 'something',
    anotherObject: {
        bProperty: 'something else'
    }
}
const content = "My content will render the property {{item.aProperty}} like this, and another nested property {{item.anotherObject.bProperty}} like this."