Forum Discussion

KPNOnline's avatar
KPNOnline
Mentor
10 years ago

Get endpoint url in freemarker

Hi Guys,

 

We have multiple environments running, all with a different URL..

 

On all the environments, we use a custom endpoint.. They are the same (code is just copied to other environment),  but the URL is different in environments, but we cannot get the 'absolute' url to the endpoint in an easy way.. So we have to do ugly if/else statements to change the url..

 

Example:

 

If we call the endpoint:

 

in env A: /etgtr99956/plugins/custom/kpn/kpnbus/post-message

in env B: /kpn/plugins/custom/kpn/kpn/post-message

 

The first diff (etgtr99956, kpn) is easy to fix, because thats just the ${community.id},

 

but the second (kpnbus, kpn) is no logic difference, so it cannot be filled up easily with an existing variable..


Thus we have to do ugly if/else statements, like this ->

 

<#if community.id == 'etgtr99956'>
  <#assign url = ' /etgtr99956/plugins/custom/kpn/kpnbus/post-message' />
<#elseif community.id == 'kpn' && env == stage>
 <#assign url = '/kpn/plugins/custom/kpn/kpn/post-message' />
<#else>
 <#assign url = '/kpn/plugins/custom/kpn/kpnbus/post-message' />
</#if>

 

So point is:

 

Is there an easy way to get the absolute url to the endpoint from within freemarker?

 

Thanks!

 

  • You can use the community.urls.communityPrefix freemarker call (see the documentation on the commuinty freemarker context object) to get the the community id (it renders as '/' + ${communityid}, so if you community id is etgtr99956 then this will return as /etgtr99956).

     

    Another thing to note is that your communities all have an apache rewrite rule in them that rewrites /plugins/... to /${communityid}/plugins/...

     

    That doesn't really help with the second variable.  For that I would recommend creating a variable or function in a macro that you include in all of your components and endpoints that can be used in constructing every endpoint url.

     

    -Doug

4 Replies

  • DougS's avatar
    DougS
    Khoros Oracle
    10 years ago

    You can use the community.urls.communityPrefix freemarker call (see the documentation on the commuinty freemarker context object) to get the the community id (it renders as '/' + ${communityid}, so if you community id is etgtr99956 then this will return as /etgtr99956).

     

    Another thing to note is that your communities all have an apache rewrite rule in them that rewrites /plugins/... to /${communityid}/plugins/...

     

    That doesn't really help with the second variable.  For that I would recommend creating a variable or function in a macro that you include in all of your components and endpoints that can be used in constructing every endpoint url.

     

    -Doug

  • KPNOnline's avatar
    KPNOnline
    Mentor
    10 years ago

    Hi DougS

     

    Thanks for the reaction, but as mentioned in the post, getting the community ID is not the problem.. Although it is nice to know, the community.id is not strictly required by the apache rewrite :) But please read the original post again.

     

    from the post:

     

    in env A: /etgtr99956/plugins/custom/kpn/kpnbus/post-message

    in env B: /kpn/plugins/custom/kpn/kpn/post-message

     

    The first diff (etgtr99956, kpn) is easy to fix, because thats just the ${community.id}

     

    but the second (kpnbus, kpn) is no logic difference, so it cannot be filled up easily with an existing variable. 

     

    So the problem is.. How to get the 'kpnbus / kpn' difference in freemarker.. Both environments are called 'kpn'.. So how lithium makes it 'kpnbus' is a mystery

     

    Thanks

  • DougS's avatar
    DougS
    Khoros Oracle
    10 years ago

    oh wow, that was quick.  I was editing my post while you were posting this.  Check out my edited post above -- I suggested you use a function or variable in a macro for the second variable.

  • KPNOnline's avatar
    KPNOnline
    Mentor
    10 years ago

    Ah cheers! Sorry haha, i'm right on it :smileytongue:

     

    Yeaah that a good fallback probably.. I will do that.

     

    Thanks DougS!