美文网首页
目录展示在网页上解析

目录展示在网页上解析

作者: 两点半的杂货铺 | 来源:发表于2018-03-20 14:43 被阅读7次

一、思路

我们已经明确的获得了,整个的目录关系,由于每一个child的是不停的嵌套,导致我们不知道什么时候是头,所以这里使用递归

  • 我们可以确定的是所用的主根目录,因为主目录是所有的头
    def menu_tree(self):


        response = ' '
        tpl = '''
        <div class="'itme %s">
            <div class="'title">%s</div>
            <div class="content">%s</div>
        </div>
            '''
        for row in self.menu_data_list():
            if not row['status']:
                continue

            active = ''
            if row['opend']:
                active = 'active'

            if 'url' in row:
                response += '<a class=%s href="%s">%s</a>' % (active, row['url'], row['caption'])
            else:
                title = row['caption']
                content = self.menu_content(row['child'])
                response += tpl % (active, title, content)

        return response

  • 我们不知道内容目录什么时候到头所以递归内容目录
    def menu_content(self,child_list):


        response = ' '
        tpl = '''
        <div class="'itme %s">
            <div class="'title">%s</div>
            <div class="content">%s</div>
        </div>
            '''
        for row in child_list:
            if not row['status']:
                continue

            active = ''
            if row['opend']:
                active = 'active'

            if 'url' in row:
                response += '<a class=%s href="%s">%s</a>' % (active, row['url'], row['caption'])
            else:
                title = row['caption']
                content = self.menu_content(row['child'])
                response += tpl % (active, title, content)

        return response

相关文章

网友评论

      本文标题:目录展示在网页上解析

      本文链接:https://www.haomeiwen.com/subject/rgpqqftx.html