0

Is there a way to do something like that in a snippet : <?php if ([[+idx]]==1) echo "0";<?

Thank you.

2 Answers 2

5

If you need to get the value of a template variable, you can use this

$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

To get idx of migx tv you need something like this -

<?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;

Taken from migx snippet https://github.com/Bruno17/MIGX/blob/master/core/components/migx/elements/snippets/snippet.getImagelist.php

1
  • Thank you, [[+idx]] is the id of a migx item, how can i get this value ? Commented Mar 17, 2013 at 12:29
0

since you are probably calling your snippet from the resource in question [are you?] you can just pass the idx to the snippet....

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

then in your snippet:

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

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

return $output;
2
  • 1
    ([[+idx]]==1) In snippets modx used pure php and this is caused an error. Maybe you wanted to write ($idx==1) ?
    – Vasis
    Commented Mar 17, 2013 at 16:50
  • @vasis - right - I just copied that from the OP without thinking. edited & fixed. Commented Mar 17, 2013 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.