user1791937
user1791937

Reputation: 41

configurable value for header-portlet-javascript in liferay

Is it possible for header-portlet-javascript to pick up from system properties?

For example:

<header-portlet-javascript>${external.js.url}</header-portlet-javascript>

Upvotes: 4

Views: 519

Answers (1)

p.mesotten
p.mesotten

Reputation: 1402

In general, this is not possible.

However, if it's ok that the Javascript you want to include occurs on every page of your portal, you could just add a reference to it inside your Liferay theme. Inside the theme, you can do dynamic stuff to retrieve the right JS url, e.g. using a portal property:

#set($jsUrl = $propsUtil.get("external.js.url"))
<script type="text/javascript" src="$jsUrl"></script>

To have the same effect with System properties, things get a bit more complex. To my knowledge there is no way to get System properties from an injected Velocity variable. Therefore, we need to create a small event handler hook that will inject this property into the Velocity context.

portal.properties

servlet.service.events.pre=my.custom.ServicePreAction

ServicePreAction.java

public class ServicePreAction extends Action {

    public void run(HttpServletRequest request, HttpServletResponse response) {
        Map<String,Object> veloVars = new HashMap<String,Object>();
        veloVars.put("externalJSurl", System.getProperty("external.js.url"));
        request.setAttribute(WebKeys.VM_VARIABLES, veloVars);
    }

}

portal_normal.vm

<script type="text/javascript" src="$externalJSurl"></script>

Upvotes: 1

Related Questions