在 CakePHP 1.2 中自定义 $session->flash 输出的样式和位置
我正在使用 CakePHP 版本 1.2(最新)编写一个简单的 Web 应用程序,并且在显示来自会话帮助程序的 flash 消息时遇到问题。 在我的布局中,存在以下代码:
<?php echo $this->renderElement('flash'); ?>
呈现以下元素:
<?php if($session->check('Message.flash')): ?>
<?php $class = isset($error) ? 'contentError' : 'contentSuccess'; ?>
<div class="contentBlockWrapper <?php echo $class; ?>">
<div class="contentBlock">
<div id="flashMessage" class="flash">
<?php $session->flash(); ?>
</div>
</div>
</div>
<?php endif; ?>
所需的行为是,如果要显示 Flash 消息,则将其显示在 DOM 内的上述位置,以便对其进行样式设置和定位如我所愿。
问题是,如果有闪现消息,CakePHP 会在开始 标记之后立即呈现它。 现在,它还将上述容器呈现在正确的位置,但只是没有将文本写入我想要的位置。
我尝试将输出缓冲区刷新到我的视图中的变量中并回显该变量,但无济于事。 我还读到,有些人尝试使用命名键来指示他们想要显示的特定消息,但这在这里不合适,因为我想要显示的文本正在显示,只是在错误的位置。
任何帮助将不胜感激 - 谢谢!
I'm writing a simple web application using CakePHP version 1.2 (the latest) and am having an issue with displaying the flash message from the Session helper. In my layout, there exists the following code:
<?php echo $this->renderElement('flash'); ?>
Which renders the following element:
<?php if($session->check('Message.flash')): ?>
<?php $class = isset($error) ? 'contentError' : 'contentSuccess'; ?>
<div class="contentBlockWrapper <?php echo $class; ?>">
<div class="contentBlock">
<div id="flashMessage" class="flash">
<?php $session->flash(); ?>
</div>
</div>
</div>
<?php endif; ?>
The desired behavior is that, if there's a flash message to be displayed, that it be displayed at the above location within the DOM so that it will be styled and positioned as I wish.
The problem is that, if there is a flash message, CakePHP is rendering it immediately after the opening <body>
tag. Now, it also renders the containers described above in the correct location, but simply doesn't write the text where I want it.
I've tried flushing the output buffer into a variable within my view and echo'ing that, but to no avail. I've also read that some folks have tried using named keys to indicate the specific message they want displayed, but that isn't appropriate here because the text that I want to display is being displayed, just in the wrong location.
Any assistance would be appreciated - thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的布局中可能有一个剩余内容,上面写着“
所以闪光灯在那里渲染并清除”。 这就是为什么它没有在您的元素中呈现的原因。
检查您的
~/app/views/layouts/default.ctp
(如果您不使用默认值,则检查其他内容),并让我们知道它是否有帮助。I think you probably have a leftover in your layout which says
So the flash is rendered there and cleared. This is why it is not rendered in your element.
Check your
~/app/views/layouts/default.ctp
(or other if you're not using the default) and let us know if it helps.确保如果您在控制器中调用 setFlash() 时使用了翻译函数,则将“true”作为第二个参数传递给它。 像这样:
$this->Session->setFlash(__('My Message Here', TRUE));
否则,消息将被回显,而不是返回! 您只会看到它添加到页面的 HTML 代码之前,并且发送到 setFlash 的消息将是未定义的,因此不会显示在您期望的位置。
Make sure that if you used translation function when calling setFlash() in your controller, you passed "true" as the second parameter to it. Like so:
$this->Session->setFlash(__('My Message Here', TRUE));
Otherwise, the message will be echoed, not returned! You will just see it prepended to your page's HTML code and the message sent to setFlash will be undefined and thus will not show up in the place where you expect it to.