Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

140
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda