Forum Discussion

iftomkins's avatar
10 years ago

How to run Freemarker code on click?

I have a bit of freemarker code which updates a user's profile fields: 

 

<#-- Call user click, set accepted terms -->
<#assign response = restadmin("/users/id/${user.id}/profiles/name/ideas_terms_accepted/set?value=checked")/>
<#assign response = restadmin("/users/id/${user.id}/profiles/name/ideas_terms_accepted_date/set?value=" + datesupport.millisecondsAsString)/>

 

When a user clicks "I accept terms and conditions", I want to run this freemarker code. How do I call this code on click?

 

I can put it in an Endpoint, but then how do I call that endpoint on click? I can put it in a Macro, but then how do I call that Macro on click?

 

Thanks!

Alan

  • JasonL's avatar
    JasonL
    10 years ago

    hi iftomkins 

    from the screenshot, i roughly guessed your Community :-)

     

    took a look at your endpoint, instead of

    <#return "test"?json_string />

    try this (since it's application/json - that's what you should be returning, the #return in freemarker is more for freemarker function calls)

    { "status": "ok" }

     

    the endpoint should work ... then the client-side ajax should be working too ... i hope

    • JasonL's avatar
      JasonL
      Lithium Alumni (Retired)

      hi iftomkins 

      one suggestion is:

      • write an endpoint, something like this:
        • <#if user.registered>
          ...
              <#assign ideas_terms_accepted= http.request.parameters.name.get("ideas_terms_accepted", "false") />
                  <#if ideas_terms_accepted?? >
                  ${rest("/users/self/profiles/name/ideas_terms_accepted/set?value=${ideas_terms_accepted}")}
              </#if>
          
          ...
            
          </#if> 
        • in studio, you will see  a preview of your endpoint directly below, mouseover to get the url of your endpoint
      • write a custom component that invokes the endpoint
        • you can use a standard HTML form or AJAX call, which ever is preferred
        • standard form is something like this:
        • <form action="/<<relative url to your endpoint>>" method="post">
            Click here to accept ideas terms:<input type="checkbox" name="ideas_terms_accepted" value="true" checked>I accept ideas term<br/> 
            <input type="submit" value="Submit">
          </form> 
        • the ajax will be similar to the link you provide, just change url to your endpoint
      • add the above custom component to appropriate page quilt

       

      Hopefully that clarifies, otherwise probably best to reach out to your local Services team (via Sales or Support)

      • iftomkins's avatar
        iftomkins
        Maven

        Thank you, JasonL ! Seems to work. My only question is how to redirect the user back to the page they were on when they clicked "Submit"? I'm stumped at the actual redirect part.

         

        I know I can add a return url parameter (?return=http://fitbit.com), and capture it like this:

         

        <#assign returnUrl = http.request.parameters.name.get("return","") />

         

        However, how do I redirect users back from the Endpoint? After the form submission I end up on a blank page (screenshot attached). I tried to implement a redirect, but the freemarker ones can be only used on the page initialization page: 

        ${http.response.setRedirectUrl("http://returnwebsite.com")}.

         

        Thanks!

         

         

         

         


        Screenshot 2015-03-12 17.45.03.png