2011-05-04 22 views
3

在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的东西。我可以推出自己的产品,但显然我希望尽可能避免使用它。

+0

Chris我认为我可以提供帮助,但是因为PHP中没有ASP类的直接端口,因此无法直接访问正确的PHP类,因此我正在寻找的主要功能是什么,因为我知道20这将满足大多数ASP类的规范,但如果你将成为一组特定的函数,我可以建议更适合你。我想我在说你想做什么? – BrandonS 2011-05-06 16:45:33

+2

如果你想使用一个完全像ASP的web.sitemap文件,你将需要编写一个类来执行此操作。大多数框架都有内置的东西来处理导航。所以也许告诉我们你使用的是什么框架会有所帮助。 Zend,Symphony,Cake,CodeIgniter? – 2011-05-06 19:07:51

+0

@BrandonS,我回答了上面的一些细节。 @Harmon Wood,我通常以Zend框架为目标。对于每个人来说,我知道我可以推出我自己的XML解析代码,我只是希望不能构建某人已经做过的事情,并且迄今为止我的搜索没有返回任何内容。此外,我不希望整个CMS(WordPress,Joomla等),我只是寻找一个导航助手。 – 2011-05-06 19:29:13

回答

2

嗯......据我所知,PHP中没有内置的解决方案。他们有很多框架。

例如在Zend中,我们有navigation containers,可以存储您选择的任何格式的所有数据 - 例如xml-config。然后在zend view navigation helpers的帮助下,我们可以在我们的布局或具体页面中输出它。

作为一个例子,你可以看到它的自动翻译后here

+0

感谢@Alexander,这是迄今为止最接近的,我甚至可以在两种文件格式之间建立映射器。 – 2011-05-09 13:30:04

+0

再次感谢@亚历山大,我要奖赏你的赏金。其他人,感谢您的意见,并感谢您展示我可以建立自己的方式。最终,我的问题是,如果某件事已经存在,并且@亚历山大的答案最符合该法案。 – 2011-05-11 12:57:02

0

澄清我是否正确理解您的目标:您是否希望单个文件处理所有页面的标题和父母?

当然! htaccess :)

RewriteEngine on 
# ROOT 
RewriteRule ^me.html$   me.php?title=Home+Page [L,QSA] 
# ABOUT 
RewriteRule ^about/index.html$ about/index.php?parent=About&title=About [L,QSA] 
RewriteRule ^about/staff.html$ about/staff.php?parent=About&title=The+Staff [L,QSA] 

但是,您不能在文件系统结构中使用.html文件,因为它是静态的。这些文件需要在.php中,但是使用该htaccess的配置,URL将保持为.html。

该网址about/index.html将寻找about/index.php并传递两个变量:父母和标题。这个php文件还需要根据你的URL目录进行组织,如index.php,staff.php在​​文件夹中。

about/index.php文件,可以输出:

<?php 
echo $_GET['parent']; 
echo "<br />"; 
echo $_GET['title']; 

研究更多的htaccess如果我的回答不够,但我认为这是你在找什么。

+0

谢谢@dragonjet,但我不认为这让我需要去的地方。这确实通过一个变量显示我的直接父母,但它不处理一个完整的层次结构,而不会变得非常混乱。我试图找到替代方法的'web.sitemap'文件允许我找出父母和孩子以及兄弟姐妹的信息。 – 2011-05-07 09:24:46

+0

哦,我看到,对于兄弟姐妹,父母和层次结构,那么您可能希望使用SimpleXML解码相同的文件并使用SimpleXML库的内置函数。在所有页面上使用它,最好将它“包含”为一个文件。但它不会对标题有帮助,所以将它与上面的htaccess一起使用。 – dragonjet 2011-05-07 10:24:12