Modx php&模板变量

时间:2013-03-17 11:59:10

标签: modx modx-revolution modx-templates

有没有办法在代码段中执行类似的操作:<?php if ([[+idx]]==1) echo "0";<?

谢谢。

2 个答案:

答案 0 :(得分:3)

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

$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代码段https://github.com/Bruno17/MIGX/blob/master/core/components/migx/elements/snippets/snippet.getImagelist.php

答案 1 :(得分:0)

因为您可能正在从有问题的资源中调用您的代码段[是吗?]您可以将idx传递给代码段....

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

然后在你的代码段中:

$output = '';
$idx = $scriptProperties['idx'];

if ($idx==1) {
$output = "0";
}

return $output;