Theme 类別方法

Theme 类別为你的应用程序提供主題。

instance($name = '_default_', array $config = array())

instance 方法扮演一个多例(multiton)。它会回傳透過 $name 識別的實例。 如果此實例不存在,它会建立一个新的 Theme 實例,使用傳遞的配置陣列。 如果沒傳遞參数,instance 会建立預設的 Theme 實例, 使用指定在 theme 設定档中的預設配置。

靜態
參数
參数 类型 預設 描述
$name 字串
'_default_'
Theme 类別實例名稱。
$config 陣列
array()
主題實例配置。如果該實例已经存在会被忽略。
回傳 一个 Theme 實例
範例
// 取得預設實例
$theme = \Theme::instance();

// 取得一个自訂的實例
$theme = \Theme::instance(
	'custom',
	array(
		'active' => 'custom',
		'view_ext' => '.twig'
	)
);

forge(array $config = array())

forge 方法回傳一个新的 Theme 實例。

如果沒傳遞配置,該配置会從全域的配置档案載入。 請注意,如果你傳遞部份的配置,它会與简介所示的預設值合併, 而不是在你配置档案中的預設值!

靜態
參数
參数 类型 預設 描述
$config 陣列
array()
Theme 實例配置。
回傳 一个 Theme 實例
範例
// 取得一个 Theme 實例
$theme = \Theme::forge(array(
	'active' => 'custom',
	'fallback' => 'default',
	'view_ext' => '.html',
));

view($view, $data = array(), $auto_filter = null)

The view method loads a view from the currently loaded theme. It will try to load it from the active theme first. If it doesn't exist in the active theme, and a fallback theme is defined, it will load it from the fallback theme instead. If it can't be found there either, the request is passed to the View class where it follows the normal flow of locating the view file.

这使用 View::forge() 來回傳檢視。这意味著 Parser 套件也支援定義在主題中的檢視。

靜態
參数
參数 类型 預設 描述
$view 字串
null
檢視名稱。檢視档案名稱應該給相對於主題的路徑。
$data 陣列
array()
值的陣列
$auto_filter 布林
null
設为 truefalse 來設定自动编码,預設是主要的配置設定(app/config/config.php)
回傳 一个新的 View 物件
拋出 \ThemeException,在請求的檢視档案找不到時。
範例
// 取得預設實例
$theme = \Theme::instance();

// 会帶預設設定載入 THEMEDIR.'/template/homepage.php'
$view = $theme->view('template/homepage');

presenter($view, $method = 'view', $auto_filter = null)

presenter 方法載入并回傳一个基於所傳 $view 字串的 Presenter 物件。 它会使用上面記錄的 view 方法來傳遞主題檢視到該表现控件。

靜態
參数
參数 类型 預設 描述
$view 字串
null
表现控件名稱。更多資訊見 Presenter
$method 字串
'view'
在 Presenter 實例中優先於呈现檢視,会被呼叫來处理資料的方法名稱。
$auto_filter 布林
null
設为 truefalse 來設定自动编码,預設是主要的配置設定(app/config/config.php)
回傳 新的 Presenter 物件
拋出 \ThemeException,在請求的檢視档案找不到時。
範例
// 取得預設實例
$theme = \Theme::instance();

/**
 * 在預設設定,会載入定義在
 * APPPATH.'classes/view/template/homepage.php' 的 Presenter,使用
 * THEMEDIR.'/template/homepage.php' 檢視档案
 */
$presenter = $theme->presenter('template/homepage');

asset_path($path)

asset_path 方法回傳指向在 $path 中請求的 asset 路徑,相對於 DOCROOT。 如果配置的 asset 文件夾是一个指向請求 $path 的 URL 会回傳 $path。

靜態
參数
參数 类型 預設 描述
$path 字串 必要 請求的資產。
回傳 字串
範例
// 取得預設實例
$theme = \Theme::instance();

// 会回傳 <img src="/THEMEDIR/assets/img/test.png" />
$img = \Html::img($theme->asset_path('img/test.png'));

請注意,每个 Theme 类別實例都有指派一个 Asset 类別實例。如何使用 Asset 类別實例詳見 進階 頁面。

add_path($path)

add_path 方法能让你在执行階段添加一个主題路徑。

靜態
參数
參数 类型 預設 描述
$path 字串 必要 指向包含主題文件夾的路徑。
回傳
範例
// 取得預設實例
$theme = \Theme::instance();

// 添加 'mythemes' 文件夾到主題搜尋路徑
$theme->add_path(DOCROOT.'mythemes');

add_paths(array $paths)

add_paths 方法能让你在执行階段添加多个主題路徑。

靜態
參数
參数 类型 預設 描述
$paths 陣列 必要 指向包含主題文件夾的路徑。
回傳
範例
// 取得預設實例
$theme = \Theme::instance();

// 添加 'mythemes' 文件夾到主題搜尋路徑
$theme->add_paths(
	array(
		DOCROOT.'mythemes'
	),
);

