我应该如何将一些 javascript 集成到 php 应用程序中?
我有一个中等规模的遗留 php 应用程序,几乎没有 javascript 集成。我最近一直在使用 Douglas Crockfords 的优秀书籍 并采用 YUI 作为我选择的库。我已经尝试了一些事情并得到了一些工作,但现在我想将各种组件等正确集成到我的应用程序中,我的问题是如何做到这一点以便于维护和代码重用。
目前,该应用程序由
- 页面每个部分的php 页面
- smarty 模板(带有一些特殊模板标记的 html 模板)组成,因此单个页面可以使用多个模板。
- 单个 css 文件。
关于如何将我的 javascript 集成到页面中,我有以下想法。
- 使用该部分所需的代码在每个 smarty 模板中编写内联 javascript。
- 编写一个单独的 .js 文件来配合链接的每个 smarty 模板,然后编写一个内联脚本来运行它。
- 每个 php 页面都有一个单独的 .js 文件,其中包含整个 .php 页面所需的所有功能。一个小的内联函数将调用所需的任何函数。
- 有什么我没想过的吗?
这有什么意义吗?有人对此有好的建议吗?
更新:
一点额外的信息是它是一个内部应用程序,因此将所有内容限制在单个文件中并不那么重要。
I have a medium size legacy php application with almost no javascript integration. I've recently been learning javascript (yes actually learning it) using Douglas Crockfords excellent book and taken YUI as my library of choice. I've tried out a few things and got a few things working but now I want to integrate the various components etc into my application properly, my question is how to go about doing this for ease of maintenance and code reuse.
Currently the application consists of
- php pages
- smarty templates (html templates with some special template markup) for each section of a page so multiple templates may be used for a single page.
- single css file.
I have the following ideas about how to integrate my javascript into the pages.
- Write inline javascript in each smarty template with the code required for that section.
- Write a seperate .js file to go with each smarty template that is linked in and then a single inline script to run it.
- a seperate .js file for each php page which would have all the functionality required for the entire .php page. A small inline function would call whatever functions were required.
- Something I havent though of?
Does this make any sense? Does anyone have a good suggestion for this?
Update:
One extra bit of info is that its an internal application, so its not so important to restrict everything to a single file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
其他两个选项:
或者,您可以混合使用这些文件:一个包含绝大多数代码的 JS 文件和一个额外的文件(如果特定页面或模板需要)。
所提到的方法都没有错误(尽管我会尽可能跳过内联 JS 方法);哪一种最好取决于您的具体情况和您的个人喜好。
Two other options:
Or you can have a hybrid of these: one JS file that contains the vast majority of your code and an extra file if needed for particular pages or templates.
None of the approaches mentioned are wrong (though I'd skip the inline JS one as much as possible); which one is best will depend on your precise situation and your personal preferences.
首先,大多数设置都允许使用主布局模板,您可以在其中放置公共页面开头,或者每个模板都包含一些全局标题。
话虽如此,您应该结合使用 1、2 和 3:
拥有一个包含在所有包含全局功能的模板中的
.js
。每个模板还可以选择拥有特定于该页面或页面部分的自己的.js
。最后,如果有少量特定于页面的代码(或者必须每次动态生成),则为其启动另一个 http 连接是没有意义的,因此让该源位于模板中。Firstly, most setups allow a master layout template in which you can place a common page opening or, alternatively, each template includes some global header.
That being said, you should do a combination of 1, 2 and 3:
Have a
.js
that is included in all templates that contains global functionality. Each template may also optionally have it's own.js
specific to that page or section of pages. Finally, if there's tiny amounts of code specific to a page (or must be dynamically generated each time), it won't make sense to initiate another http connection for it so have that source be right in the template.如果你没有大量的 javascript,那么创建一个外部 js 文件并将其包含在网页的标题中并完成它。
if you don't have a ton of javascript then create an external js file and include it in the header of the webpages and be done with it.