【问题标题】:How to access markup twig variable from code in OctoberCMS如何从 OctoberCMS 中的代码访问标记树枝变量
【发布时间】:2020-02-10 11:56:55
【问题描述】:

我正在尝试从 octoberCMS 代码模块中的标记 (Twig) 访问变量。该变量由插件生成器循环打印。

我在标记中有这个变量:

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

我想在代码模块中访问 {{frontId}} 变量。

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

【问题讨论】:

    标签: laravel twig octobercms


    【解决方案1】:

    嗯,很遗憾,您不能将变量从 Markup 传递到 Code 部分。因为Code 部分在Markup 之前执行,所以你不能这样做。

    您似乎正在使用 Builder 的 Record details 组件,所以您必须从 url 传递 :id

    解决方案 1 [使用参数]

    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');
    
    }
    

    解决方案 2 [可以使用全局组件数组及其别名,请务必使用 onEnd 生命周期钩子]

    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');
    
    }
    

    参考截图

    如有任何疑问,请发表评论。

    【讨论】:

      猜你喜欢
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 2014-07-13
      • 1970-01-01
      • 2016-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多