Ad

How To Access Markup Twig Variable From Code In OctoberCMS

- 1 answer

I'm trying to access variable from markup (Twig) in octoberCMS code module. This variable is printed in loop by plugin builder.

I have this variable in markup:

{% set frontId = record.id %}
{{frontId}}

And I want to access {{frontId}} variable it in code module.

function onStart()
{
    $this["slots"] =  Db::table('oblikovanje_izobrazevanja_vnos')->where('id', $frontId)->value('free_slots');
    echo $frontId;
}
Ad

Answer

Hmm, unfortunately you can not pass variables from Markup to Code section. Because Code section as whole executed before Markup so you can not do that.

It seems you are using Builder's Record details component so you must be passing :id from url

Solution 1 [ use param ]

function onStart() { // you can use onEnd as well
    $frontId = $this->param('id'); // this will get :id param from url

    // now slots variable are available in `Markup section`    
    $this["slots"] =  Db::table('oblikovanje_izobrazevanja_vnos')->where('id', $frontId)->value('free_slots');

}

Solution 2 [ you can use global component array with its alias name, make sure to use onEnd life-cycle hook ]

function onEnd () { // you must use onEnd as at this moment all components are initialized properly 

    // we can access component from $this->components with alias name and get its details
    $frontId = $this->components['builderDetails']->record->id;

    // now slots variable are available in `Markup section`    
    $this["slots"] =  Db::table('oblikovanje_izobrazevanja_vnos')->where('id', $frontId)->value('free_slots');

}

Reference Screenshot

enter image description here

if any doubts please comment.

Ad
source: stackoverflow.com
Ad