What's the use of Jade or Handlebars when writing AngularJs apps

前端 未结 10 1112
情歌与酒
情歌与酒 2020-12-07 08:38

I am new(ish) to the whole javascript full stack applications, and completely new to Angular, so I was hoping somebody can put the record straight for me here.

Why w

相关标签:
10条回答
  • 2020-12-07 08:46

    First of all, you always need some kind of server-side templating.

    Pure client-side templating have huge disadvantages in a loading time, so it's often mitigated by rendering some static elements on the server. This way when user partially loads a page, he'll already see some elements on the page.

    And well, templates are handy in this case, although people sometimes use static html generator like Jekyll instead.


    There is another reason for using Jade that's not mentioned here before.

    Whitespace.

    If you're writing human-maintainable HTML with indentations and line-breaks, every single linebreak will result in a whitespace text node. It can pretty much screw formatting of inline elements in some cases, and make javascript code more weird.

    You can read more details here: https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Whitespace_in_the_DOM

    If you're writing Jade code, it is compiled into one-line HTML that doesn't have this issue.

    0 讨论(0)
  • 2020-12-07 08:47

    Jade is definitely much more closer to html than say Haml. So the context switch is actually very minimal. Yet it is not completely absent. It may not matter to a developer at all. But when your designer comes and asks you how to get a nested tag to work properly, you are solving an unnecessary problem that you created in the first place.

    HTML can still be written very legibly and partials can be used to make it more comprehensible. 500 lines of anything is hard to read - be it Jade or HTML.

    Here is a Jade template

    .product-container
    
        .input-group.msB.col-md-5.no-padding
            .fnt-light-navyblue.mtB(for='name')
                strong Name the sticker
            input.full-input(type='text', placeholder='Awesome Batman Sticker')
        .clear
    
        .form-group.mmT
            label.form-label.fnt-light-navyblue
                strong Choose size
            .selector-group(ng-repeat="size in sizes", ng-class="{ 'msT': !$first}")
                - raw
                span.radio
                    input.radio(name='choose_sticker_size',
                                ng-model="selectedSize",
                                type='radio',
                                value='{{size}}',
                                id="sticker-{{size}}")
                    span.fake-radio
                label(for='sticker-{{size}}') {{size}} inch
                - endraw
        // end form-group
        .clear
    

    And the equivalent HTML

    <div class="product-container">
    
        <div class="input-group msB col-md-5 no-padding">
            <div for="name" class="fnt-light-navyblue mtB">
                <strong>Name the product</strong>
            </div>
            <input type="text" placeholder="Awesome Batman Sticker" class="full-input" />
        </div>
        <div class="clear"></div>
    
        <div class="form-group mmT">
            <label class="form-label fnt-light-navyblue">
                <strong>Choose size</strong>
            </label>
            <div
                class="selector-group"
                ng-class="{ 'msT': !$first}"
                ng-repeat="size in sizes">
                    {% raw %}
                    <span class="radio">
                        <input
                            id="sticker-{{size}}"
                            class="radio"
                            name="choose_sticker_size"
                            ng-model="selectedSize"
                            type="radio"
                            value="{{ size }}" />
                        <span class="fake-radio"></span>
                    </span>
                    <label for="sticker-{{size}}">{{size}}</label>
                    {% endraw %}
            </div>
        </div><!-- end form-group -->
        <div class="clear"></div>
    </div>
    

    When written legibly I dont see HTML to be very particularly disadvantaged so as to warrant a switch. Sure enough, the angular brackets are an eyesore. But I would rather have them, than having to deal with the designer's doubts whether the indirection I introduced is breaking the html. ( It may not. But proving it is not a worthy effort )

    0 讨论(0)
  • 2020-12-07 08:48

    I honestly don't understand why people care about the difference between this:

    <html ng-app>
     <!-- Body tag augmented with ngController directive  -->
     <body ng-controller="MyController">
       <input ng-model="foo" value="bar">
       <!-- Button tag with ng-click directive, and string expression 'buttonText' wrapped in "{{ }}" markup -->
       <button ng-click="changeFoo()">{{buttonText}}</button>
       <script src="angular.js">
     </body>
    </html>
    

    and this:

    html(ng-app="ng-app")
      // Body tag augmented with ngController directive  
      body(ng-controller="MyController")
        input(ng-model="foo", value="bar")
        // Button tag with ng-click directive, and string expression 'buttonText' wrapped in "{{ }}" markup
        button(ng-click="changeFoo()") {{buttonText}}
        script(src="angular.js")
    

    Except that I find one slightly more human-readable. Slightly. I don't get why people are so fervent about the topic. It's all bikeshedding. The difference is negligible and any competent programmer could easily translate one into the other after five seconds on Google. Use what you want and let everyone else quarrel over nothing. Pick your battles and engage in debates about things that actually matter, like atomic reactors ;)

    0 讨论(0)
  • 2020-12-07 08:48
    1. You don't need to use Handlebars with AngularJS since it has it's own template engine.
    2. The reason they use Jade, because it's just a server renderer which will be compiled to html and served by angularJS later on the frontend.

    So TL;DR, on server, you can use whatever language [jade,haml,...] to generate just html structure for your application, it doesn't have anything to do with angularJS since it will render and work with HTML at runtime on frontend.

    You don't have to use Jade on server, and I suggest not using since it will confuse new developers. In projects that you see they use Jade only because it's cleaner and they are used to it, and if it uses with angularJS, it's only job is to generate plain html without any logic.

    0 讨论(0)
  • 2020-12-07 08:51

    I've read all the answers above and was a bit surprised no one had mentioned one aspect which makes using jade over generating AngularJS templates a very useful thing.

    As it already been told, in production, realistic scenarios difference between typing raw html and jade is actually notable, but the more important thing we should never forget is that sometimes we need dynamically changed and reinitialized angularjs templates.

    To put it simple, sometimes we need to change html via innerHTML and then force AngularJS to recompile contents. And this is exactly the type of task when generating such views via jade can have it benefits.

    Also, AngularJS works well with models, which structure is by definition well known. Actually, it happens that we actually don't know the exact structure (imagine, say, JSON renderer). AngularJS will be quite clumsy here (even if were are building an angular app), while jade will do the job.

    0 讨论(0)
  • 2020-12-07 08:54

    When working in a team, front-end likely prefers designing their pages as static html. Translating that static html into dynamic templates is a source of errors, and adding jade adds such translation step.

    As many others, I favour simplicity!

    0 讨论(0)
提交回复
热议问题