This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

PabloRQ
PabloRQ

Hi,

I'm having a problem with the variables of a component, this is there are no variables on the partial side of the component. Doing some tests I end up with the following code:

component.php

public function testCode()
{
    $test01 = 'test';
    dump($test01);
    return $test01;
}

default.htm

<div class="panel panel-default">
    <div class="panel-body">
        {{ testCode.testCode }}<br/>
        {{ test01 }}<br/>
        {{ testCode.test01 }}<br/>
        {{ __SELF__.test01 }}<br/>
    </div>
</div>

Nothing works. The only thing I have is the dumps of the php code, but nothing on the variables in the default partial.

Related documentation: Docs / Building components and Docs / Markup / Templating.

Any idea?

Thank you in advance.

Last updated

Maz
Maz

You should pass the variable this way:

$this->test01 = 'test';

And use this syntax in your frontend

{{ __SELF__.test01 }}
PabloRQ
PabloRQ

Maz said:

You should pass the variable this way:

$this->test01 = 'test';

And use this syntax in your frontend

{{ __SELF__.test01 }}

Thank you Maz, but it doesn't work.

By now, the only way I found to pass variables is:

  • Using AJAX
  • With a onRender function
From the documentation, I think that what I wrote in my previous post and your way should work. But I'm afraid not.

(

PabloRQ
PabloRQ

By the way.

Using AJAX I'm able to pass the variables with

$this->page['test01'] = 'test';

...and using this in the HTML side:

{{ tes01 }}

This is with the AJAX response.

I don't know why it isn't working in a direct way as documented on the OctoberCMS and Twig sites.

mjauvin
mjauvin

I suggest looking at other components source code for the different Rainlab plugins to see how it's properly done.

JeffGoldblum
JeffGoldblum

I recommend signing up for premium support if you have a lot of questions and want a faster response: https://octobercms.com/premium-support

PabloRQ
PabloRQ

Thank you both!

I'm using the default data on the documentation and it doesn't work. I'm just reporting a possible bug or error in the documentation that can be affecting other users, whom doesn't report and switch to other framework.

Cheers!

mjauvin
mjauvin

Can you describe precisely what bug you see in the documentation? I have no such evidence, I have been referring to this documentation for a long time and it's accurate.

JeffGoldblum
JeffGoldblum

Provide an example of the actual code you're trying to use, not abstracted through fake names like "testMethod"

PabloRQ
PabloRQ

mjauvin said:

Can you describe precisely what bug you see in the documentation? I have no such evidence, I have been referring to this documentation for a long time and it's accurate.

I was trying at this example:

/components/TestCode.php

// Alias: testCode
// This array becomes available on the page as {{ component.posts }}
    public function posts()
    {
        return ['First Post', 'Second Post', 'Third Post'];
    }

/components/testcode/default.htm

{{ testCode.posts[0] }}

Just before writing this post I tested again to have more detail, and now it works. I'm testing passing arrays, variables and more, and works ok.

Never mind. A glitch on the matrix.

Thank you for your advise!

1-10 of 10

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.