0

i have a function getQuantityValueFromURL in helpers

getQuantityValueFromURL: (url) => {
    const currentURL = new URL(url);
    const urlParams = new URLSearchParams(currentURL.search);
    return urlParams.get('quantity');
}

how can i pass the current url of the site when calling this function

{{getQuantityValueFromURL current url}}

i tried using window.location.href for the url but it didn't work

Lamey
  • 1
  • What is `current` and what is `url` in the `{{getQuantityValueFromURL current url}}` snippet? – 76484 Aug 04 '23 at 21:31

1 Answers1

0

If I understand you correctly, current url is one term and not two distinct terms (current and url) and that you want to pass the url that is being requested. If so, you can pass that data to your template by just adding it in the optional second parameter to res.render method and it will be accessible by your helper function getQuantityValueFromURL like so:

// route.js

router.get('/path/here', (req, res) => {
   res.render('your_view', {
      data: someData,
      currentUrl: req.originalUrl
   });
});


// your_view.hbs

{{getQuantityValueFromURL currentUrl}}

However the information your helper is extracting is already accessible in Express in one of the many properties of the req object. You don't need a helper function.

Example 1 (Route Params) if user has requested page https://www.yourshop/path/here/55:

// route.js

router.get('/path/here/:quantity', (req, res) => {
  res.render('your_view', {
     data: someData,
     quantity: req.params.quantity // (quantity = 55)
  });
});

// your_view.hbs

{{ quantity }}

Example 2 (Query Params) if user has requested page https://www.yourshop/path/here?quantity=88:

// route.js

router.get('/path/here', (req, res) => {
  res.render('your_view', {
     data: someData,
     quantity: req.query.quantity // (quantity = 88)
  });
});

// your_view.hbs

{{ quantity }}
jQueeny
  • 714
  • 2
  • 13