在不同的 中填充菜单项
我正在尝试填充 Silverstripe 中的四个菜单项。我已经设法在一个 div 中使用 li class="area{$Pos}" 用 SS 填充前两个菜单项。但是,第三个和第四个菜单项与前两个菜单项的 div 和类不同。我一直在搜索,但不确定如何使用 SS 填充不同 div(/第二个 div> 和类中的第三和第四个菜单项?抱歉,我是新手,所以任何帮助将不胜感激。
这是我的代码。
<div id="top">
<ul class="sf-menu">
<% control Menu(1) %>
<li id="area{$Pos}">
<a href="#a">$MenuTitle</a>
<% if Children %>
<ul><% control Children %>
<li>
<a href="#aa">$MenuTitle</a>
<% if Children %>
<ul>
<% control Children %>
<li>
<a href="#aa">$MenuImg.SetSize(20,20) $MenuTitle</a>
</li>
<% end_control %>
</ul>
<% end_if %>
</li>
<% end_control %>
</ul><% end_if %>
</li>
<% end_control %>
</ul>
</div>
<div id="test" >
<ul class="sf-menu sf-vertical">
<li id="area3">
<a href="#a">Area 3</a>
</li>
<li id="area4">
<a href="#">Area 4</a>
<ul class="show_left">
<li>
<a href="#">Store one</a>
</li>
<li>
<a href="#">Store Two</a>
</li>
</ul>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web
技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我正在尝试填充 Silverstripe 中的四个菜单项。我已经设法在一个 div 中使用 li class="area{$Pos}" 用 SS 填充前两个菜单项。但是,第三个和第四个菜单项与前两个菜单项的 div 和类不同。我一直在搜索,但不确定如何使用 SS 填充不同 div(/第二个 div> 和类中的第三和第四个菜单项?抱歉,我是新手,所以任何帮助将不胜感激。
这是我的代码。
<div id="top">
<ul class="sf-menu">
<% control Menu(1) %>
<li id="area{$Pos}">
<a href="#a">$MenuTitle</a>
<% if Children %>
<ul><% control Children %>
<li>
<a href="#aa">$MenuTitle</a>
<% if Children %>
<ul>
<% control Children %>
<li>
<a href="#aa">$MenuImg.SetSize(20,20) $MenuTitle</a>
</li>
<% end_control %>
</ul>
<% end_if %>
</li>
<% end_control %>
</ul><% end_if %>
</li>
<% end_control %>
</ul>
</div>
<div id="test" >
<ul class="sf-menu sf-vertical">
<li id="area3">
<a href="#a">Area 3</a>
</li>
<li id="area4">
<a href="#">Area 4</a>
<ul class="show_left">
<li>
<a href="#">Store one</a>
</li>
<li>
<a href="#">Store Two</a>
</li>
</ul>
</li>
</ul>
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的
隐私政策
了解更多相关信息。 单击
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文
发布评论
评论(2)
您已经在使用 $Pos 控件(参见代码中的文档),您也可以在“if”块内使用它。
这应该可以解决问题(未经测试):
注意此代码段无法按预期工作。请参阅下面的更新,
只需注意
<% if Pos <% 3 %>
语法不受支持。要以更简洁的方式执行相同的操作,请参阅 此片段。
更新
不幸的是,silverstripe似乎也不支持上述更复杂的条件。
支持这一点(如文档中):
而这个不是:
所以,最好的解决方案似乎是我也向您指出的第二个解决方案(上面 ssbits 上的片段)。
在您的情况下,这可能如下所示:
将函数添加到您用来检查当前位置的“Page”类:
然后,在模板中调用它们:
看起来不太聪明 - 但这次应该可以工作:)
you're already using the $Pos Control (see the docs) in your code, you can also use it inside an 'if' block.
this should do the trick (untested):
BEWARE this snippet doesn't work as expected. see the UPDATE below
just note that
<% if Pos < 3 %>
syntax isn't supported.for a cleaner way to do the same thing, refer to this snippet on ssbits.
UPDATE
unfortunately, silverstripe doesn't seem to support more complex conditions as above either.
this one is supported (as in the docs):
whereas this one isn't:
so, the best solution seems to be the second one i pointed you too (the snippet on ssbits above).
in your case, this could read as follows:
add functions to the 'Page' class you're using to check the current position:
then, call them in your template:
doesn't look very smart - but should work this time :)
恕我直言,您不想在该位置上构建此结构,因为如果您想将其更改为第一个 div 中的 1-3 页和第二个 div 中的 4-5 页,则需要更改基础模板。
相反,我会创建 2 个页面(可能是重定向页面 - 您可以在 CMS 中选择该页面),在其中添加要在模板中显示的页面。所以你会在 CMS 中看到类似这样的内容:
在您的模板中,您可以使用
<% control ChildrenOf(foo) %>
(或bar
)来访问两个不同的页面地区。注意:如果遇到模板错误,请务必将页面置于开发模式并将
?showtemplate=1
附加到您的 URL。这将向您显示已编译的模板文件,您可以在其中找到实际的错误。PS:显然,如果更改 URL,则需要更改模板,但这是我在实时项目中尝试避免的事情(损坏的外部链接,...),而新菜单条目非常常见。
IMHO you do NOT want to structure this on the position, because if you want to change it to 1-3 pages in the first and 4-5 in the second div, you'd need to change the underlying template.
Instead I would create 2 pages (probably redirect pages - you can select that in the CMS) under which you'd add the pages you want to display in your template. So you would have something like this in the CMS:
In your template you could then use
<% control ChildrenOf(foo) %>
(orbar
) to access the pages of your two different regions.NOTE: If you run into template errors, be sure to put your page into development mode and append
?showtemplate=1
to your URL. This will show you the compiled template file where you can then find the actual error.PS: Obviously you'll need to change your template if you change the URLs, but that's something I try to avoid on live projects (broken external links,...), while new menu entries are pretty common.