Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
How do I use JS String Interpolation to insert Shopify sections? ('not a valid section type')

I'm building a 'view' template for certain blogs' tags. I'm grabbing the pathname with javascript and trying to insert a hero section based on which tag the user is on. If I hardcode the blog name and tag into the section string, it works. But if I try to interpolate the variables into it, I get a liquid error.

The code:

const pathName = window.location.pathname;
let blog_div = document.getElementById('blog-hero');
let blogName = pathName.split('/')[2];
let tagName = pathName.split('/')[4];

return blog_div.innerHTML = `{% section 'blog--hero-${blogName}-blog--${tagName}' %}`;

Expected Output: Section loads on screen

Actual Output: Nothing on-screen. Error in HTML: 'Liquid error: Error in tag 'section' - 'blog--hero-(blogName)-blog--(tagName)' is not a valid section type'

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Ended up using the Section Rendering API, per the docs.

But the JSON.parse() line didn't work. So I had to use the onload, readyState, and status methods per the MDN docs

New code ended up looking like

const pathName = window.location.pathname;
let blog_div = document.getElementById('blog-hero');
let blogName = pathName.split('/')[2];
let tagName = pathName.split('/')[4];

let request = new XMLHttpRequest();
request.open('GET', `/?section_id=blog--hero-${blogName}-blog--${tagName}`)
request.onload = () => {
    if(request.readyState === request.DONE) {
        if (request.status === 200) {
            blog_div.innerHTML = request.responseText;
        }
    }
};

request.send(null);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!