active($theme = null)

active 方法能让你設定活躍主題。它会回傳活躍主題的定義陣列。

靜態
參数
參数 类型 預設 描述
$theme 字串
null
要選擇的活躍主題名稱。如果为 null,它会回傳目前活躍主題。
回傳 陣列
拋出 \ThemeException,當請求的主題找不到。
範例
// 取得預設實例
$theme = \Theme::instance();

// 設定活躍主題为 'darkglow',并回傳其定義
$active = $theme->active('darkglow');

fallback($theme = null)

fallback 方法能让你設定備用主題。它会回傳備用主題的定義陣列。

靜態
參数
參数 类型 預設 描述
$theme 字串
null
要選擇的備用主題名稱。如果为 null,它会回傳目前備用主題。
回傳 陣列
拋出 \ThemeException,當請求的主題找不到。
範例
// 取得預設實例
$theme = \Theme::instance();

// 設定備用主題为 'basic',并回傳其定義
$fallback = $theme->fallback('basic');

get_template()

get_template() 方法将回傳目前載入主題樣板的 View 實例。

靜態
參数
回傳 View
範例
// 取得預設實例
$theme = \Theme::instance();

// 檢索目前樣板來設定頁面标題
$theme->get_template()->set('title', 'This is the page title');

set_template($template)

set_template 方法能让你为頁面設定主題樣板。

靜態
參数
參数 类型 預設 描述
$template 字串 必要 要載入的主題樣板檢視的名稱。
回傳 View
範例
// 取得預設實例
$theme = \Theme::instance();

// 設定頁面樣板为 subpage 布局,并設定頁面标題
$theme->set_template('layouts/subpage')->set('title', 'Subpage title');

get_partial($section, $view)

The get_partial method allow you to get the view instance of a previously set partial in a named section of your page template.

靜態
參数
參数 类型 預設 描述
$section 字串 必要 你想要取得局部的頁面樣板段落的名稱。
$view 字串 必要 要給局部使用的檢視名稱
回傳 View
範例
// 取得預設實例
$theme = \Theme::instance();

/**
 * Get the View instance of the 'partials/menu' view in the 'sidebar' section of the
 * currently loaded page template, and assign a variable to it.
 */
$theme->get_partial('sidebar', 'partials/menu')->set('class', 'menu green');

When you get a partial, use the name of the view you used to set it. If you assign the same view to the same section multiple times, the first one will be returned. If you have passed a View instance to set_partial(), you can get it using the number of the partial, prefixed with 'partial_'. Example: you retrieve the second partial using the view name 'partial_2'.

set_partial($section, $view, $overwrite = false)

The set_partial method allow you to set a view partial for a named section of your page template.

靜態
參数
參数 类型 預設 描述
$section 字串 必要 The name of page template section you want to add this partial to.
$view 字串|View 必要 The name of view to use for the partial, or a View object.
$overwrite 布林
false
If false, append the partial to any partials already defined for this section. If true, existing contents will be deleted.
回傳 View
範例
// 取得預設實例
$theme = \Theme::instance();

/**
 * Assign the 'partials/menu' view to the 'sidebar' section of the
 * currently loaded page template.
 *
 * In the template, this partial is echo'd out as $partials['sidebar'];
 */
$theme->set_partial('sidebar', 'partials/menu');

partial_count($section)

The partial_count method returns a count of the number of partials defined for the given section.

靜態
參数
參数 类型 預設 描述
$section 字串 必要 The name of page template section you want to check for defined partials.
回傳 整数
範例
// 取得預設實例
$theme = \Theme::instance();

// Get the number of partials assigned to the sidebar
$partials = $theme->partial_count('sidebar');

has_partials($section)

The has_partials method allows you to check if a template section has any partials defined.

靜態
參数
參数 类型 預設 描述
$section 字串 必要 The name of page template section you want to check for defined partials.
回傳 布林
範例
// 取得預設實例
$theme = \Theme::instance();

// Check if we have sidebar partials defined
if ( ! $theme->has_partials('sidebar'))
{
	// some code here to hide the sidebar...
}

get_chrome($section)

The get_chrome method allow you to get the view instance of a previously set partial chrome.

靜態
參数
參数 类型 預設 描述
$section 字串 必要 The name of section you want to get the partial chrome from.
回傳 View
範例
// 取得預設實例
$theme = \Theme::instance();

/**
 * Get the View instance for the chrome assigned to the 'sidebar' of the
 * template and assign a variable to it.
 */
$theme->get_chrome('sidebar')->set('title', 'This is a sidebar');

set_chrome($section, $view, $var = 'content')

set_chrome 方法能让你为你頁面樣板的局部段落定義 chrome。

靜態
參数
參数 类型 預設 描述
$section 字串 必要 The name of page template section you want to add this partial to.
$view 字串|View 必要 The name of view to use for the chome of the partial, or a View object.
$var 字串
'content'
The name of the variable in the chrome view used to output the partial content.
回傳 View
範例
// 取得預設實例
$theme = \Theme::instance();

