视图如何工作(MVC)?

发布于 2024-08-19 11:04:38 字数 306 浏览 10 评论 0原文

我现在正在开发一个没有框架的网络应用程序,并且我正在尝试将其构建为 MVC 应用程序。问题是,MVC 应用程序的一些技术方面我没有注意到。

首先,应该如何构建视图?我想使用像 eRuby 或 #haml 这样的标记语言,但我不知道它们到底是如何工作的以及如何在非 Rails 应用程序中实现它们。

视图如何从模型获取数据并从控制器获取方向?在 Rails 中,这一切都被混淆了。我怎样才能实现这个?这背后的理论是什么?我是否在视图中连接到我的数据库,然后在那里自由使用它?除了 #haml 渲染器之外,我是否让它通过另一个程序来为其提供数据?

谢谢!

I'm working on a web-app without a framework right now, and I'm trying to structure it as a MVC app. The problem is, there are some technical aspects of MVC apps that escape me.

Primarily, how should a view be build? I'd like to use a markup language like eRuby or #haml, but I don't know how exactly they work and how to implement them in a non-Rails application.

How does the view get data from the model and directions from the controller? In Rails, this is all obfuscated. How can I implement this? What's the theory behind this? Do I make a connection to my database in the view and then use it freely there? Do I have it pass through another program aside from just the #haml renderer to give it data?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

高速公鹿 2024-08-26 11:04:38

我还没有足够的观点来评论,但要回答你对 cwninja 答案的问题,在大多数情况下,你在控制器操作中渲染视图表单。控制器获取请求,执行正确的操作并返回响应,在本例中,渲染的视图作为响应主体。使用 haml 的简单示例如下所示:

class SomeController

 def some_action
   @foo = "bar"
   Haml::Engine.new(template).render(self)
 end
end

在这种情况下,控制器中设置的实例变量将自动可供视图使用,因为它们是在同一上下文中定义的。
模型中的变量将不可用,因为它们不应该可用,但是您可以从控制器操作访问的任何内容都可以从视图访问。
大多数模板系统还允许您将局部变量的散列传递给视图,例如:

   Haml::Engine.new(template).render(self, {:foo => "bar"})

我希望这可以消除您的一些困惑:)

I don't yet have enough points to comment but to answer your question on cwninja's answer, in most cases you render the view form within a controller action. A controller gets a request, executes the right action and return's a response, in this case a rendered view as the response body. A simple example using haml could look like this:

class SomeController

 def some_action
   @foo = "bar"
   Haml::Engine.new(template).render(self)
 end
end

In this case instance variables setup in the controller will automatically be made available to the view since they are defined in the same context.
Variables from the model will not be available as they shouldn't, however anything you can access from the controller action can be accessed from the view.
Most templating systems also allow you to pass along a hash of local variables to the view, eg:

   Haml::Engine.new(template).render(self, {:foo => "bar"})

I hope this clears up some of your confusion :)

私野 2024-08-26 11:04:38

简单情况:

ERB.new("your_template.erb").result(binding)

这将使用调用模板的上下文来评估模板。

我首选/简单的方法是在控制器内部调用它,并处理控制器和视图的稍微合并。

如果你不想这样做,你需要创建一个视图类,它有一个方法如下:

def get_binding
  binding
end

然后创建一个新实例,根据需要在视图中设置所有实例变量,然后调用:

view = ViewClass.new
view.object = my_data_from_the_db
return [200, {…}, [ERB.new("your_template.erb").result(view.get_binding)] ]

或者…放弃并使用 sinatra。

Simple case:

ERB.new("your_template.erb").result(binding)

This will evaluate the template with the context of where it's called.

My preferred/simplistic way of doing this is to just call it inside the controller, and deal with the controller and view being a little merged.

If you don't want to do that, you need to create a view class, that has a method as follows:

def get_binding
  binding
end

Then create a new instance, setting all the instance variables as needed in the view, then call:

view = ViewClass.new
view.object = my_data_from_the_db
return [200, {…}, [ERB.new("your_template.erb").result(view.get_binding)] ]

Or… just give up an and use sinatra.

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