0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

componentの自作

Posted at

備忘録です。OctoberCMSだんだん使い方分かってきて、component試してみました。

php artisan create:component my.company TestComponent

で雛形ができます。次にコンポーネントを利用するための登録を行います。

vim plugins/my/company/Plugin.php

public function registerComponents()
{
    //return []; // Remove this line to activate
    //ここで先程のcomponentを登録する
    return [
        'my\Company\Components\TestComponent' => 'TestComponent',
    ];
}

backendからlogout loginで登録されたcomponentが見えるようになります。
そこでテストページを作り、コンポーネントエリアからTestComponentをドロップダウンすると、コンポーネントとしての挙動開始します。
この段階では、

This is the default markup for component TestComponent
You can delete this file if you want

と表示されるだけで、コンポーネントのプロパティもエイリアスのみです。
作成したTestComponentを編集します。

vim plugins/my/company/components/TestComponent.php

//ここで定義したプロパティにアクセスできるようになります
public function defineProperties()
{
    return [
        'productid' => [
            'title'       => '商品番号',
            'description' => '指定された商品番号の商品を展開します',
            'default'     => '{{ :slug }}',
            'type'        => 'string'
        ],
    ];
}

//コンポーネントが実際にページから描画される際は onRun()が呼ばれるようです
public function onRun(){
    //コンポーネントエディタで登録したプロパティ値が取得出来てます
    dump($this->property('productid'));

    $db = new MyDB(); //モデルはここから呼び出せるものなら何でも良いはず
    $val =$db->hoge($this->property('productid'));
        
    dump($val); //コンポーネントがデータ受け取れていること確認

    //次が少しまごつきましたが、$this->pageに渡すことで,componentのdefault.htmでも、
    //呼び出す方の .htmでも使えるようになりました。
    $this->page['val'] = $val;

component側の default.htm

vim plugins/my/company/components/testcomponent/default.htm
<p>default.htm start </p>
{{ dump (val) }}
<p>default.htm end </p>

貼り付けた方のhtm

vim themes/test/pages/testcomponent.htm

{% component 'Test' %}
<p>呼んだ方の始まり</p>
{{ dump(val)}}
<p>呼んだ方終わり</P>

で2回valがdump表示されます。

component はpartialの拡張版みたいなものに感じました。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?