SilverStripe PHP 重写 $Layout 变量
嘿...我怎样才能覆盖 $Layout 变量。我想添加一些逻辑,这样如果用户登录,它会显示内容 X(如果不是内容 Y)。我尝试在 Page.php 中定义布局函数,但它不起作用。谢谢。
Hey... how can I override $Layout variable. I would like to add some logic so if a user is logged in it shows content X if not content Y. I tried defining Layout function inside Page.php but it does not work. Thx.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我不认为你真的想重写 $Layout 函数...这用于包含页面类型的适当模板。例如,如果你有一个类名为 NewsPage 的页面类型,那么 SilverStripe 会检查模板内部/包括 NewsPage.ss 文件的目录。如果不存在,那么它将使用该目录中的 Page.ss 文件以及模板目录根目录中的 Page.ss 文件(其中包含 $Layout 变量)请参阅 SilverStripe 模板。
如果您想显示不同的内容,那么一种方法是覆盖 Content 变量。您可以通过重写index()函数来做到这一点,如下所示(将其添加到您的控制器中):
另一种方法是在模板本身中执行此操作,如下所示:
I don't think you really want to override the $Layout function... this is used to include an appropriate template for the page type.e.g. if you had a page type with a class name of NewsPage then SilverStripe would check inside the templates/Includes directory for a NewsPage.ss file. If that didn't exist then it would use the Page.ss file in that directory along with the Page.ss in the root of the templates directory (which has the $Layout variable) See SilverStripe Templates.
If you're wanting to show different content then one way would be to override the Content variable. You could do this by overriding the index() function like so (add this to your controller):
Another way would be to do this in the template itself like this: