<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Pass variable to custom module template in Dashboards &amp; Visualizations</title>
    <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132901#M44427</link>
    <description>&lt;P&gt;"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 ?&lt;/P&gt;</description>
    <pubDate>Fri, 08 Nov 2013 09:01:17 GMT</pubDate>
    <dc:creator>leustean</dc:creator>
    <dc:date>2013-11-08T09:01:17Z</dc:date>
    <item>
      <title>Pass variable to custom module template</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132896#M44422</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;

&lt;P&gt;I'm having some troubles developing my custom module. In particular I cannot pass data from the ModuleHandler to the Mako template .&lt;/P&gt;

&lt;P&gt;Here's my code :&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;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):
    &amp;lt;label&amp;gt; Data passed into mako &amp;lt;/label&amp;gt; 
% else:
    &amp;lt;p&amp;gt;Nothing&amp;lt;/p&amp;gt;
% endif


4 CustomModule.conf
[module]
className = Splunk.Module.CustomModule
superClass = Splunk.Module     
description = Description
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;The current behaviour is that the module UI displays "Nothing" . It is like the &lt;CODE&gt;generateResults&lt;/CODE&gt; function from the CustomModule.py is not ever called.&lt;/P&gt;

&lt;P&gt;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 .&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2013 16:39:44 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132896#M44422</guid>
      <dc:creator>leustean</dc:creator>
      <dc:date>2013-11-07T16:39:44Z</dc:date>
    </item>
    <item>
      <title>Re: Pass variable to custom module template</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132897#M44423</link>
      <description>&lt;P&gt;see the tutorial at &lt;A href="http://dev.splunk.com/view/SP-CAAADXX#Todefineyourcustommodule" target="_blank"&gt;http://dev.splunk.com/view/SP-CAAADXX#Todefineyourcustommodule&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;There are errors in your code:&lt;/P&gt;

&lt;P&gt;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.&lt;/P&gt;

&lt;P&gt;Let me fix the code a bit:&lt;/P&gt;

&lt;P&gt;(1) in CustomModule.js, I let this.getResults() get called once the module is initialized:&lt;/P&gt;

&lt;P&gt;Splunk.Module.CustomModule= $.klass(Splunk.Module, {&lt;BR /&gt;
    initialize: function($super, container) {&lt;BR /&gt;
        $super(container);&lt;BR /&gt;
        this.resultsContainer = this.container;&lt;BR /&gt;
        this.getResults();&lt;BR /&gt;
    },&lt;BR /&gt;
})&lt;/P&gt;

&lt;P&gt;in reality, you should put it in some event handling methods. but here let's do that for the testing.&lt;/P&gt;

&lt;P&gt;(2) once this.getResults() is called, you will now find errors in your CustomModule.py. The working one is as below:&lt;/P&gt;

&lt;P&gt;import controllers.module as module&lt;BR /&gt;
class CustomModule(module.ModuleHandler):&lt;BR /&gt;
    def generateResults(self, host_app, client_app, savedSearchName=None):&lt;BR /&gt;
    ##### def generateResults(self, hostApp=None, clientApp=None,  savedSearchName=None):                   &lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;    mylist = ['A','B','C','D','E']        

    return self.controller.render_template('CustomModule/CustomModule.html', dict(mydata=mylist))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;I recommend not using list and dict for variable names, since they are python's keywords.&lt;/P&gt;

&lt;P&gt;(3) The final template page CustomModule.html that I made some changes as below:&lt;/P&gt;

