Smarty的比。的JavaScript / AJAXSmarty、JavaScript、AJAX

2023-09-10 19:13:36 作者:终遇你

我有个疑问: - 有没有标准/约定,当我应该使用Smarty的模板,当我应该使用JavaScript Ajax调用生成内容?我可以使用Ajax / JavaScript调用动态生成的内容。

我的应用程序同时使用Ajax和 Smarty的的,但我想为开发人员提供了规则

解决方案

您应该只使用AJAX调用来加载不知道此刻的动态数据的页面加载 。例如,当你点击评论链接对堆栈溢出给定的问题/答案,一个AJAX调用,以动态地加载数据。这是点击注释链接,网页加载的不结果的用户的结果。你不知道,你应该显示在页面加载时间这些意见,所以它是适当的在这情况下,AJAX调用。

您应该使用模板来显示页面加载是已知目前任何数据。它可以更容易地处理那些禁用了JavaScript(我知道,不是很多)人,并且它提供了从presentation的明确分离的逻辑。用模板的另一个重要好处是,是可显著降低从客户端的浏览器发出的HTTP请求的数目这一事实。

前端小白必学基础之js基础 ajax返回数据处理

这是在移动浏览世界,在那里等待时间,而不是带宽,是最大的障碍尤为重要。在移动Safari浏览器,例如,一个HTTP请求到Smarty的模板化网页将显著更快加载比请求加载javascript,模板页,使五六个额外的HTTP请求。这是特别真实使用EDGE,3G和其它非无线移动数据服务时。其实,这是非常重要的,它是在雅虎的的最佳实践加快你的网站

在理想情况下,你也应该适度降低功能时,Javascript已禁用。一个很好的例子是自动完成的搜索框。这真的很酷的,你键入建议的搜索字词神奇地出现,但是如果你关闭JavaScript,你仍然有一个功能的搜索框。这是在服务好退化的一个典型例子。堆栈溢出一般做了伟大的工作提供了坚实的非JavaScript的经验。它达不到一个地方是在评论。当JavaScript被禁用,只有最流行的注释显示,并发布新的回应是禁用的。

除非绝对必要,您应该考虑的Javascript作为可能没有启用,而不是东西,应该被用于构建关键部分你的网站奖金特点。有明显的例外情况(有些事是不能没有的Javascript来完成)。你会发现,例如,堆栈溢出是非常有用的关闭JavaScript。你不会得到实时更新,当新的答案张贴,或花哨的实时降价previews,但核心功能仍然存在。所有的繁重的完成与HTML和CSS。 JavaScript是只是锦上添花(固然很好结冰)在蛋糕上。这是怎样的一个侧面说明,但它很重要的就更不用说了。

I have a doubt: - Is there any standard/convention that when should I use "Smarty templating" and when should I use Javascript Ajax calls to produce the content? I can use Ajax/Javascript calls to produce the content dynamically.

My application uses both Ajax and Smarty, but I want to set a rule for developers

解决方案

You should only use AJAX calls to load dynamic data that is not known at the moment the page is loaded. For example, when you click the "comments" link for a given question/answer on Stack Overflow, an AJAX call is made to dynamically load the data. This is a result of the user clicking the comments link, not a result of the page loading. You don't know that you should show those comments at the time the page is loaded, so it is appropriate to make an AJAX call in this case.

You should use templating to show any data that is known at the moment the page is loaded. It makes it easier to deal with people that have Javascript disabled (I know, not a lot), and it provides a clear separation of logic from presentation. Another important benefit of using templating is the fact that is can significantly decrease the number of HTTP requests made from the client's browser.

This is especially important in the mobile browsing world where latency, not bandwidth, is your biggest obstacle. In mobile Safari, for example, a single HTTP request to a Smarty-templated page will load significantly faster than a request to load a Javascript-templated page that makes five or six additional HTTP requests. This is especially true when using EDGE, 3G, and other non-wifi mobile data services. In fact, this is so important that it is the first guideline in Yahoo's Best Practices for Speeding Up Your Website.

Ideally, you should also gracefully degrade functionality when Javascript is disabled. A good example is an auto-completing search box. It's really cool to have suggested search terms magically appear as you type, but if you turn off Javascript, you still have a functional search box. That's a classic example of a good degradation in service. Stack Overflow generally does a great job providing a solid non-Javascript experience. One place it falls short is in the comments. When Javascript is disabled, only the most popular comments are displayed, and posting new comments is disabled.

Unless absolutely necessary, you should think of Javascript as a bonus feature that might not be enabled, not as something that should be used to construct critical pieces of your website. There are obviously exceptions (some things just can't be done without Javascript). You'll notice, for example, that Stack Overflow is very usable with Javascript turned off. You won't get the real-time updates when new answers are posted, or fancy real-time Markdown previews, but the core functionality is still there. All the "heavy lifting" is done with HTML and CSS. Javascript is just icing (admittedly very good icing) on the cake. This is kind of a side note, but it's important enough to mention.