【问题标题】:Modx php & Template variableModx php & 模板变量
【发布时间】:2013-03-05 19:47:46
【问题描述】:

有没有办法在 sn-p 中做类似的事情:<?php if ([[+idx]]==1) echo "0";<?

谢谢。

【问题讨论】:

    标签: modx modx-revolution modx-templates


    【解决方案1】:

    如果需要获取模板变量的值,可以使用这个

    $id = $modx->resource->get('id');//ID of current resource
    $name = $modx->resource->get('pagetitle');//title of current resource
    $val = $modx->resource->getTVValue('name_of_tv');//get tv value of current resource by name
    $val = $modx->resource->getTVValue($tv_id);//get tv value of current resource by ID
    

    要获得 migx tv 的 idx,您需要这样的东西 -

    <?php
    $docid = $modx->resource->get('id'); // id of curent resource
    $tvname = 'name_of_your_tv'; // change to yours
    $tv = $modx->getObject('modTemplateVar', array('name' => $tvname));
    $outputvalue = $tv->renderOutput($docid);
    $items = $modx->fromJSON($outputvalue);
    $idx = 0; // initialize idx
    $output = array();
    foreach ($items as $key => $item) {
        $idx++; // increase idx
        $output[] = print_r($item,1); // test output
    }
    $outputSeparator = "\n";
    $o = implode($outputSeparator, $output); // implode output
    return $o;
    

    取自 migx sn-p https://github.com/Bruno17/MIGX/blob/master/core/components/migx/elements/snippets/snippet.getImagelist.php

    【讨论】:

    • 谢谢,[[+idx]] 是一个 igx 项目的 id,我怎样才能得到这个值?
    【解决方案2】:

    因为您可能是从相关资源调用您的 sn-p [是吗?] 您可以将 idx 传递给 sn-p....

    [[!callMySnippet? &idx=[[+idx]] ]]
    

    然后在你的 sn-p 中:

    $output = '';
    $idx = $scriptProperties['idx'];
    
    if ($idx==1) {
    $output = "0";
    }
    
    return $output;
    

    【讨论】:

    • ([[+idx]]==1) 在 sn-ps modx 中使用了纯 php,这会导致错误。也许你想写($idx==1)
    • @vasis - 对 - 我只是不假思索地从 OP 复制了它。编辑和修复。
    猜你喜欢
    • 2014-10-21
    • 2016-03-10
    • 1970-01-01
    • 2012-12-13
    • 2021-04-25
    • 1970-01-01
    • 2014-03-18
    • 1970-01-01
    • 2016-10-03
    相关资源
    最近更新 更多