我应该如何将一些 javascript 集成到 php 应用程序中?

发布于 2024-09-27 05:09:23 字数 829 浏览 3 评论 0原文

我有一个中等规模的遗留 php 应用程序,几乎没有 javascript 集成。我最近一直在使用 Douglas Crockfords 的优秀书籍 并采用 YUI 作为我选择的库。我已经尝试了一些事情并得到了一些工作,但现在我想将各种组件等正确集成到我的应用程序中,我的问题是如何做到这一点以便于维护和代码重用。

目前,该应用程序由

  • 页面每个部分的php 页面
  • smarty 模板(带有一些特殊模板标记的 html 模板)组成,因此单个页面可以使用多个模板。
  • 单个 css 文件。

关于如何将我的 javascript 集成到页面中,我有以下想法。

  1. 使用该部分所需的代码在每个 smarty 模板中编写内联 javascript。
  2. 编写一个单独的 .js 文件来配合链接的每个 smarty 模板,然后编写一个内联脚本来运行它。
  3. 每个 php 页面都有一个单独的 .js 文件,其中包含整个 .php 页面所需的所有功能。一个小的内联函数将调用所需的任何函数。
  4. 有什么我没想过的吗?

这有什么意义吗?有人对此有好的建议吗?

更新

一点额外的信息是它是一个内部应用程序,因此将所有内容限制在单个文件中并不那么重要。

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.

  1. Write inline javascript in each smarty template with the code required for that section.
  2. Write a seperate .js file to go with each smarty template that is linked in and then a single inline script to run it.
  3. 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.
  4. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

走过海棠暮 2024-10-04 05:09:23

其他两个选项:

  1. 一个 JS 文件,其中包含整个站点的所有 JS。根据您的缓存设置,您应该能够让用户为整个站点仅下载一个文件,并为每个其他页面使用缓存版本。
  2. 根据功能而不是页面划分 JS,并包含每个页面所需的任何功能。例如,一个页面可能需要选项卡、表单验证和动画,而另一页面可能只需要选项卡。

或者,您可以混合使用这些文件:一个包含绝大多数代码的 JS 文件和一个额外的文件(如果特定页面或模板需要)。

所提到的方法都没有错误(尽管我会尽可能跳过内联 JS 方法);哪一种最好取决于您的具体情况和您的个人喜好。

Two other options:

  1. A single JS file that contains all the JS for your entire site. Depending on your caching settings, you should be able to get the user to download just one file for the entire site and use a cached version for every other page.
  2. Divide your JS up according to function, rather than page, and include whatever functionality each page requires. E.g. one page may require tabs, form validation and animation, while another may only require tabs.

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.

年华零落成诗 2024-10-04 05:09:23

首先,大多数设置都允许使用主布局模板,您可以在其中放置公共页面开头,或者每个模板都包含一些全局标题。

话虽如此,您应该结合使用 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.

酒中人 2024-10-04 05:09:23

如果你没有大量的 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文