【问题标题】:ModX: Use PHP var in Wayfinder?ModX:在 Wayfinder 中使用 PHP var?
【发布时间】:2012-12-18 00:08:00
【问题描述】:

我有一个从数据库获取文档 ID 的 sn-p。

我想把这些放在 Wayfinder 的菜单中,但无法让它工作。有谁知道在 Wayfinder 中放置 PHP var 的正确方法?试过了,但没有运气:

echo '[[Wayfinder? &includeDocs=`' . $docid . '`]]';

(PS:使用 Revo)

编辑:添加更多代码

简而言之,我的代码从会话中获取登录用户 ID 并找到他们的访问组。最终目标是显示访问组内资源的链接。这是 sn-p 的后半部分,我已经获得了适当的资源 ID,只需将它们输出即可。

//RETRIEVE DOCUMENT GROUPS RELATED TO ACCESS GROUPS
          $docgroups = "SELECT * FROM `modx_document_groups` WHERE `document_group` = '$target' ";

          $docstmt = $modx->query($docgroups );              

          while ($docrow = $docstmt->fetch(PDO::FETCH_ASSOC)) {
          $docid = $docrow['document'];            

          echo '[[Wayfinder? &startId=`0` &includeDocs=`' . $docid . '`]]';

          }//END

【问题讨论】:

    标签: php variables content-management-system modx modx-revolution


    【解决方案1】:

    在 modx 中,您的 sn-ps 必须返回一个值才能以块/模板等形式访问它。这是一种方式:

    //MySnippet
    <?php
    // logic
    $docIDs = array(1, 2, 23, 17);
    return implode(',', $docIDs);
    

    然后你的寻路器调用使用sn-p:

     // wayfinder call
    [[!Wayfinder? &includeDocs=`[[!MySnippet]]`]]
    
    // is the same as:
    [[!Wayfinder? &includeDocs=`1,2,23,17`]]
    

    【讨论】:

    • 啊,好吧,所以'return'是我的sn-p的输出?那么没有办法将 Wayfinder 合并到 while 循环中吗?我只需要输出一个数组?
    • 为什么要在 while 循环中使用 WF? - 也许发布您的 sn-p 代码可能有助于阐明您想要实现的目标
    • 我在上面添加了更多代码。我确信循环中的 Wayfinder 是不必要的,而且无论如何你的方式会更好
    • 你的方式会多次调用寻路器并创建多个列表,不必要的处理等。但我可以看到你的逻辑!通过在 while 循环中简单地创建一个数组,并将返回值传递给 wayfinder,您可以获得更多的控制权,并且最终应该做您想做的事情。
    【解决方案2】:

    在 Modx Revolution 做到这一点的正确方法:

    $c = $modx->newQuery('modResourceGroupResource');
    $c->where(array( 'document_group' => $target ));
    $docs = $modx->getCollection('modResourceGroupResource', $c);
    
    $docids = array();
    foreach($docs as $doc) {
        $docids[] = $doc->get('document');
    }
    
    $output = $modx->runSnippet('Wayfinder',array(
        'startId' => 0,
        'includeDocs' => implode(',', $docids);
    ));
    
    return $output;
    

    【讨论】:

    • 是的,这是正确的方法,而且效率更高,因为它只需要解析一次。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-29
    相关资源
    最近更新 更多