Nested Javascript Templates … is this possible / valid?

前端 未结 6 1829
小蘑菇
小蘑菇 2020-12-03 18:21

Is it possible to have nested JavaScript template tags e.g.



        
6条回答
  •  生来不讨喜
    2020-12-03 18:56

    Sometimes I think I want nested scipt tags for templating, but this is what you have to do instead:

    Script 1

     
    

    Script 2

     
    

    You can 'namespace' the ID if you really want, to indicate it is a 'child' template. This makes it clearer is it not intended to be used by itself (obviously this is up to you if it makes sense or not).

    After reading T.J.s answer this became the only obvious alternative to what I originally thought I wanted (nested scripts).

    However, you probably don't need nested templates unless you need to make a choice as to which 'child' template is used. If there's only one possible layout for the child template then just use a 'foreach' according to your template engine.

提交回复
热议问题