可重复使用的组件适合您的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:命名惯例和组件变体