We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 14973
    • 7 Posts
    Ok, so I can’t do this in a snippet:

    <?php
    $foo="[*parent*]";
    $bar="[*somevariable*]";
    if ($foo == $bar) {
    echo "T";
    }
    ?>

    for whatever the reason. The variables foo and bar seem to be set as should, but the comparison doesn’t work. How should I do this?

    --edit--

    $foo can (apparently) be set like this:

    $foo=$modx->documentObject[’parent’];

    so now I just need something similar for template variables (i.e. for [*somevariable*])... In MODx documentation I found this:

    http://modxcms.com/gettemplatevar.html

    but "usage" part on that page says nothing to me ($idname?, $fields?, $docid?, $published?), so how do I get the value of the template variable to a php variable?
      • 26903
      • 1,336 Posts
      From looking at the code briefly,

      $idname is the numerical id of the tv, can also be an array of such
      $fields are the fields you want, defaults to ’*’ so it will get all of them, you can leave this I guess
      $docid allows you to specify a particular document, if you don’t, current document is used
      $published allows you to specify if you want only published docs, this is the default.

      it will return an array or false if it fails

      The return value you want is ’default_text’ I think in the returned array.
        Use MODx, or the cat gets it!
        • 14973
        • 7 Posts
        With this:

        $bar = $modx->getTemplateVar("somevariable");
        echo $bar[’value’];

        I get the contents or ’value’. Now unfortunately the content is @INHERIT. That’s not what I want. I want it to be what it’s supposed to inherit...
          • 7231
          • 4,205 Posts
          You need to use getTemplateVarOutput(); to get the rendered output of the TV,

          Try this:
          $bar = $modx->getTemplateVarOutput( "somevariable" );
          return $bar['somevariable'];


          getTemplateVarOutput returns an array.
            [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

            Something is happening here, but you don&#39;t know what it is.
            Do you, Mr. Jones? - [bob dylan]
            • 7455
            • 2,204 Posts
            lol, someone beats me to this, but read the wiki lots of nice API pages

            you can use $modx->getTemplateVarOutput() instead
            http://wiki.modxcms.com/index.php/API:getTemplateVarOutput

            Dimmy
              follow me on twitter: @dimmy01
              • 14973
              • 7 Posts
              Ok, getTemplateVarOutput did the trick, thanks smiley I’m off to the next problem then laugh

              --edit--

              Actually, I’ll just ask you guys right now and save myself a lot of trouble. How do I get the output of this:

              [[UltimateParent? &top=`[*somevariable*]`]]

              to a php variable?
                • 7231
                • 4,205 Posts
                In that syntax the name of the parametr becomes a var automatically. Therefore, in your example the &top=`` parameter can be accessed via the $top variable in the snippet code automatically.

                So if you would simply echo $top; it should read the contents of the TV [*somevariable*]
                  [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                  Something is happening here, but you don&#39;t know what it is.
                  Do you, Mr. Jones? - [bob dylan]
                  • 14973
                  • 7 Posts
                  I got the result I wanted with this:

                  $bar=$modx->getTemplateVarOutput("somevariable");
                  $baz=$bar[’somevariable’];

                  $params[’top’]="$baz";
                  $bax=$modx->runSnippet (’UltimateParent’, $params);

                  Maybe not that elegant, but seems to work just fine smiley