可重複使用的組件適合您的ProcessWire-Templates!
歡迎來到Twack! TWACK可幫助您構建結構良好且可維護的過程電視項目。受角的啟發。
| ProcessWire-Module: | https://modules.processwire.com/modules/twack/ |
| 支持福利: | https://processwire.com/talk/topic/23549-twack/ |
| 存儲庫: | https://github.com/sebiworld/twack |
| Wiki: | https://github.com/sebiworld/twack/wiki/ |
TWACK可以像ProcessWire中的所有其他模塊一樣安裝。檢查以下指南以獲取詳細信息:如何安裝或卸載模塊
先決條件是php> = 5.5.0 ,並且是processwire版本> = 3.0.0 。但是,在安裝模塊期間也檢查了這一點。沒有進一步的依賴。
TWACK使用組件將網站細分為邏輯組件。典型的組件可以代表自己的依賴性,這使其可以重複使用且易於維護。大多數組件由控制器和視圖組成。
如果您尚未完成,請創建site/templates/components目錄。在這裡,我們將放置所有未來的組件。在目錄的內部添加一個新文件hello_world.view.php 。創建site/templates/services ,我們所有的服務課都可以找到房屋。
打開文件並填寫以下內容:
<?php
namespace ProcessWire ;
?>
<h2>Hello World!</h2>完成了!我們有第一個功能組件。誠然,該組件僅包含一個視圖,但是即使對於簡單的組件,也有許多有用的目的。我們將稍後添加數據處理和功能。但是首先,讓我們學習如何將我們的組件在我們的ProcessWire-Templates之一中使用。
如果我們有一個控制級的班級,那麼該課程將被稱為HelloWorld 。在下一章中有關此內容的更多信息。要在ProcessWire-Teemplate中使用我們的組件,我們必須向其詢問Twack模塊,它將為我們找到並初始化它。
<?php
namespace ProcessWire ;
// Get a new instance of our HelloWorld-component:
$ myComponent = wire ( ' twack ' )-> getNewComponent ( ' HelloWorld ' );
?>
<html>
<head>
<title>Hello World!</title>
</head>
<body>
<?php
// Render the component`s view:
echo $ myComponent -> render ();
?>
</body>
</html>如您所見,在傳統的ProcessWire-Templates中包括一個Twack組件非常簡單。您可以在Twack視圖中構建完整的HTML,並使用其全部潛力,但您不必這樣做。可以逐漸替換頁面的各個部分。
讓我們回到我們的組件。您創建了自己的Helloworld-Component,只有一個視圖輸出了大膽的“ Hello World!”。大多數組件不僅需要輸出。我們需要一個控制器來使視圖更加動態。
創建一個新的目錄site/templates/components/hello_world/ ,然後將視圖文件移至此目的地。此外,在此新目錄中使用名稱hello_world.class.php創建一個控制器文件。
twack-controller需要一些樣板編碼才能正確函數。將以下代碼複製到您的控制器文件( hello_world.class.php ):
<?php
namespace ProcessWire ;
class HelloWorld extends TwackComponent {
public function __construct ( $ args ) {
parent :: __construct ( $ args );
}
}每個Twack控制器都必須擴展我們的一般TWACKCOMPONENT,這為控制器帶來了許多背景功能。與parent::__construct($args);在執行自定義組件的代碼之前,我們讓父TwackComponent完成其一般初始化工作。
在我們的構造函數中,我們將定義變量,添加兒童組件並為視圖做所有邏輯工作。
一個更高級的控制器看起來可以這樣:
<?php
namespace ProcessWire ;
class HelloWorld extends TwackComponent {
public function __construct ( $ args ) {
parent :: __construct ( $ args );
$ this -> title = ' Hello World! ' ;
if ( isset ( $ args [ ' title ' ])) {
$ this -> title = $ args [ ' title ' ];
}
// Add and initialise a child-component
$ testChildArgs = [
' myTestValue '
];
$ this -> addComponent ( ' TestChild ' , $ testChildArgs );
}
} $this->title將是“ Hello World!”,只要我們從構造函數的$args參數中獲得$args['title']的價值。如果我們用$twack->getNewComponent('HelloWorld', ['title' => 'My new Title.']); ,我們將其設置為新值。
在視圖中也可以訪問控制器的每個屬性,您不必關心傳輸值。
可以通過$this->addComponent()添加子組件。在我們的示例中,我們添加組件“ testChild”,該組件應位於site/templates/components/hello_world/test_child/test_child.class.php下。 TWACK自動查看當前組件目錄中的子目錄。指定另一個路徑也是可能的。我創建了一個數組$testChildArgs ,以演示將其他參數傳遞給TestChild ,該參數將傳遞給其構造函數。
我們的新觀點看起來像這樣:
<?php
namespace ProcessWire ;
?>
<div class="hello_world_component">
<?php
if (! empty ( $ this -> showTitle )){
echo " <h2> { $ this -> showTitle } </h2> " ;
}
?>
<p>Lorem ipsum</p>
<div class="children_wrapper">
<?php
foreach ( $ this -> childComponents as $ childComponent ) {
echo ( string ) $ childComponent ;
}
?>
</div>
</div>如您所見,我們只顯示標題,如果$this->title具有值。在$this->childComponents下,我們有一個通過$this->addComponent()在控制器中添加的所有組件的列表。
現在,我們創建了一個基本的Twack組件,現在您的一般概念是TWACK的工作原理。但是Twack具有許多更棒的功能,可以調整和簡化您的開發過程。
lang param,添加許多可以用作快捷方式的langugage代碼lang param以在Multilang環境中選擇一種語言wire('twack')一樣可用。 (感謝@bernhardbaumrock) 我們使用SEMVER進行版本控制。有關可用的版本,請參見此存儲庫上的標籤。
該項目由Mozilla公共許可證2.0版獲得許可 - 有關詳細信息,請參見許可證文件。
➡️繼續2:命名慣例和組件變體