Bootstrap 3的动态菜单生成器:项目和链接

本文介绍了如何使用Bootstrap 3构建动态菜单。内容包括项目(Item)和链接(Link)类的实现,以及如何使用这些类来创建菜单。文章详细展示了如何创建菜单项、设置属性、获取子项以及添加链接等内容,最后给出了如何在实际项目中使用这个菜单构建器的例子。
摘要由CSDN通过智能技术生成

In part 1, we prototyped the end product and wrote the main Menu class, which serves as the menu manager – a container to hold all sub-units (items and links). In this part, we’ll build the remainder of the classes and demonstrate the menu builder’s usage.

在第1部分中,我们对最终产品进行了原型设计,并编写了主Menu类,该类用作菜单管理器–一个容纳所有子单元(项目和链接)的容器。 在这一部分中,我们将构建其余的类,并演示菜单构建器的用法。

项目 (Item)

Represents our menu items as independent objects.

将我们的菜单项表示为独立的对象。

Create a new file called item.php and paste in the following code:

创建一个名为item.php的新文件,并粘贴以下代码:

item.php

item.php

class Item {
	
	protected $manager;
	protected $id;
	protected $pid;
	protected $meta;
	protected $attributes = array();
	
	public    $link;
	
	//...
  • $manager stores a reference to the menu manager (Menu object). This makes us able to use menu manager methods within Item context.

    $manager存储对菜单管理器( Menu对象)的引用。 这使我们能够在Item上下文中使用菜单管理器方法。

  • $id stores the item’s id.

    $id存储项目的ID。

  • $pid stores item’s parent id if it has one otherwise it’ll be set to null.

    $pid存储项目的父ID(如果有的话),否则将设置为null

  • $meta an array for storing extra data with each item.

    $meta一个数组,用于存储每个项目的额外数据。

  • $attributes an array of HTML attributes.

    $attributes分配HTML属性数组。

  • $link stores an instance of class Link.

    $link存储类Link的实例。

__construct(管理员,标题,URL,属性,PID) (__construct(manager, title, url, attributes, pid))

Initializes the attributes.

初始化属性。

public function __construct($manager, $title, $url, $attributes = array(), $pid = 0)
{
	$this->manager     = $manager;
	$this->id          = $this->id();
	$this->pid         = $pid;
	$this->title       = $title;
	$this->attributes  = $attributes;
	
	// Create an object of type Link
	$this->link        = new Link($title, $url);
}
添加(标题,选项) (add(title, options))

Class Item has an add() method as well (just like the menu manager). In fact this method doesn’t create items on its own. It gets the arguments, adds a pid key to $options and calls add() of the menu manager.

Item也具有add()方法(就像菜单管理器一样)。 实际上,此方法不会自行创建项目。 它获取参数,将pid键添加到$options并调用菜单管理器的add()

public function add($title, $options)
{
	if( !is_array($options) ) {
			$options = array('url' => $options);
		}
		
	$options['pid'] = $this->id;
				
	return $this->manager->add( $title, $options );
}

This gives us the power to create sub items in a more semantic way rather than explicitly defining a pid:

这使我们能够以更语义的方式创建子项,而不是显式定义pid

$menu = new Menu;
	
	$about = $menu->add('About', 'about');
	
	// We write it this way
	$about->add('What we do?', 'what-we-do');
	
	// instead of:
	// $menu->add('What we do?', array('url' => 'what-we-do', 'pid' => $about->get_id()));
ID() (id())

Generates a unique id for the Item. We use this identifier to refer to the item later.

生成项目的唯一ID。 我们稍后使用此标识符来引用该项目。

protected function id()
{
	return $this->manager->length() + 1;
}

In fact id() calls length() of the menu manager and increments it by 1.

实际上, id()调用菜单管理器的length()并将其递增1。

get_id() (get_id())

We also need to create a getter method to return the id when needed:

我们还需要创建一个getter方法来在需要时返回id:

public functi
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值