/**
 * 指派 'chrome/roundedcorners' 檢視
 * 到目前載入頁面樣板的 'sidebar' 段落
 * 來給該局部段落一个圓角邊框。
 *
 * 在 chrome 檢視中,局部輸出会輸出为 $body;
 */
$theme->set_chrome('sidebar', 'chrome/roundedcorners', 'body');

find($theme)

find 方法会檢索定義的搜尋路徑來尋找請求的主題。

靜態
參数
參数 类型 預設 描述
$theme 字串 必要 要尋找的主題名稱。
回傳 混合。如果找到回傳指向主題的路徑,或沒有是 false
範例
// 取得預設實例
$theme = \Theme::instance();

// 尋找 'darkglow' 主題
if ($path = $theme->find('darkglow'))
{
	// 該主題可以在 $path 中找到
}
else
{
	// 無法找到主題
}

all()

all 方法回傳一个在所有主題路徑中的主題陣列,按字母順序排序。

靜態
參数
回傳 陣列
範例
// 取得預設實例
$theme = \Theme::instance();

// 取回所有安装的主題
$themes = $theme->all();

use_modules($enable = true)

use_modules 方法啟用或停用模組的自动前綴功能。 在載入一个主題檢視時,如果啟用,該檢視档案会帶有目前活躍模組的名稱前綴。 如果找不到,它会不帶前綴再檢查一次, 允許來自模組內的全域檢視。

靜態
參数
參数 类型 預設 描述
$enable 布林字串
true
True 和一个包含文件夾名的字串來啟用該功能,false 以停用它。
回傳 Theme
範例
// 取得預設實例
$theme = \Theme::instance();

// 當在 'test' 模組,这会載入主題檢視 'test/controller/view'
$info = $theme->use_modules()->set_partial('content', 'controller/view')->use_modules(false);

// 當在 'test' 模組,这会載入主題檢視 'modules/test/controller/view'
$info = $theme->use_modules('modules')->set_partial('content', 'controller/view')->use_modules(false);

你可以透過 theme.php 配置档案的 use_modules 鍵來設定一个全域的預設值。

load_info($theme = null)

load_info 方法回傳主題的完整資訊陣列。 如果沒有指定主題,会回傳活躍的主題資訊。

靜態
參数
參数 类型 預設 描述
$theme 字串
null
主題名稱。
回傳 陣列
拋出 \ThemeException,當請求的主題找不到。
範例
// 取得預設實例
$theme = \Theme::instance();

// 取得 'basic' 主題的資訊陣列。
$info = $theme->load_info('basic');

如果找不到主題資訊档案,这个方法 Throws \ThemeException 如果 require_info_file 設为 true,或回傳一个空陣列如果 require_info_file 設为 false

save_info($type = 'active')

save_info 儲存主題資訊陣列的內容回到該主題資訊档案。

靜態
參数
參数 类型 預設 描述
$type 字串
'active'
應該被儲存的活躍或備用主題資訊。
回傳 布林
拋出 \ThemeException,當請求的主題找不到。
範例
// 取得預設實例
$theme = \Theme::instance();

// 为活躍主題儲存資訊陣列
$info = $theme->save_info('active');

如果找不到主題資訊档案,这个方法 Throws \ThemeException 如果 require_info_file 設为 true,或回傳一个空陣列如果 require_info_file 設为 false

get_info($var, $default = null, $theme = null)

get_info 方法回傳一个來自主題資訊陣列的指定變数。 如果沒有指定主題,会使用活躍主題的資訊陣列。

靜態
參数
參数 类型 預設 描述
$var 字串 必要 要檢索的資訊變数名稱。
$default 混合
null
如果請求的 $var 不存在要回傳的值。
$theme 字串
null
應該被搜尋的主題資訊档案名稱。
回傳 混合
拋出 \ThemeException,當請求的主題找不到。
範例
// 取得預設實例
$theme = \Theme::instance();

// 取得 'basic' 主題定義的 color,如果沒設定,使用 'blue'
$var = $theme->get_info('color', 'blue', 'basic');

如果你指定一个主題,該值会從主題資訊档案被載入。这是真的,即使指定的主題是目前設为活躍或被动的主題。 對於他們來說,不使用載入(并可能修改)的資訊!

set_info($var, $value = null, $type = 'active')

set_info 方法能让你在活躍或備用主題資訊陣列中設定變数。 如果沒有指定主題,会使用活躍主題的資訊陣列。

靜態
參数
參数 类型 預設 描述
$var 字串 必要 要設定的資訊變数的名稱。
$value 混合
null
要設定的值。
$type 字串
'active'
無論在活躍或備用主題中,該變数應該被設定。
回傳 Theme
範例
// 取得預設實例
$theme = \Theme::instance();

// 設定 fallback 主題的 color 为 blue
$theme->set_info('color', 'blue', 'fallback');