We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 22797
    • 134 Posts
    From what I’m able to gather, inheritance of TV values goes back only one generation (or, more accurately, to the most recent non-null generation). Inheritance is not cumulative, and there doesn’t seem to be a way to force it to be cumulative.

    Here’s a scenario: I have a TV for inheritable styles. I’d like to be able to assign styles for a given folder at the topmost level and have it inherit all the way down to all the children at all levels. This does work, but only if I don’t have anything to add to the list of styles at some point along the line. If I try to add to the list of styles in some child document, it will override the list of styles from the topmost document. I can always copy in the styles from the topmost document, but that seems to defeat part of the purpose of inheritance.

    I tried putting "@INHERIT" on the first line, followed by the additional styles, but that causes MODx to display only the inherited styles from the topmost parent. It ignores the styles after the text "@INHERIT". I also tried putting "@INHERIT" after all of the styles, but that resulted in the text  "@INHERIT" showing up as plain text in the final HTML code.

    Is there a way to force inheritance of TV values to be cumulative?
      • 28042 ☆ A M B ☆
      • 24,524 Posts
      No, @INHERIT works as you have discovered; it takes as its value the first content it finds working up the tree to the top-most parent.
        Studying MODX in the desert - http://sottwell.com
        Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
        Join the Slack Community - http://modx.org
        • 22797
        • 134 Posts
        For what it’s worth, I’d like to vote for inheritance being cumulative, in the way that many programmatic objects, or operating system permissions, or CSS properties are cumulative. Maybe there can be an optional flag to stop looking for inheritable properties once something is found, but it would make the most sense to me if they were all inherited, with the closest ones overriding the most distant ones, in the cases where the properties conflict.

        Isn’t that usually the way inheritance works in other contexts?

          • 21257 MODX Staff
          • 730 Posts
          Quote from: paulb at Aug 21, 2008, 05:11 PM

          .. but it would make the most sense to me if they were all inherited, with the closest ones overriding the most distant ones, in the cases where the properties conflict.
          Isn’t that usually the way inheritance works in other contexts?

          Inheritance can work that way in other contexts, but only because there is a clear programmatic model set up. In the case of CSS, the web browser invests quite a lot of logic into first parsing plaintext CSS (from inline styles, stylesheets etc), then going about organizing, weighing and assigning style values to HTML elements. MODx does no such parsing inherently of Template Variables.. they’re processed just like plain text. (Certain widgets do parse the values, for example the grid widget, but this is only done for output.)

          In the case of object-oriented code, you have your object and it’s properties, and yes, you can have one property inherited from the immediate parent while another property might be inherited from further up. Think of Template Variables like a single property of an object. In o-o, if you inherit a property which happens to be an array, the inheritance model does not go as far as looking up the inheritance chain for missing array entries to fill in.. not unless you override the inheritance mechanism yourself.

          There are so many things that TVs can be used for, so many types of data, that the processing has to remain generic.

          It is, however, conceivable to write a plugin that processes CSS from TVs, and overrides the default inheritance mechanism. That would be a highly specialized use case, and would likely take quite a bit of time to develop.
            Mike Schell
            Lead Developer, MODX Cloud
            Email: [email protected]
            GitHub: https://github.com/netProphET/
            Twitter: @mkschell
            • 22797
            • 134 Posts
            I wouldn’t need a full-fledged document parser. I’m envisioning just a simple additive text function. Let’s say the topmost document (id=0) has a value in an inheritable TV, and its child document, and that child’s document. I would want them them each appended to the next. Here’s an example:

            Document id=0
            value of inheritable TV: "body {color:black;}"

            Document id= 1 (child of document 0)
            value of inheritable TV: "p {background:blue;color:white}"
            cumulative value:
              "body {color:black;}
              p {background:blue;color:white}"

            Document id=2 (child of document 1)
            value of inheritable TV: "a {color:yellow}"
            cumulative value:
              "body {color:black;}
              p {background:blue;color:white}
              a {color:yellow}"

            In programming terms, the final result would be reached by something as simple as:
            $inheritableTvs = array(put values here);
            $inheritableTvs = implode("\r\n", $inheritableTvs);


            It’s just a matter of putting them one right after another, presumably with a line break in between them, or a space, but nothing more than that. Of course, to arrive at that last bit of code would require walking through the hierarchy back to the topmost parent. There would be some overhead involved in that. One way to deal with that would be to require the word "@INHERIT" at the beginning of the value of the TV as a flag to continue walking through the hierarchy. So the value for document 1 would be:

            @INHERIT
            p {background:blue;color:white}

            The value for document 2 would be:

            @INHERIT
            a {color:yellow}

            If you want to stop the inheritance, you could omit the "@INHERIT", and that would be the flag to stop there.