Smarter Ways to Produce a Deep Nested HTML Structure

Smarter Ways to Produce a Deep Nested HTML Structure

< img src= "https://websitedesign-usa.com/wp-content/uploads/2020/10/smarter-ways-to-produce-a-deep-nested-html-structure.jpg" class =" ff-og-image-inserted" > Let’s say we wish to have the following HTML structure: << div class=' boo'> <>< div class='> <>< div class='> <>< div> < div>class=' boo>

‘ >. That’s genuine a discomfort to compose by hand. And the reason that this post was born was being frightened on seeing it created with Haml like this:

< pre rel=" Haml" class=

" wp-block-csstricks-code-block language-javascript" data-line >. boo.boo.boo.boo.boo There were really about twenty levels of nesting in the code I saw, however perhaps some individuals read thing on a cellphone, so let's not fill the whole viewport with boos, even if Halloween is near.

As you can most likely inform, by hand drawing up every level is far from perfect, particularly when the HTML is created by a preprocessor (or from JavaScript, or perhaps a back-end language like PHP). I'm personally not a fan of deep nesting and I do not utilize it much myself, however if you're going all out anyhow, then I believe it deserves performing in a way that scales well and is quickly maintainable.

Let's very first take an appearance at some much better services for this base case and variations on it and then see some enjoyable things done with this kind of deep nesting!

< svg aria-hidden="

real "class= "aal_svg" height=" 16 "variation= "1.1 "viewBox =" 0 0 16 16" width=" 16" >< course fill-rule =" evenodd "d=" M4

9h1v1H4c-1.5 0-3-1.69
-3 -3.5 S2.55 3 4 3h4c1.45 0 3 1.69 3<3.5 0 1.41 -.91 2.72-2 3.25 V8.59 c. 58 -.45 1-1>.27 1-2.09 C10 5.22 8.98 4 8 4H4c -.98 0-2 1.22-2 2.5 S3 9>4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5 S13.98 12 13 12H9c -.98 0-2-1.22 -2 -2.5 0 -.83.42 -1.64 1-2.09 V6.25c-1.09.53 -2 1.84-2 3.25 C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5 S14.5 6 13 6z"/ > The base option What we require here is a recursive technique. With Haml, the following little code suffices

:< pre rel= "Haml" class=" wp-block-csstricks-code-block language-javascript "data-line >

- def nest( cls, n);- return" unless n > 0;.-"< div class=' # cls

.' > # nest( cls, n- 1)"; end= nest ('', 5)There's an emoji class therein since we can and since this is simply an enjoyable little example. I certainly would not utilize emoji classes on a real site, however in other scenarios, I like to have a little bit of enjoyable with the code I compose. We can likewise produce the HTML with Pug:< pre rel=" Pug" class=" wp-block-csstricks-code-block language-javascript" data-line > mixin nest( cls, n) div( class= cls)

if-- n +nest( cls, n) +nest ('', 5

<) Then there's likewise the JavaScript choice:< pre rel= " function nest (_ moms and dad, cls, n) ; nest( document.body,'', 5) With PHP, we can utilize something like this:< pre rel=" PHP" class=" wp-block-csstricks-code-block language-javascript" data-line >

Leave a Reply

Your email address will not be published. Required fields are marked *