8 Replies Latest reply on Nov 4, 2014 10:59 PM by 462739

    Hello World Widget tutorial

      I have been trying to work out this tutorial all day and everything was easy except I cannot get it to pick up the .ftl file.  The result is a widget that just has a title.  You can edit the properties (so I know it is picking up the .properties file at least), but no effect.  Attached is the jar and .java src file.

       

      I have seen a lot of questions about this on the forum, and a lot of people declaring success after a while, but no one posted the solution.

       

      I am pasting the .ft declaration from the src file here in case something is obvious:

       

          // FreeMarker template for rendering preview and published widget.
          private static final String FREEMARKER_FILE = "/plugins/example/resources/HelloWorldWidget.ftl";

          // FreeMarker template for rendering preview and published widget.

          private static final String FREEMARKER_FILE = "/plugins/example/resources/HelloWorldWidget.ftl";

        • Re: Hello World Widget tutorial
          donaldrivard

          Are you getting any errors logged? Are you sure that the template is not being loaded?

          If you are not sure, I would make the template as simple as possible to make sure that it is at least being loaded.

           

           

           

          <#-- Use the widget's greetUser property to define the greeting style. -->
          <#if greetUser>
                  <#assign greeting = "Hello " + userName + "!">
          <#else>
                  <#assign greeting = "Hello World!">
          </#if>

           

          <p class="gobig">${greeting} Welcome to the ${communityName} community!</p>

            • Re: Hello World Widget tutorial
              Thank you for the response.  My log does return errors:
              Feb 2011 09:38:30,535 [http-127.0.0.1-9001-1] [096FE0EFE6D770671395080EB0FA19AA.:admin] ERROR freemarker.runtime -
              Method public abstract java.lang.String com.jivesoftware.community.widget.view.WidgetRenderer.renderPreview(com.jivesoftware.community.widget.WidgetContext,com.jivesoftware.community.widget.Widget$ContainerSize) threw an exception when invoked on com.jivesoftware.community.widget.ext.impl.DefaultExtWidgetProvider$DefaultWidgetRenderer@632843c7
              The problematic instruction:
              ----------
              ==> ${widgetRenderer.renderPreview(widgetContext, enums["com.jivesoftware.community.widget.Widget$ContainerSize"].SMALL)} [on line 6, column 5 in template/global/include/preview-widget.ftl]
              ----------
              Java backtrace for programmers:
              ----------
              freemarker.template.TemplateModelException: Method public abstract java.lang.String com.jivesoftware.community.widget.view.WidgetRenderer.renderPreview(com.jivesoftware.community.widget.WidgetContext,com.jivesoftware.community.widget.Widget$ContainerSize) threw an exception when invoked on com.jivesoftware.community.widget.ext.impl.DefaultExtWidgetProvider$DefaultWidgetRenderer@632843c7

              Thank you for the response.  My log does return errors:

               

              --

              Feb 2011 09:38:30,535 [http-127.0.0.1-9001-1] [096FE0EFE6D770671395080EB0FA19AA.:admin] ERROR freemarker.runtime -

               

              Method public abstract java.lang.String com.jivesoftware.community.widget.view.WidgetRenderer.renderPreview(com.jivesoftware.community.widget.WidgetContext,com.jivesoftware.community.widget.Widget$ContainerSize) threw an exception when invoked on com.jivesoftware.community.widget.ext.impl.DefaultExtWidgetProvider$DefaultWidgetRenderer@632843c7

              The problematic instruction:

              ----------

              ==> ${widgetRenderer.renderPreview(widgetContext, enums["com.jivesoftware.community.widget.Widget$ContainerSize"].SMALL)} [on line 6, column 5 in template/global/include/preview-widget.ftl]

              ----------

               

              Java backtrace for programmers:

              ----------

              freemarker.template.TemplateModelException: Method public abstract java.lang.String com.jivesoftware.community.widget.view.WidgetRenderer.renderPreview(com.jivesoftware.community.widget.WidgetContext,com.jivesoftware.community.widget.Widget$ContainerSize) threw an exception when invoked on com.jivesoftware.community.widget.ext.impl.DefaultExtWidgetProvider$DefaultWidgetRenderer@632843c7

              --

               

              I also tried the simple ftl file code that you included in your response.  Same error.

                • Re: Hello World Widget tutorial
                  donaldrivard

                  The error:

                   

                  The problematic instruction:

                  ----------

                  ==>  ${widgetRenderer.renderPreview(widgetContext,  enums["com.jivesoftware.community.widget.Widget$ContainerSize"].SMALL)}  [on line 6, column 5 in template/global/include/preview-widget.ftl]

                  ----------

                   

                  Indicates that there is an issue with the line:

                  <#if containerSize == enums['com.jivesoftware.community.widget.Widget$ContainerSize'].SMALL>

                   

                  If you want to check the container size in a template I would add the ContainerSize to the properties

                  Map props.put("size", size);

                  Map props.put("ContainerSize", ContainerSize);

                  and check

                  <#if size == ContainerSize.LARGE>

                   

                  I am not sure but I do not see where containerSize is added to the template scope. I can see where this code is used in some of the Jive core templates. They must be adding that object to the scope.

                   

                   

                  Also for my test example are you sure that you removed that line from the simple template that i suggested?

                   

                  <#-- Use the widget's greetUser property to define the greeting style. -->
                  <#if greetUser>
                          <#assign greeting = "Hello " + userName + "!">
                  <#else>
                          <#assign greeting = "Hello World!">
                  </#if>

                   

                  <p class="gobig">${greeting} Welcome to the ${communityName} community!</p>

                   

                   

                  If so it maybe possible that your instance is caching the template, as that line was removed.

                    • Re: Hello World Widget tutorial

                      I am willing to believe anything, but based on the problematic instruction:

                      ----------

                      ==>  ${widgetRenderer.renderPreview(widgetContext,  enums["com.jivesoftware.community.widget.Widget$ContainerSize"].SMALL)}  [on line 6, column 5 in template/global/include/preview-widget.ftl]

                      ----------

                      it appears to be using a completely different ftl file than the one I provided (mine was called "HelloWorldWidget.ftl").  I presume this is a generic ftl that the widget defers to in the event of not having an ftl (since ftl's are optional).  Is this presumption correct?

                       

                      I have managed to achieve some results by completely ignoring the ftl and doing all the logic in the java method, but I would really like to get this working "the right way."

                       

                      and yes, I am absolutely sure that I removed that line... it could be cached, but I would think that the apprestart would clear that out... I have also tried refreshing the page and different browsers to confirm the local cache is not the problem.

                        • Re: Hello World Widget tutorial
                          donaldrivard

                          You are 100% correct. Disregard all the useless typing I added . I have not seen that error in my widget development.

                           

                          What I can tell you from my experiences is that if the plugin cannot find the template I get an error indicating that it cannot load the template.

                          I have never built a widget without an UI / template so I am not sure about the fallback. I have also never checked for the container size in a template, I have done the check in the widget code, 1 template for each size.

                           

                                 if(widgetContext.isEdit()){
                                        output.append(applyFreemarkerTemplate(widgetContext, size, EDIT_TEMPLATE));
                                        return output.toString();
                                    }
                                    //choose the appropriate render template according to the size
                                    if (size == ContainerSize.LARGE) {
                                           output.append(applyFreemarkerTemplate(widgetContext, size, LARGE_FREEMARKER_TEMPLATE));
                                      
                                    } else {
                                        output.append(applyFreemarkerTemplate(widgetContext, size, SMALL_FREEMARKER_TEMPLATE));
                                    }


                          Are you building this with maven? Under the target directory do you see the example/resources/HelloWorldWidget.ftl? Have you debugged into the HelloWorldWidget class, setting a break point on the render method to

                          see if you template is being loaded?

                           

                           

                          Also my 2 cents, putting all the logic in a java class is not necessarily wrong, and putting the logic in the template is not necessarily the "right way". I prefer to put as much logic in java and let the template code be as simple as possible. Rendering UI objects, instead of determinig which UI objects to render. Just my 2 cents.