Web 2.0 生态系统/堆栈
作为前端网站开发的新手,我可以理解一些东西,比如路由、ORM 等。我不明白的是它们是如何一起发挥作用的。我的理解是,使用 Pyramid/Django 等构建的网站有很多组件:
模板引擎:可以让您从代码中抽象出 HTML 的东西。有道理。
SQLAlchemy 等人:ORM。很好。
渲染器。不知道。
JS 库:JQuery 等: 除了添加漂亮的效果之外,不知道这些有什么用。它如何与模板引擎交互?它如何与整个框架交互?我可以在 Pyramid 中编写 jquery 代码,还是单独编写 JS,将 JS 文件插入我的模板或...?
表单模板库(formish、formalchemy 等):这些与总体情况有何关系?它们插入哪里?
我还缺少其他重要组件吗?
那么,有人可以帮我解释一下堆栈吗?
Being new to front-end website development, I can understand some stuff, things like routes, ORM, etc. What I don't understand is how they all play together. My understanding is, there are a bunch of components for a website built with Pyramid/Django etc:
A templating engine: Something for you to abstract away your HTML from your code. Makes sense.
SQLAlchemy et al: An ORM. Fine.
A renderer. No idea.
JS libraries: JQuery et al:
No idea what use these are except for adding pretty effects. How does this interact with the templating engine? How does this interact with the entire framework? Can I write code for jquery in Pyramid, or do I write JS separately, plug in my JS file into my template or...?Form templating libraries (formish, formalchemy et al): How do these relate to the big picture? where do they plug in?
Any other important components that I'm missing?
So, could someone help me out and explain the stack?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
其中有几个可用。 Mako 尝试在模板中利用许多常见的 Python 习惯用法,以避免学习许多新概念。 Jinja2 与 Django 类似,但具有更多功能。如果您喜欢基于 XML 的模板,Genshi 就是您的最佳选择。
不幸的是,作为一个刚接触这一切的人,很难说哪个最容易开始。也许是金贾2。
是的。
渲染器是 Pyramid 视图配置选项,它告诉 Pyramid 如果您的视图返回一个字典,那么它应该传递给给定的“渲染器”。渲染器设置为使用扩展名,并且 Pyramid 附带了几个内置的:
http://docs.pylonsproject.org/projects /pyramid/1.0/narr/renderers.html#built-in-renderers
简而言之,渲染器选项仅查看您传递给它的名称,并找到与扩展名(.mak、.pt)匹配的模板引擎,'json','string',.etc),并用它渲染字典结果。
在许多框架中,您不会将渲染器指定为配置,而是在视图中包含一些代码,如下所示:
在 Pyramid 中,您可以执行相同的操作:
后者是一种有用的功能有几个原因:
您可以添加多个根据其他条件更改渲染器的配置。
考虑这个示例,它根据 HTTP 请求是否是 XHR(需要 JSON 格式结果的 AJAX 请求,而不是需要模板引擎吐出 HTML 的一般 HTTP 请求)来更改渲染器。
JS 库使编写 Javascript 变得更加容易。它们在浏览器中与 DOM 交互,除了向可能需要 JSON 格式结果的 Web 应用程序发送 HTTP 请求之外,不与 Pyramid 交互。
首先,我建议完全忽略 Javascript,直到您更加熟悉 HTML、DOM 树,并获得一个仅使用 HTML、CSS 和 Web 应用程序的网站。
我强烈建议完全忽略这些,并编写基本的 HTML 表单元素。您是整个 Web 堆栈的新手,实际上没有必要在不先熟悉基础知识的情况下直接跳到 Web 开发的最高级方面。
不过,在编写基本表单之后,您将需要一个表单验证库,它可以更轻松地验证提交的表单是否包含有效参数。回到 PHP 的旧时代,人们会编写数百行通过表单的 if/else 语句(有些人仍然这样做!ack!)。
如今,我们使用表单验证库,可以轻松声明表单的有效参数。我建议首先使用 FormEncode,因为它相当容易使用只是进行验证。对于 Pyramid,使用 FormEncode 最简单的方法可能是 Pyramid_simpleform:
http://packages.python.org/pyramid_simpleform/
现在,忽略表单渲染部分并编写自己在模板中添加 HTML 表单元素,并使用 Pyramid_simpleform 只是为了简单的 FormEncode 集成。
简而言之,首先使用视图函数和模板显示带有链接的 HTML 页面(并使用 URL 调度,对于初学者来说比遍历更容易掌握)。然后添加表单、它们的 HTML 和验证,然后添加 CSS 来开始设计样式。
接下来,您可以从一些基本的 Javascript 和 jQuery 开始,使内容在页面上移动,然后通过 AJAX 与 Web 应用程序交互以获取更多数据。只是不要一次处理太多问题,这样应该更容易看出它们如何组合在一起。
There's several of these available. Mako tries to utilize many common Python idioms in the templates to avoid having to learn many new concepts. Jinja2 is similar to Django, but with more functionality. Genshi is if you like XML based templating.
As someone new to the whole thing, it's hard to say which is easiest to begin with unfortunately. Perhaps Jinja2.
Yep.
A renderer is a Pyramid view configuration option, which tells Pyramid that if your view returns a dict, then it should be passed to the given 'renderer'. Renderers are setup to work with extension names, and Pyramid comes with several built-in:
http://docs.pylonsproject.org/projects/pyramid/1.0/narr/renderers.html#built-in-renderers
In short, the renderer option merely looks at the name you pass it, and finds a template engine that matches the extension (.mak, .pt, 'json', 'string', .etc), and renders the dict results with it.
In many frameworks you don't designate a renderer as configuration, but instead have some code inside the view which looks something like this:
In Pyramid, you could do the same thing with:
There are several reasons the latter is a useful capability:
When it's entirely in configuration, you can override the renderer without having to change the view code logic.
You can add multiple configurations that change the renderer based on other conditions.
Consider this example which changes the renderer based on if the HTTP request is an XHR (AJAX request that wants a JSON formatted result, instead of a general HTTP request that wants HTML spit out by the template engine).
JS libraries make it easier to write Javascript. They interact in the browser with the DOM, and have no interaction with Pyramid beyond sending HTTP requests to your web application that might want JSON formatted results.
To begin with, I'd suggest ignoring Javascript entirely until you're much more familiar with HTML, the DOM tree, and getting a site that works with just HTML, CSS, and the web-application.
I would highly suggest ignoring those entirely, and writing basic HTML form elements. You're new to the whole web stack, and there's really no need to jump straight to the most advanced aspects of web development without getting familiar with the basics first.
What you will need though, after writing basic forms, is you will want a form validation library that makes it easier to verify that the form which was submitted contains valid parameters. Back in the old days of PHP, people would write hundreds of lines of if/else statements that went through forms (some still do! ack!).
Nowadays we use form validation libraries which make it easy to declare what the valid parameters are for a form. I'd suggest FormEncode to begin with, as its fairly easy to use just for validation. For Pyramid, the easiest way to get going with FormEncode is probably pyramid_simpleform:
http://packages.python.org/pyramid_simpleform/
For now, ignore the form rendering part and write the HTML form elements in the template yourself, and use pyramid_simpleform just for the easy FormEncode integration.
In short, start with just displaying HTML pages with links using view functions and templates (and use URL dispatch, its easier to grasp than traversal for beginners). Then add forms, their HTML and validation, then add CSS to start styling things.
Next you can start with some basic Javascript with jQuery to make things move around on the page, and work your way up to interacting with the webapp via AJAX to get more data. Just don't tackle too much at once, and it should be easier to see how they fit together.
通常,渲染器会获取您的数据/模型并将其转换为客户端想要的内容。如果客户端只是浏览器,那么渲染器通常会通过模板混合数据以生成 HTML。如果客户端是一些 JavaScript 代码或非浏览器应用程序(桌面应用程序、正在使用数据的另一台服务器……),则渲染器通常会生成 JSON(或可能是 XML)。您可以将其视为序列化或编组系统。
这些是您用来对用户界面进行编程的库。用户界面可能只是 HTML 之上的一些漂亮的效果,但它还可以有更多。例如,Google Docs 就是 JavaScript,而且不仅仅是漂亮的效果; Cloud9 IDE 将是另一个使用 JavaScript 构建的完整应用程序示例(感谢 Raynos 提供的另一个示例)。
您可以将它们视为(或多或少)模板引擎的宏系统。如果您有数据模式,那么您可以使用这些东西来生成模板块并自动处理相应返回数据的服务器端处理。
您可以将现代 Web 堆栈视为传统的客户端服务器系统;这可能会激怒一些人,但除了规模之外,这里并没有什么全新的东西。客户端使用 HTML 和 CSS 进行布局,使用 JavaScript(可能使用工具包)构建功能和视觉效果。该服务器是某种网络服务器。客户端和服务器之间的通信通常通过 HTTP 结合 JSON 和 HTML 来完成。您可以将 web-1.0(愿上帝原谅我的营销术语)视为老式的哑终端,而 web-2.0 更像是在客户端上有一些大脑的 X 终端。
Generally a renderer takes your data/model and converts it into something that the client wants. If the client is just a browser then the renderer will usually mash your data through a template to produce HTML. If the client is some JavaScript code or a non-browser application (a desktop application, another server that is consuming your data, ...) then the renderer would usually produce JSON (or possibly XML). You can think of this as a serialization or marshalling system.
These are what you use to program the user interface. The user interface may just be some pretty effects slapped on top of HTML but it could be a lot more. Google Docs, for example, is JavaScript and a bit more than pretty effects; Cloud9 IDE would be another example full application built with JavaScript (thanks to Raynos for another example).
You can think of these as (more or less) macro systems for the template engine. If you have a data schema then you can use these things to generate template chunks and to automatically handle the server side processing of the corresponding return data.
You can think of the modern web stack as a traditional client server system; this will probably anger some people but there's nothing radically new here except possibly the scale. The client is built with HTML and CSS for the layout and JavaScript (possibly with a toolkit) for the functionality and eye candy. The server is a web server of some sort. Communication between client and server is usually done in a combination of JSON and HTML over HTTP. You can think of web-1.0 (may deity forgive my marketing-talk terminology) as old school dumb terminals where web-2.0 is more like an X-terminal with some brains on the client.