Forum Discussion

kiettuongwork's avatar
kiettuongwork
Contributor
7 years ago

How to retrive info from query string and pass into component in Lithium

Hi Support Team,

 

I have tried to check how component in Lithium queries data for 4 pages above.

There is an issue need to get advise from Lithium support.

Scenario:

Once we access URL as below 

We want to get last part of URL in this case is "Reviews" and then pass that parameter to component name "recent-blog-posts"

And in component we will get parameter by using code as below

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

Question: 

  1. Where should check that component is used in admin of Lithium?
  2. How can we parse parameter from URL after /bg-p and then pass that parameter to component recent-blog-posts?
    1. 11112222
    2. Reviews
    3. LetsInspire
    4. NewAtDigi

Thanks,

Mickey

  • I also tried as below code in component recent-blog-posts

    <#assign url = http.request.url />
    <#assign parts = url?split("/") />
    <#assign category = parts[(parts?size)-1] />
    Hello category ${category}

     

    You can see result at 

    https://communitystg.digi.com.my/t5/Blog/bg-p/11112222

     

    Hello category = recent-blog-posts?render_behavior=raw&page=1

     

    It should be Hello category = 11112222

     

    Could you please advise how to retrieve 11112222?

     

    Thanks

    • luk's avatar
      luk
      Boss

      kiettuongwork there are several things you can try, I would do it as follows:

       

      <#--
          assuming URL: https://communitystg.digi.com.my/t5/Blog/bg-p/11112222
          with the goal to extract the number at the end, e.g. 11112222
      -->
      
      <#assign url = http.request.url />
      <#-- first split away any URL parameters, that's the stuff after a question mark "?" what you seem to want is a url-path element, basically what we want is everything before a possible question mark -->
      <#assign urlparts = url?split("?")?first />
      <#-- we then split that first part at every slash "/" -->
      <#assign urlparts = url?split("?")?first?split("/") />
      <#-- and eventually select just the last bit of that sequence to get what you want -->
      <#assign urlparts = url?split("?")?first?split("/")?last />
      
      <#-- of course you do not need to do that assignment 3 times, once is enough, e.g. like this: -->
      <#assign urlparts = url?split("?")?first?split("/")?last />
      
      ${urlparts}
      
      

      hope it helps!

       

      PS: The link to your stage instance of Lithium is blocked by .htaccess, therefore nobody can see your demo-links

      • kiettuongwork's avatar
        kiettuongwork
        Contributor

        I have tried them but could not work. You may need review my previous comments to see

      • syafiq's avatar
        syafiq
        Ace
        please try it in the page where you use this component. it should be working in the blog page.

        https://xxxxxxxxx/t5/blogname/bg-p/blogname