dqz84361326 2016-01-14 18:55 采纳率: 0%
浏览 31
已采纳

ExpressionEngine:将参数传递给变量标签对并在函数中获取

I need to pass parameter into inner tag, ex.

{exp:myplugin:func}
  {last_year subject="xxx"}

  {/last_year}
{/exp:myplugin:func}

How can I fetch that parameter in the function?

  • 写回答

1条回答 默认 最新

  • duanaigua4033 2016-01-19 11:43
    关注

    Here's a function I've written to return the parameters for a variable pair inside a template tag pair:

    private function get_variable_parameters($tagData, $varName) {
        $parameters = array();
    
        if (strpos($tagData, LD."/".$varName.RD) !== FALSE) {
            //###   Closing variable tag exists   ###
            if (preg_match_all("/".LD.$varName."(.*?)".RD."(.*?)".LD."\/".$varName.RD."/s", $tagData, $matches)) {
                for ($num = 0; $num < count($matches[0]); $num++){
                    $allParams = explode(" ", trim($matches[1][$num]));
                    foreach($allParams as $value) {
                        $value = str_replace(array('"', "'"), '', $value);
                        $param = explode("=", $value);
                        if (!empty($param[1]))
                            $parameters[$param[0]] = $param[1];
                    }
                }
            }
        }
    
        return $parameters;
    }//###   End of get_variable_parameters function   ###
    

    So from your example code in your func method:

    $tagData = ee()->TMPL->tagdata;
    $varParameters = $this->get_variable_parameters($tagData, "last_year");
    echo $varParameters["subject"];
    

    Looking back over my code though, I don't think it handles multiple use of the same variable pair inside the same loop, so if required may need to change this line:

    $parameters[$param[0]] = $param[1];
    

    to:

    $parameters[$num][$param[0]] = $param[1];
    

    And then work out what instance of the variable pairs inside the same loop. Untested though, probably needs more work!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题