&lt;P&gt;% if ( mydata &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;
    &lt;LABEL&gt; Data passed into mako &lt;/LABEL&gt; &lt;BR /&gt;
    &lt;/P&gt;&lt;P&gt; ${mydata} &lt;/P&gt;&lt;BR /&gt;
% else:&lt;BR /&gt;
    &lt;P&gt;Nothing&lt;/P&gt;&lt;BR /&gt;
% endif&lt;P&gt;&lt;/P&gt;

&lt;P&gt;(4) the final outcome is:&lt;/P&gt;

&lt;P&gt;Data passed into mako&lt;BR /&gt;
['A', 'B', 'C', 'D', 'E'] &lt;/P&gt;</description>
      <pubDate>Mon, 28 Sep 2020 15:13:50 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132897#M44423</guid>
      <dc:creator>btsay_splunk</dc:creator>
      <dc:date>2020-09-28T15:13:50Z</dc:date>
    </item>
    <item>
      <title>Re: Pass variable to custom module template</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132898#M44424</link>
      <description>&lt;P&gt;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&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2013 18:50:21 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132898#M44424</guid>
      <dc:creator>leustean</dc:creator>
      <dc:date>2013-11-07T18:50:21Z</dc:date>
    </item>
    <item>
      <title>Re: Pass variable to custom module template</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132899#M44425</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2013 22:05:36 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132899#M44425</guid>
      <dc:creator>mkinsley_splunk</dc:creator>
      <dc:date>2013-11-07T22:05:36Z</dc:date>
    </item>
    <item>
      <title>Re: Pass variable to custom module template</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132900#M44426</link>
      <description>&lt;P&gt;That's some good tutorial.  And don't forget to look at this, &lt;A href="http://dev.splunk.com/view/SP-CAAAEEQ"&gt;http://dev.splunk.com/view/SP-CAAAEEQ&lt;/A&gt;, for more about debugging/logging.&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2013 00:04:24 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132900#M44426</guid>
      <dc:creator>ghartsel</dc:creator>
      <dc:date>2013-11-08T00:04:24Z</dc:date>
    </item>
    <item>
      <title>Re: Pass variable to custom module template</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132901#M44427</link>
      <description>&lt;P&gt;"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 ?&lt;/P&gt;</description>
      <pubDate>Fri, 08 Nov 2013 09:01:17 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132901#M44427</guid>
      <dc:creator>leustean</dc:creator>
      <dc:date>2013-11-08T09:01:17Z</dc:date>
    </item>
    <item>
      <title>Re: Pass variable to custom module template</title>
      <link>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132902#M44428</link>
      <description>&lt;P&gt;The link I provided is an example of using superClass = Splunk.Module.DispatchingModule&lt;/P&gt;

&lt;P&gt;&lt;A href="http://dev.splunk.com/view/SP-CAAADXX#Todefineyourcustommodule"&gt;http://dev.splunk.com/view/SP-CAAADXX#Todefineyourcustommodule&lt;/A&gt;&lt;/P&gt;

&lt;P&gt;that basically displays data after job done, see you have some backend job that gets data to display.&lt;/P&gt;

&lt;P&gt;Let me use your example to bind an event, for example:&lt;/P&gt;

&lt;P&gt;a. I create a button in the file CustomModule.html as a single line content:&lt;/P&gt;

&lt;P&gt;&lt;BUTTON class="mybutton" type="button"&gt;Click Me!&lt;/BUTTON&gt; &lt;/P&gt;

&lt;P&gt;b. I create another file CustomData.html to display data as below:&lt;/P&gt;

&lt;P&gt;% if ( mydata &lt;span class="lia-unicode-emoji" title=":disappointed_face:"&gt;😞&lt;/span&gt;&lt;BR /&gt;
    &lt;LABEL&gt; Data passed into mako &lt;/LABEL&gt; &lt;BR /&gt;
    &lt;/P&gt;&lt;P&gt; ${mydata} &lt;/P&gt;&lt;BR /&gt;
% else:&lt;BR /&gt;
    &lt;P&gt; Nothing &lt;/P&gt;&lt;BR /&gt;
% endif&lt;P&gt;&lt;/P&gt;

&lt;P&gt;c. In CustomModule.py, I made the changes to render CustomData.html instead:&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;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))
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;d. Now, in CustomModule.js, I bind the click event of mybutton, call getResults() while clicking:&lt;/P&gt;

&lt;P&gt;Splunk.Module.CustomModule= $.klass(Splunk.Module, {&lt;BR /&gt;
    initialize: function($super, container) {&lt;BR /&gt;
        $super(container);&lt;BR /&gt;
        $(document).find('.mybutton').click(this.onMyButtonClick.bind(this));&lt;BR /&gt;
    },&lt;/P&gt;

&lt;PRE&gt;&lt;CODE&gt;onMyButtonClick:function() {
    this.getResults();
}
&lt;/CODE&gt;&lt;/PRE&gt;

&lt;P&gt;})&lt;/P&gt;

&lt;P&gt;So the outcome is:&lt;/P&gt;

&lt;P&gt;a. first, it shows up the button only.&lt;BR /&gt;
 b. when you click on button, you get data displayed underneath.&lt;/P&gt;

&lt;P&gt;This gives you an example to bind an event for getting data.&lt;/P&gt;</description>
      <pubDate>Tue, 12 Nov 2013 21:17:59 GMT</pubDate>
      <guid>https://community.splunk.com/t5/Dashboards-Visualizations/Pass-variable-to-custom-module-template/m-p/132902#M44428</guid>
      <dc:creator>btsay_splunk</dc:creator>
      <dc:date>2013-11-12T21:17:59Z</dc:date>
    </item>
  </channel>
</rss>

