1

I am defining the AMD module under OSGi module. I have to read host property in AMD loader definition. how can I read the property from portal-ext.properties file?

below is sample code how I am defining the AMD module and property in portal-ext.properties file.

portal-ext.properties

# host detail
host={{host_url}}

define AMD module

Liferay.Loader.define('genelec-shopping-cart', [], function(){
    const host = ""; //here i have to read the property
    return{
        getHost:function(){
            return host;
        },
    };
});

1 Answers1

0

You can do this with ftl or jsp, building your JS with the value embedded. Instead of creating the whole code from a JSP you can also just build a small portion of it, adding the value to a JS variable, making it available for scripts that will load later.

Let's say you have an OSGi module which JS code, you can create a .js.jsp that will build the JS before sending it.

Using JSP to create a small portion of JS:

<%@ page contentType='application/javascript' %>

//here i have to read the property -> do it in java

Liferay.Loader.define('genelec-shopping-cart', [], function(){
    const host = "${read_in_java}"; 
    return{
        getHost:function(){
            return host;
        },
    };
});

You can include it from other JSP files as:

<c:url var='url' value='/variables.js.jsp'>
    <c:param name='namespace' value='${namespace}'/>
</c:url>

<script src='${url}'></script> 

But as you mentioned the host, you are probably looking for something much simpler than that (especially because this is kind of a hack), using Liferay's JS api:

Liferay.ThemeDisplay.getPortalURL() and friends, docs here: https://dev.liferay.com/de/develop/tutorials/-/knowledge_base/7-1/liferay-javascript-apis

Victor
  • 3,520
  • 3
  • 38
  • 58