-
Notifications
You must be signed in to change notification settings - Fork 0
3: Component Parameters
You cannot only pass custom parameters to newly instantiated components. There are a few core-parameters that influence the component's behavior as well.
<?php
namespace ProcessWire;
$myComponent = wire('twack')->getNewComponent('PageTitleOutput', [
'directory' => 'general'
]);
Setting a value for the directory
-parameter makes Twack to look for the component files under a different location. With the code above, we instruct Twack to initialize a PageTitleOutput
-component which is located not under site/templates/components/page_title_output/page_title_output.class.php
but under site/templates/components/general/page_title_output/page_title_output.class.php
.
The directory
-path is relative to the components-directory, which is defined in the module-configuration. You can also set it to an empty string to instruct Twack to look for a component at the root-level. This is especially useful when you add a child-component that is not located in your parent components directory:
<?php
namespace ProcessWire;
class HelloWorld extends TwackComponent {
public function __construct($args) {
parent::__construct($args);
// Our "General"-component is located under site/templates/components/general/general.class.php:
$this->addComponent('General', ['directory' => '']);
}
}
Each component has a ProcessWire page it is internally linked with. Per default that would be the page which was initially called by the user - the same as in wire('page'). You can access the component's page via $this->page
:
<?php
namespace ProcessWire;
?>
<h2><?= $this->page->title; ?>(<?= $this->page->id; ?>)</h2>
site/templates/components/general/page_title_output.view.php
But you can also change the internally linked page. For example, if you want to show a list of pages you can call our PageTitleOutput
component on each page:
<?php
namespace ProcessWire;
foreach(wire('pages')->find('template.name=newsitem') as $item){
$myComponent = wire('twack')->getNewComponent('PageTitleOutput', [
'directory' => 'general',
'page' => $item
]);
echo $myComponent->render();
}
When you pass custom values to the new component they will be available in the component constructor's $args
array. You can work with these values and set them via $this->my_value = $args['my_value'];
to a component-attribute that will be also accessible in the view. If you don't want to do any logic and validation with a custom value, you can set it as a component-attribute right in the initialization step. Add a parameters
array with your custom parameters, that should be available as a component attribute and in the view.
<?php
namespace ProcessWire;
$myComponent = wire('twack')->getNewComponent('PageTitleOutput', [
'directory' => 'general',
'parameters' => [
'showTitle' => false
]
]);
echo $myComponent->render();
Even though I haven't needed it much in my components so far, it's possible to set a custom view name. With this feature, you could for example initialize a component but say, that it should use a different view file than the default {class_name}.view.php.
<?php
namespace ProcessWire;
$myComponent = wire('twack')->getNewComponent('PageTitleOutput', [
'directory' => 'general',
'viewname' => 'OnlyDescription'
]);
echo $myComponent->render();
The example above would instruct the component to use the view site/templates/components/general/page_title_output/only_description.view.php
.
➡️ Continue with 4: Asset Handling
⬅️ Back to 2: Naming conventions & component variants
Twack
Reusable components for your ProcessWire-templates!
ProcessWire-Module: | https://modules.processwire.com/modules/twack/ |
Support-Forum: | https://processwire.com/talk/topic/23549-twack/ |
Repository: | https://github.com/Sebiworld/Twack |
Wiki: | https://github.com/Sebiworld/Twack/wiki/ |