We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 36541
    • 222 Posts
    Subject says it all. I want to get an inherited value of template variable.

    Say, there is a TV named [*category*] which specifies document category. The field ’default value’ contains this definition:

    @INHERIT general


    When used in document, if not set to anything else, it returns ’general’. If set for the parent document to ’sports’ it returns ’sports’ in rendered document. However, when accessed with MODx API it returns:

    <?php
    $output = $modx->getTemplateVar('category',"*",$docid);
    return $output['value']; // returns '@INHERIT general'
    
    $output = $modx->getTemplateVarOutput('category',$docid);
    return $output['category']; // returns 'general'
    ?>


    The question is: how to get the inherited value of template variable for the particular document? In this case it should return ’sports’.
      This is the web: the only thing you know about who will come is that you don't know who will come.
      • 36541
      • 222 Posts
      Certainly there should be a way to get the inherited value of template variable so I don’t have to duplicate already existing functionality.

      Anyone?
        This is the web: the only thing you know about who will come is that you don't know who will come.
        • 25663 MODX Staff
        • 12,272 Posts
        There’s no direct API call for that if that’s what you’re asking. You’ll need to pull some queries I’m afraid.
          Ryan Thrash, MODX Co-Founder
          Follow me on Twitter at @rthrash or catch my occasional unofficial thoughts at thrash.me
          • 34162
          • 1 Posts
          huh How can the renderer get another result than the API?
          I do not understand that!
          Maybe the point is how inheritance works: just "virtually". The renderer looks for the parent, the API does not.
          IMHO that’s a design-flaw that should be fixed in the next release.
          getTemplateVarOutput must return what will be displayed on the rendered page, not the default value for the TV.
            • 17883
            • 1,039 Posts
            I second that. I just had the same problem. So the getTemplateVarOutput APi function has - in case of "inherit" - to look for the parent document value. Has to be some kind of recursive function, so it looks for parent values till it finds something != inherit. If it finds an empty value in a parent it has to return an empty value.

            I also discovered that a checkbox TV with @inherit always is checked in the Quick Edit bar. That´s clear because there is "something" written in the TV value. But it also has to look for the parent value, if empty value==empty.

            Shouldn´ t we open an entry to the tracker for that?

            Marc
              • 24292
              • 28 Posts
              I second that. I just had the same problem.

              And I’d like to third that. The problem also occurs in snippets like Ditto that evaluate TVs in templates. The value returned is always the default value, if the TV is inherited but the TV has the correct value if evalueted on the page itself.
                • 22303 MODX Staff
                • 10,725 Posts
                All very good points that will be addressed by the upcoming object-oriented API, and future releases will feature some great new capabilities in terms of defining inheritance for any kind of component, be it a TV, a chunk, a snippet, or whatever. In the meantime, if someone hasn’t already entered this bug, please do so and link to it here once you have; this specific @INHERIT issue needs to be addressed before next release.
                  • 17883
                  • 1,039 Posts
                    • 36541
                    • 222 Posts
                    Thank you all for input. In the meantime I found a function ProcessTVCommand (in /manager/includes/tmplvars.commands.inc.php) which seems to make just that: recursively look for not inherited value of template variable.

                    However, I wasn’t able to call it with:
                    $modx->ProcessTVCommand($value,$tvname);
                    
                      This is the web: the only thing you know about who will come is that you don't know who will come.
                      • 10487 MODX Staff
                      • 1,535 Posts
                      No, you won’t be able to call the processTVCommand directly like that. If you include the tmplvars.commands.inc.php file, then you will have access to this function.

                      In fact, if I’m reading the code properly for getTemplateVarOutput() then it’s calling the function above via another function: getTVDisplayFormat().

                      So, it looks to me like the recursive nature of @INHERIT has been incorporated, but isn’t working quite right at the moment.

                      Could be wrong though ... best thing is to log it in the bugtracker, if it’s not there already.

                      Thanks,
                      Garry
                        Garry Nutting
                        Senior Developer
                        MODX, LLC

                        Email: [email protected]
                        Twitter: @garryn
                        Web: modx.com