question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Self closing custom tag?

See original GitHub issue

Is it possible to create self-closing custom tags? Is there some obvious thing I am missing, or would this require a change to the core?

For example, I want to be able to do this:

{% visor %}

In this situation I want my tag to output HTML, and will never accept content.

For reference, I’d prefer to do the above compared to the below, which I’m doing now:

{% visor %}{% endvisor %}

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:11 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
garygreencommented, Jul 18, 2014

It would be difficult/impossible for nunjucks to determine what is a self closing tag and what is not, unless a difference syntax is used like:

{% visor /%}

Similar to html self closing tag but I really don’t see much use for this. You mention:

In this situation I want my tag to output HTML, and will never accept content.

Wouldn’t it be easier to pass a function to your view and do:

 {{ visor() }}

If the function is required across all your views, make use of the new addGlobal(name, func) function.

0reactions
facelegcommented, Aug 9, 2014

I do use template inheritance. My example is bare bones for simplicity.

I’m part of the dev team at a web studio, working on what I hope to be our next CMS system.

The example focuses on the page-meta module that I want to have the ability to inject & manage meta for all pages - be they CMS style pages that are created by the user or for controller style pages, pages from another module (i.e. a product management module), whatever. The sole responsibility of said module is to manage meta for pages. No other module will touch meta.

The great thing about nunjucks is that it has allowed me to code the meta module so it creates a meta block that can then be used in templates. The meta block will be given the relevant page (page is a local), and output nothing or the meta the end user (client) has entered to be the meta data for that page.

This sort of thing allows us to “create once”, i.e. the meta module, then deploy it without modification, saving time. The client can edit the meta using said module’s admin features, we don’t ever have to think about meta tags again. For us, time is limited and this kind of thing helps immensely.

  • The CMS module adds a cmsblock block that allows the dev to set default HTML, then wraps it in markup appropriate for Raptor to trigger for users with the correct permission.
  • Similarly, the visor module allows other modules to add “admin links”, that are spat out in the visor block.
  • The auth module creates a permission block that allows devs to wrap portions of template in a block that only outputs the contained HTML if the user accessing the page has the relevant permissions.

I do understand template inheritance (it is awesome, I love nunjucks). I also understand your confusion as to why the hell anyone would want to do waht we’re doing. Believe me, for us it will be a massive time saver and allow us to focus on the fun parts of being at a web studio.

I realise we’re coming from completely different angles, that’s fine. Nunjucks is great and I can’t tell you how much I appreciate the effort you’ve all poured into creating it.

Self closing tags, async globals would be great for us, but nunjucks is no hardship without them!

Thanks!

P.S. apologies for the wall-o-text.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Do custom elements require a close tag? - Stack Overflow
Yes. Custom elements require a closing tag. Only certain tags in HTML are allowed to be self-closing due to the parser.
Read more >
Custom 'void' or self-closing elements (HTML parser ...
It's just another place for errors. A self closing tag means "the child content of this element is meaningless, and should even be...
Read more >
[HTML5/JS] Define a custom self closing (void) HTML ...
Self closing HTML elements do not require or support a closing tag. Modern browsers support custom element tags and behavior using document.
Read more >
How to deal with custom self closing tags after jQuery 3.5.0?
Closed Question heavily relies on custom self-closing HTML tags, e.g. <inlinechoice id="a" /> . These are processed server side as XML, but now ......
Read more >
Custom Price Tags - Self Closing - No String Needed - Etsy
112 TAGS - Custom Price Tags - Self Closing - No String Needed - Personalized Custom Printed. QUANTITY: 112 SIZE: 1x1 square tops,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found