- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Pass variable to custom module template
Hi,
I'm having some troubles developing my custom module. In particular I cannot pass data from the ModuleHandler to the Mako template .
Here's my code :
1. CustomModule.py
class CustomModule(module.ModuleHandler):
def generateResults(self, hostApp=None, clientApp=None, savedSearchName=None):
list = ['A','B','C','D','E']
dict = {'list':list}
return self.controller.render_template('CustomModule.html', dict)
2. CustomModule.js
Splunk.Module.CustomModule= $.klass(Splunk.Module, {
initialize: function($super, container) {
$super(container);
this.myParam = this.getParam("myParam");
this.resultsContainer = this.container;
},
onContextChange:function($super) {
this.getResults();
}
})
3. CustomModule.html
%if (list):
<label> Data passed into mako </label>
% else:
<p>Nothing</p>
% endif
4 CustomModule.conf
[module]
className = Splunk.Module.CustomModule
superClass = Splunk.Module
description = Description
The current behaviour is that the module UI displays "Nothing" . It is like the generateResults
function from the CustomModule.py is not ever called.
Any hint on what I'm doing wrong or what I still need to do in order to pass a variable into the mako template would be greatly apreciated .
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

The link I provided is an example of using superClass = Splunk.Module.DispatchingModule
http://dev.splunk.com/view/SP-CAAADXX#Todefineyourcustommodule
that basically displays data after job done, see you have some backend job that gets data to display.
Let me use your example to bind an event, for example:
a. I create a button in the file CustomModule.html as a single line content:
b. I create another file CustomData.html to display data as below:
% if ( mydata 😞
${mydata}
% else:
Nothing
% endif
c. In CustomModule.py, I made the changes to render CustomData.html instead:
def generateResults(self, host_app, client_app, savedSearchName=None):
mylist = ['A','B','C','D','E']
mydict = {'mylist':mylist}
return self.controller.render_template('CustomModule/CustomData.html', dict(mydata=mylist))
d. Now, in CustomModule.js, I bind the click event of mybutton, call getResults() while clicking:
Splunk.Module.CustomModule= $.klass(Splunk.Module, {
initialize: function($super, container) {
$super(container);
$(document).find('.mybutton').click(this.onMyButtonClick.bind(this));
},
onMyButtonClick:function() {
this.getResults();
}
})
So the outcome is:
a. first, it shows up the button only.
b. when you click on button, you get data displayed underneath.
This gives you an example to bind an event for getting data.
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

see the tutorial at http://dev.splunk.com/view/SP-CAAADXX#Todefineyourcustommodule
There are errors in your code:
The this.getResults() in CustomModule.js never got called from client browser, because there is no context changed event occurs. Because getResults() is not called, so the CustomModule.py is not called either. Splunk system found you have CustomModule.html, the module is still valid, so it loads CustomModule.html alone. That was why you didn't have list object in the template html.
Let me fix the code a bit:
(1) in CustomModule.js, I let this.getResults() get called once the module is initialized:
Splunk.Module.CustomModule= $.klass(Splunk.Module, {
initialize: function($super, container) {
$super(container);
this.resultsContainer = this.container;
this.getResults();
},
})
in reality, you should put it in some event handling methods. but here let's do that for the testing.
(2) once this.getResults() is called, you will now find errors in your CustomModule.py. The working one is as below:
import controllers.module as module
class CustomModule(module.ModuleHandler):
def generateResults(self, host_app, client_app, savedSearchName=None):
##### def generateResults(self, hostApp=None, clientApp=None, savedSearchName=None):
mylist = ['A','B','C','D','E']
return self.controller.render_template('CustomModule/CustomModule.html', dict(mydata=mylist))
I recommend not using list and dict for variable names, since they are python's keywords.
(3) The final template page CustomModule.html that I made some changes as below:
% if ( mydata 😞
${mydata}
% else:
Nothing
% endif
(4) the final outcome is:
Data passed into mako
['A', 'B', 'C', 'D', 'E']
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
"in reality, you should put it in some event handling methods" ... Could you also enumerate the available event handling methods ?, or provide link to available documentation ?
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's some good tutorial. And don't forget to look at this, http://dev.splunk.com/view/SP-CAAAEEQ, for more about debugging/logging.
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

logging is subject to the log configuration. It can mislead you fairly easily. If you're using a logger that is undefined or logging at a level below the logging level it will never show up. Try logger.error() for your logging messages. Otherwise increase the logging level up to debug.
- Mark as New
- Bookmark Message
- Subscribe to Message
- Mute Message
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Indeed, that's something I need to change. However I also log some text in "generateResults" function (which I did't add to the code for simplicity) . These logs never get printed, suggesting that "generateResults" is never runned. I'm guessing that somehow I didn't hooked correctly all the pieces together, but I don't have a clue to what I'm missing
