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

208
Visualizações
How can I have simple macros in HTML?

I have a piece of HTML that I repeat frequently on a webpage but with some parts different. It contains some things inconvenient to write repeatedly, so I'd like to have a shortcut with parameters.

An example of how I would use it is to format dates. I format them like (2022‑04‑06), where the entities are non-breaking hyphens. I want to not have to remember or copy the code for a non-breaking hyphen, so I'd like to be able to write it simply.

In LATEX, I could use a macro like this:

…
\newcommand{\mydate}[3]{(#1\nobreakdash-#2\nobreakdash-#3)}
\begin{document}
  \mydate{2022}{04}{06}
\end{document}

I know that HTML has custom tags, but I don't know how exactly they are supposed to be used. I imagine that the macro would be used like this:

<body>
  <my-date 2022 04 06>
</body>

I imagine that the macros would be defined in the head of the page, and a Javascript function would run when the page loads and format all the tags with defined macros.

How can be something like that achieved in HTML? I expect that it would involve a Javascript library. Then, I'd like that the library would be light and fast, and it would require not much boilerplate code.

This could be achieved by templates and preprocessing the webpage, but I don't want that; I want that I could quickly edit the page, save it, and it would be live.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You could use a templating engine like Handlebars. Just design your HTML template and define a custom helper function.

const pad = (val, char, count) => (val + '').padStart(count, char);

const parseTemplateHtml = (selector) =>
  Handlebars.compile(document.querySelector(selector).innerHTML);

Handlebars.registerHelper('formatDate', (year, month, date) =>
  `${pad(year, '0', 4)}-${pad(month + 1, '0', 2)}-${pad(date, '0', 2)}`);

const now = new Date();
const template = parseTemplateHtml('#my-date-template');
const context = {
  year: now.getFullYear(),
  month: now.getMonth(),
  date: now.getDate()
};

document.body.innerHTML = template(context); // Rendered HTML;
<script src="https://cdnjs.cloudflare.com/ajax/libs/handlebars.js/4.7.7/handlebars.min.js"></script>

<script id="my-date-template" type="text/x-handlebars-template">
  <div class="my-date" data-value="{{formatDate year month date}}">
    {{formatDate year month date}}
  </div>
</script>

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