Uploaded image for project: 'PUBLIC - Liferay Portal Community Edition'
  1. PUBLIC - Liferay Portal Community Edition
  2. LPS-79548

Global type variable injected by template context contributor module is not recognized in freemarker portlet

    Details

      Description

      Steps to reproduce:

      Create a template context contributor that injects a variable sample_text of type global
      1. Clone the Liferay Blade Samples project:

       git clone https://github.com/liferay/liferay-blade-samples.git
      

      2. Modify liferay-blade-samples\maven\themes\template-context-contributor\src\main\java\com\liferay\blade\samples\theme\contributor\BladeTemplateContextContributor.java:

      Replace:

      property = {"type=" + TemplateContextContributor.TYPE_THEME},
      

      with:

      property = {"type=" + TemplateContextContributor.TYPE_GLOBAL},
      

      With this, we want to make the injected variable (sample_text) available for non-theme templates (like ADT's, web content templates, etc).

      See https://dev.liferay.com/develop/reference/-/knowledge_base/7-0/template-context-contributor

      Are you working with templates that aren't themes (e.g., ADTs, DDM templates, etc.)? You can change the context in which your variables are injected by modifying the property attribute in the @Component annotation. If you want your variable available for all templates, change it to
      
      property = {"type=" + TemplateContextContributor.TYPE_GLOBAL}
      

      Modify the sample freemarker portlet to use the sample_text variable
      3. Modify \liferay-blade-samples\maven\apps\freemarker-portlet\src\main\resources\templates\init.ftl by adding $

      {sample_text}

      in a new line.

      4. Build the full maven/ project:

      cd liferay-blade-samples/maven
       mvn clean install
      

      5. Get a Liferay DXP SP6 bundle and start it. (this is customer's current patch level)

      6. Add the Blade basic-api artifact to you Liferay bundle:

      cp ~/.m2/repository/blade/basic-api/1.0.0/basic-api-1.0.0.jar $LIFERAY_HOME/osgi/modules/
      

      7. Deploy the generated template context contributor module jar from its target folder:

      cp liferay-blade-samples\maven\themes\template-context-contributor\target\com.liferay.blade.template.context.contributor-1.0.0.jar  $LIFERAY_HOME/deploy/
      

      8. Deploy the freemarker portlet module jar from its target folder:

      cp liferay-blade-samples\maven\apps\freemarker-portlet\target\com.liferay.blade.freemarker.portlet-1.0.0.jar  $LIFERAY_HOME/deploy/
      

      9. Log in as admin.
      10. Place the freemarker potlet on a page.

      Result: The portlet rendering crashes and in the Tomcat console you see errors starting with:

      2018-03-21 11:31:29.554 ERROR [http-apr-8080-exec-8][runtime:60] Error executing FreeMarker template
      FreeMarker template error:
      The following has evaluated to null or missing:
      ==> sample_text  [in template "com.liferay.blade.freemarker.portlet_SERVLET_CONTEXT_/templates/init.ftl" at line 30, column 3]
      

      You can see the full stack trace in the Tomcat console.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                sharry.shi Sharry Shi
                Reporter:
                istvan.dezsi Istvan Dezsi
                Participants of an Issue:
                Recent user:
                Csaba Turcsan
              • Votes:
                0 Vote for this issue
                Watchers:
                1 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved:
                  Days since last comment:
                  1 year, 31 weeks ago

                  Packages

                  Version Package
                  7.0.0 DXP FP46
                  7.0.6 CE GA7
                  7.0.0 DXP SP8
                  7.0.X
                  7.1.0 Beta 1
                  7.1.X
                  Master