在 Smarty 中使用 Recess 框架时如何指定 Content-type

发布于 2024-12-26 20:07:20 字数 732 浏览 1 评论 0原文

我目前正在使用 PHP Recess 框架和 Smarty 模板引擎。在我的控制器中,我的代码类似于:

/**
* !View Smarty
* !RespondsWith Smarty
* !Prefix Views: templates/, Routes: /
*/

class XHomeController extends Controller {

    /** !Route GET */
    function index()
    {
            $this->title = "Some title...";
    }

}

并且在相应的 Smarty 视图中,我像往常一样引用 {$title}

该视图在除 Android 浏览器(在我的 2.3 Nexus One、3.2 平板电脑以及 Android 模拟器上)之外的所有浏览器中都能正确呈现。我认为我已经将问题追溯到这样一个事实:Smarty 视图正在渲染并发送到没有内容类型的浏览器。

使用 http://web-sniffer.net/,我注意到响应中的内容类型为空。

使用Smarty时如何指定Recess中的Content-type?我尝试将 header('Content-type: text/html') 添加到控制器中的方法,但这不起作用。

知道我做错了什么吗?

I am currently using the PHP Recess framework with the Smarty templating engine. In my controller, I have code similar to:

/**
* !View Smarty
* !RespondsWith Smarty
* !Prefix Views: templates/, Routes: /
*/

class XHomeController extends Controller {

    /** !Route GET */
    function index()
    {
            $this->title = "Some title...";
    }

}

and, in the corresponding Smarty view, I refer to {$title} as usual.

The view renders correctly in all browsers except Android browsers (on my 2.3 Nexus One, on a 3.2 tablet as well as in the Android emulator). I think that I've traced the problem to the fact that the Smarty view is being rendered and sent to the browsers without a Content-type.

Using http://web-sniffer.net/, I notice that Content-type in the Response is empty.

How can I specify the Content-type in Recess when using Smarty? I've tried adding header('Content-type: text/html') to the method in the controller but this does not work.

Any idea of what I'm doing wrong?

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

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

发布评论

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

评论(1

楠木可依 2025-01-02 20:07:20

我想在凹槽/框架/视图中查看 SmartyView 代码。该类应该有一个 canRespondWith() 方法,该方法将验证视图是否可以使用特定的 MIMEType 进行响应。例如:

class XmlView extends AbstractView {

    public function canRespondWith(Response $response) {
            return 'xml' === $response->request->accepts->format();
    }
}

如果返回 true,则将使用 XmlView。在 AbstractView 类中,sendHeaders() 方法将设置 Content-Type:

protected function sendHeadersFor(Response $response) {
    header('HTTP/1.1 ' . ResponseCodes::getMessageForCode($response->code));

    $format = $response->request->accepts->format();
    header('Content-Type: ' . MimeTypes::preferredMimeTypeFor($format));
    /* ... */
}

查看 escape/http/MimeTypes.class.php 以查看“xml”如何使用正确的标头进行响应。您还需要查看 SmartyView 以查看您返回的 mimetype,以了解将设置什么标头。

I would like to see the SmartyView code in recess/framework/views. That class should have a canRespondWith() method that will verify if a view can respond with a certain MIMEType. For example:

class XmlView extends AbstractView {

    public function canRespondWith(Response $response) {
            return 'xml' === $response->request->accepts->format();
    }
}

If this returns true, then the XmlView will be used. In the AbstractView class, the sendHeaders() method will set the Content-Type:

protected function sendHeadersFor(Response $response) {
    header('HTTP/1.1 ' . ResponseCodes::getMessageForCode($response->code));

    $format = $response->request->accepts->format();
    header('Content-Type: ' . MimeTypes::preferredMimeTypeFor($format));
    /* ... */
}

Look in recess/http/MimeTypes.class.php to see how 'xml' will respond with the correct headers. You also need to look in your SmartyView to see what mimetype you are returning to see what header will be set.

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