我应该在ejs文件中使用脚本标签吗? [英] Should I be using script tags in ejs files?

查看:64
本文介绍了我应该在ejs文件中使用脚本标签吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习如何开发节点应用程序。该应用程序可以让人们发布整个城市发生的事件。

I'm learning how to develop a node application. It's an app where people can post events happening around the city.

我有一个ejs文件new.ejs,该文件允许用户提交新事件。显然,有一个事件的开始时间和结束时间。我想确保结束时间晚于开始时间,所以我只是添加了一个脚本来完成此操作,如下所示:

I have an ejs file, new.ejs, that allows users to submit a new event. Obviously there is an event start time and end time. I want to make sure that the end time is AFTER the start time, so I simply added a script to do that, as follows:

          <!-- EVENT DATE AND TIME -->
          <div class=row>
              <!-- DATE -->
              <div class="form-group col-md-4">
                  <label for="date">Date *</label>
                  <input name="date" type="date" class="form-control" id="date"> 
              </div>
              <!--START TIME -->
              <div class="form-group col-md-4 ">
                  <label for="starttime">Start Time *</label>
                  <input name="starttime" type="text" class="form-control" id="starttime">
              </div>
              <!--END TIME -->
              <div class="form-group col-md-4 ">
                  <label for="endtime">End Time *</label>
                  <input name="endtime" type="text" class="form-control" id="endtime">
              </div>
              <script type="text/javascript">
                    $('#starttime').timepicker();
                    $('#endtime').timepicker({
                        'minTime': '12:00am',
                        'showDuration': true
                    });
                    $('#starttime').on('changeTime', function() {
                        $('#endtime').timepicker('option', 'minTime', $(this).val());
                    });
              </script>
          </div> <!-- END OF ROW -->  

现在,这很好,它可以执行我想要的操作。

Now this works just fine, it does what I want it to do.

但是,我知道EJS旨在接收后端(节点)JavaScript代码并将其呈现到视图中。

However, I know that EJS is designed to take back-end (node) javascript code and render it out into the view.

我的问题是:


  1. 是否在标记之间添加前端代码?也就是说,这是正确的编码做法吗?如果没有,什么是更好的方法呢?

  1. Is adding front-end code between tags a hack? I.e, is this proper coding practice? If not, what is a better way of doing this?

现在,我的脚本标签之间只有少量代码。在我继续开发应用程序时,如果代码太长会发生什么?它应该保留在ejs文件中吗?似乎太乱了...

Right now, I just have a small amount of code between my script tags. As I continue to develop the application, what happens if the code gets way too long? Should it remain in the ejs file? Seems too messy...


推荐答案


  1. 如果您的应用是服务器端呈现的(不是单页应用),则您的方法是正确的。

  1. If your app is server-side rendered (not a Single Page Application), then your approach is sound.

您可以将代码放入 / public 文件夹中的JS文件,并将您的节点服务器配置为将这些文件作为静态文件提供,并添加< script> .ejs 文件中的c>标记引用了这些JS文件。

You can put the your code in JS files in a /public folder and configured your node server to serve those files as static files, and add <script> tags in the .ejs files that reference those JS files.

我有一个带有这种实现的示例项目。 将是您的ejs文件(情况)和就是放置脚本文件的位置。最后,将您的应用程序配置为从

I have a sample project with such an implementation. This will be your ejs file (jade for my case) and this is where you put your script files. Lastly, configure your app to serve static assets from a directory like this.

这篇关于我应该在ejs文件中使用脚本标签吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