在ASP.Net我通常使用标准web.sitemap
文件开我的主导航如下列:到ASP.Net在PHP web.sitemap中最佳替代
<?xml version="1.0" encoding="utf-8" ?>
<siteMap xmlns="http://schemas.microsoft.com/AspNet/SiteMap-File-1.0" >
<siteMapNode url="~/index.html" title="My Awesome Site">
<siteMapNode url="~/About/index.html" title="About">
<siteMapNode url="~/About/Board.html" title="Board"/>
<siteMapNode url="~/About/Vision.html" title="Vision"/>
<siteMapNode url="~/About/Mission.html" title="Mission"/>
<siteMapNode url="~/About/Support.html" title="Support"/>
<siteMapNode url="~/About/Locations.html" title="Locations"/>
<siteMapNode url="~/About/Funding-Sources.html" title="Funding Sources"/>
<siteMapNode url="~/About/Volunteer.html" title="Volunteer"/>
<siteMapNode url="~/About/Staff.html" title="Staff"/>
</siteMapNode>
<siteMapNode url="~/Parents/index.html" title="Parents">
<siteMapNode url="~/Parents/Child-Development.html" title="Child Development"/>
<siteMapNode url="~/Parents/Referrals.html" title="Referrals"/>
</siteMapNode>
<siteMapNode url="~/Resources/index.html" title="Resources">
<siteMapNode url="~/Resources/Resource-Library.html" title="Resource Library"/>
<siteMapNode url="~/Resources/Links.html" title="Links"/>
</siteMapNode>
</siteMapNode>
</siteMap>
然后我通常创建用于用户控制导航,我可以使用SiteMap.CurrentNode
和/或SiteMap.CurrentNode.IsDescendantOf
将当前页面标记为“已选”或其他内容。我正在寻找类似于PHP的东西。在ASP经典中,我们几乎不得不使用include和一组if
语句,但是一旦深入几级,就会得到一堆代码。我可能会在每个页面的顶部创建变量,指定我们正在使用哪个“部分”,“子部分”和“页面”,但如果可能的话,我想避免这种情况。
我发现this link它通过查询字符串反弹的事情,但是这不是一个选项。
如果有谁不熟悉web.sitemap
文件,如果需要,我可以解释它的更多细节。
编辑 - 评论对@BrandonS
什么我要找的核心是能够定义一个代表我的网站页面的单个文件。 ASP.Net使用一个XML文件,它很方便捕捉一些潜在的错误,但不是必需的。理想情况下,我实际上想使用ASP.Net使用的完全相同的文件格式,以便我可以共享代码,但仍不是绝对必需的。
在ASP.Net中,使用上面的站点地图,我将有一个用户控件(基本上是一个荣耀的包含文件),它在每个页面上输出<title>
标记。在主页页它会输出只是My Awesome Site
,该关于 L2它会输出My Awesome Site - About
页和董事会 L3它会输出My Awesome Site - About - Board
页面上。子页面上的面包屑可能会使用同样的想法。 (快速预留,为我所有的网站页面只出现一次,因此没有有两个家长页的机会。)
在ASP.Net每个页面请求的地图对象被自动创建。 (其实我觉得它的惰性加载意味着它实际上还没有生成,除非您尝试访问,但这种情况发生透明。)因此,在任何给定的时刻,我可以存在于Sitemap文件的任何页面上使用SiteMap.CurrentNode
对象。 (如果页面没有在网站地图文件返回null
存在)。从我可以要求SiteMap.CurrentNode.ParentNode
或者我可以走了SiteMap.CurrentNode.ChildNodes
。我也可以获得代表站点地图文件根目录的SiteMap.RootNode
。从那我可以走SiteMap.RootNode.ChildNodes
知道这些直接的孩子代表我的网站的2级页面。然后,我可以单独行走这些孩子,知道每个“孙子”代表一个L3等等。
因此,使用上述递归我可以走根节点的孩子定义我的网站的主要导航,如果我在我的资产净值有下拉菜单然后我就可以走子儿。在步行孩子或孙辈时,我可以根据SiteMap.CurrentNode
是否等于或小孩或孙子的后代来设置课程。下面是VB中的一个例子。我如何使用站点地图为建设L2导航网:
''//An array that we append potential CSS class names to such as "first" or "active"
Dim Classes As List(Of String)
''//A StringBuild is just an efficient way to work with Strings
''//if you are not familiar with it just now that Append() is basically "&=" and AppenLine() is basically "&= ...\n"
Dim Buf As New StringBuilder()
Buf.AppendLine(" <ul>")
''//Walk the root child nodes, the N variable will be the current ChildNode of the SiteMap.RootNote
For Each N As SiteMapNode In SiteMap.RootNode.ChildNodes
''//For the first and last child we want to add extra classes in case we need to adjust borders, padding, etc
Classes = New List(Of String)
If N.PreviousSibling Is Nothing Then
Classes.Add("first")
ElseIf N.NextSibling Is Nothing Then
Classes.Add("last")
End If
''//See if the page being requested is the current child or if the current page is descended from it so that we can mark it as active
If SiteMap.CurrentNode IsNot Nothing Then
If SiteMap.CurrentNode.Equals(N) OrElse SiteMap.CurrentNode.IsDescendantOf(N) Then
Classes.Add("active")
End If
End If
''//This code below is just for outputting the <li class="..."><a href="...">...</a></li> code
''//Write the opening list tag
Buf.Append(" <li")
''//If the above code created any CSS classes append the class attribute and the space-delimited list of classes
If Classes.Count > 0 Then
Buf.Append(" class=""" & Join(Classes.ToArray(), " ") & """")
End If
''//Close the list tag
Buf.Append(">")
''//Append the hyperlink. The "N" object is the current child of the SiteMap.RootNode.ChildNodes
''//All SiteMapNode objects have a Url property and a Title property which is defined in the XML file
Buf.AppendFormat("<a href=""{0}"">{1}</a></li>", N.Url.Replace("/index.html", "/"), HttpUtility.HtmlEncode(N.Title))
''//Append a blank line, I like to keep my code formatted nicely
Buf.AppendLine()
Next
''//Append the closing list tag
Buf.AppendLine(" </ul>")
附加说明
有些人可能会认为这是矫枉过正,但是这是所有内置的.Net,这就是为什么我询问是否存在PHP的东西。我可以推出自己的产品,但显然我希望尽可能避免使用它。
Chris我认为我可以提供帮助,但是因为PHP中没有ASP类的直接端口,因此无法直接访问正确的PHP类,因此我正在寻找的主要功能是什么,因为我知道20这将满足大多数ASP类的规范,但如果你将成为一组特定的函数,我可以建议更适合你。我想我在说你想做什么? – BrandonS 2011-05-06 16:45:33
如果你想使用一个完全像ASP的web.sitemap文件,你将需要编写一个类来执行此操作。大多数框架都有内置的东西来处理导航。所以也许告诉我们你使用的是什么框架会有所帮助。 Zend,Symphony,Cake,CodeIgniter? – 2011-05-06 19:07:51
@BrandonS,我回答了上面的一些细节。 @Harmon Wood,我通常以Zend框架为目标。对于每个人来说,我知道我可以推出我自己的XML解析代码,我只是希望不能构建某人已经做过的事情,并且迄今为止我的搜索没有返回任何内容。此外,我不希望整个CMS(WordPress,Joomla等),我只是寻找一个导航助手。 – 2011-05-06 19:29:13