We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29240
    • 8 Posts
    Here is a snippet you may remember from,

    http://modxcms.com/forums/index.php/topic,8644.0.html

    It injects a chunk after the first paragraph of content. It doesn’t work when the $modx->getTemplateVar(’Ads’) = "@INHERIT"
    How do I get it to pickup the TV value from the parent?

    <?php
    $where = 1;
    $content = $modx->documentObject['content'];
    $content = explode('</p>', $content);
    $tv = $modx->getTemplateVar('Ads');
    $params['chunks'] = $tv['value'];
    $params['wrapperdiv'] = 'adSenseBox';
    for ($i = 0; $i<count($content); $i++){
    	if ($i == $where) {
    		echo $modx->runSnippet(chunkListParser, $params);
    		}
    	echo $content[$i] . '</p>';
    	}
    echo $tv['value'];
    ?>
    

      • 22303 MODX Staff
      • 10,725 Posts
      You would have to process the TV value the same way the core parser does. This currently is not trivial, requiring you to reproduce code from the core, but will in the future be handled via the API’s.
        • 29240
        • 8 Posts
        Thanks for your reply. Is this something we can look forward to in .97 or further down the line? For now I’ll just forego using @INHEREIT for this if its not going to be too long.
          • 28042 ☆ A M B ☆
          • 24,524 Posts
          Consider that the value of a TV with the default of @INHERIT is going to be just that, "@INHERIT", and only the parent (or perhaps grandparent or great-grandparent) document’s TV will have the actual output value. So your snippet would need to note that the value of the snippet for this document is literally "@INHERIT" and then traverse the document tree upwards until it finds a parent of this document that has a different value. That will be the output value you’re looking for.
            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
            • 29240
            • 8 Posts
            Thanks for the input Susan, made a lot of sense and I got it working for the most part. It has a problem if there are only @INHERITS up to the root. There shouldn’t be that anyway but I couldn’t find a way to account for that error.

            <?php
            $where = 1;
            $content = $modx->documentObject['content'];
            $content = explode('</p>', $content);
            $tv = $modx->getTemplateVar('Ads');
            if ($tv['value']=="@INHERIT") {
            	$id = $modx->documentObject['id'];
            	$pid = $modx->getParent($id,'','id');
            	while($tv['value']=="@INHERIT") {
            		$id = $pid['id'];
            		$tv = $modx->getTemplateVar('Ads','*',$id);
            		$pid = $modx->getParent($id,'','id');
                        }
            }
            
            $params['chunks'] = $tv['value'];
            $params['wrapperdiv'] = 'adSenseBox';
            for ($i = 0; $i<count($content); $i++){
            	if ($i == $where) {
            		echo $modx->runSnippet(chunkListParser, $params);
            		}
            	echo $content[$i] . '</p>';
            	}
            ?>


            I am a bit curious how I could fix this but as it is now it works just fine. Thanks a lot for all your help.
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              Have you looked at the /manager/includes/tmplvars.commands.inc file?
                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
                • 29240
                • 8 Posts
                Thanks for the pointer, I got it working based on the core code!

                <?php
                $where = 1;
                $content = $modx->documentObject['content'];
                $content = explode('</p>', $content);
                $tv = $modx->getTemplateVar('Ads');
                if ($tv['value']=="@INHERIT") {
                	$id = $modx->documentIdentifier;
                	$pid = $modx->getDocument($id, 'id,parent');
                	$tv = $modx->getTemplateVar('Ads','*',$pid['id']);
                	while((int)$pid['parent'] != 0) {
                		if ((string) $tv['value'] !== '' && substr($tv['value'], 0, 1) != '@') {
                			break;
                		}
                		$pid = $modx->getDocument($pid['parent'], 'id,parent');
                		$tv = $modx->getTemplateVar('Ads','*',$pid['id']);
                            }
                }
                
                $params['chunks'] = $tv['value'];
                $params['wrapperdiv'] = 'adSenseBox';
                for ($i = 0; $i<count($content); $i++){
                	if ($i == $where) {
                		echo $modx->runSnippet(chunkListParser, $params);
                		}
                	echo $content[$i] . '</p>';
                	}
                ?>
                
                  • 46404
                  • 5 Posts
                  snippet (parent_title)
                  <?php
                  // created by Badichan Dmitriy (c) badichan.com
                  $page = $modx->getObject('modResource', $input);
                  $id = $page->get('parent');
                  $parent_page = $modx->getObject('modResource', $id);
                  echo $parent_page->get('pagetitle');
                  ?>
                  chunk
                  [[!parent_title? &input=`[[*parent]]`]] [ed. note: badichan last edited this post 12 years, 8 months ago.]