Forum Discussion

yurikleban's avatar
9 years ago

custom code only on 2 pages

Dear community,

 

 

Does anyone have an example of how we could use the page FreeMarker context object (http://community.lithium.com/t5/Developers-Knowledge-Base/page-FreeMarker-context-object/ta-p/9331) to specify a code to run only on two pages (based on URL).

 

Context: we have a remarketing code snippet that we want to fire when a user goes to two specific pages. Lithium said that we can only do this via the skin part, but that will enable this snippet on all pages. We only want it on the main page (e.g. /#) and a board (e.g./t5/community/b/AwesomeBoard)

 

Thus, was wondering if someone could assist in showing an example

 

e.g. (sorry for the terrible syntax) :

 

<#if page.isLink(url || url) >

<code snippet here>

</#if>

 

Thanks a lot,

Y

 

 

  • The page object doesn't support that.    Instead, use the http.request context object which does have a url method.   Using that along with the Freemarker contains built-in can do what you want.   For example:

     

    <#if http.request.url?contains("/t5/path/to/page1") || http.request.url?contains("/t5/path/to/page2")>
        <code for special pages here>
    </#if>

     

  • The page object doesn't support that.    Instead, use the http.request context object which does have a url method.   Using that along with the Freemarker contains built-in can do what you want.   For example:

     

    <#if http.request.url?contains("/t5/path/to/page1") || http.request.url?contains("/t5/path/to/page2")>
        <code for special pages here>
    </#if>

     

    • Thanks so much, that did the trick :) now just need to figure out how to do 'equals' instead of 'contains'. Cheers! :)
      • rwm's avatar
        rwm
        Advisor

        Glad that helped.   Any reason you don't like contains?    You could do a string compare with "==" like many languages, but for the http.request.url method you would have to include the entre URL from 'http' to the end, for your compare.   That's why contains is nice because it just valdates the part you need to check.    If the URL happens to contain query parameters after the path then you have to know those too since everything after the '?' is also part of the http.request.url.   Since those are often dynamic then that would not fly.   Or you may want to read about starts_with and ends_with in case they can help.