We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7737
    • 10 Posts
    Based on Mark’s getdoc, I wrote a snippet called outputPage that will simply output a page, replacing the template variables with what they shlould be.

    It takes one arg, $id, which is the id of the page you would like to output.

    $tblContent= $modx->db->config['table_prefix'] . 'site_content';
    $tblTemplate= $modx->db->config['table_prefix'] . 'site_templates';
    
    $query = "select template.content from $tblContent content, $tblTemplate template where content.id=$id and template.id = content.template";
    if (!$rs= $modx->dbQuery($query)) {
        return '';
    }
    
    $row= $modx->fetchRow($rs);
    $content = $row['content'];
    preg_match_all('~\[\*(.*?)\*\]~',$content,$matches);
    $cnt = count($matches[1]);
       
    for ($i=0; $i<$cnt; $i++) {
            $value = $modx->getTemplateVarOutput($idname=array($matches[1][$i]), $id, "1");
            $v = $value[$matches[1][$i]];
            $content = str_replace("[*".$matches[1][$i]."*]",$v,$content);
    }
    
    return $content;
    


    Enjoy!
      • 6726
      • 7,075 Posts
      (Could you add the [Snippet] thing in the title, it helps identifying... smiley )

      Thanks !

      But could you help me understand with an example... I fail to see how to use it huh
        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l&#39;outil id
      • Looks to me like a snippet version of the @DOCUMENT binding for TVs...?
          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
          • 7737
          • 10 Posts
          The reason I created this snippet is because I am not sure how to include a page in another page.

          This snippet will do that, and replace the TVs with their values defined by the included page. If there is a built-in way to do this, please let me know! It seems there are a whole bunch of MODx functions that are undocumented undecided
            • 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
              • 7737
              • 10 Posts
              The disadvantage of using @DOCUMENT is that you need to create a TV for each document you want to include.

              For the site I’m working on, that would mean about 100+ TVs and confused users. My snippet gets rid of that disadvantage.
                • 32241
                • 1,495 Posts
                Quote from: dogas at Mar 03, 2006, 08:48 PM

                The disadvantage of using @DOCUMENT is that you need to create a TV for each document you want to include.

                For the site I’m working on, that would mean about 100+ TVs and confused users. My snippet gets rid of that disadvantage.

                Hi dogas, I agree with what you’re saying, but there is a warning from me though, take a note that we can use [*#tv_name*] format, other than just plain [*tv_name*].

                One other thing, I believe when you fetch the data from database, it will outputed the whole content to the page, including all the TV tag, which it will later be evaluated by modx document parser. The only problem is that the TV that is assigned to the template that is being assigned to the document that you fetched will not be parsed to the content, unless if you have the same template with the document that called the content output using snippet. Another problem will be, the content of the TV will be replaced by the one from the doc that called the snippet.

                So your approach in making this snippet will basically solved them all, but a word of warning, watch the amount of getTemplateVars calling that you use. It’s basically hitting database everytime you call it.
                I would suggest to change the code from this
                for ($i=0; $i<$cnt; $i++) {
                        $value = $modx->getTemplateVarOutput($idname=array($matches[1][$i]), $id, "1");
                        $v = $value[$matches[1][$i]];
                        $content = str_replace("[*".$matches[1][$i]."*]",$v,$content);
                }
                

                into this

                $value = $modx->getTemplateVarOutput($matches[1], $id, "1");
                for ($i=0; $i<$cnt; $i++) {
                $v = $value[$matches[1][$i]];
                $content = str_replace("[*".$matches[1][$i]."*]",$v,$content);
                }
                <br /><br />I haven’t tested the code yet, but it should work or it might need just a little modification.<br />Btw nice job, looks like you’re going strong with a couple useful things that you contribute to the community.<br /><br />Keep it up ;D
                  Wendy Novianto
                  [font=Verdana]PT DJAMOER Technology Media
                  [font=Verdana]Xituz Media
                  • 7737
                  • 10 Posts
                  That’s a good idea to do it all in one shot, rather than hitting the DB for each TV you have.

                  I shamelessly ganked some of that code from the newslisting snippet, so they might want to do that also.

                  Perhaps on monday I’ll change it so it will pick up the [*#template_name*] as well as [*template_name*].


                  One other thing, I believe when you fetch the data from database, it will outputed the whole content to the page, including all the TV tag, which it will later be evaluated by modx document parser.
                  Before I wrote the code to replace the TVs with their actual value, it would simply output the template, and any variables (eg [*something*]) would simply not be displayed. The template would display fine, but there were no TVs displayed.
                  The only problem is that the TV that is assigned to the template that is being assigned to the document that you fetched will not be parsed to the content, unless if you have the same template with the document that called the content output using snippet. Another problem will be, the content of the TV will be replaced by the one from the doc that called the snippet.
                  Can you elaborate a bit more? I admit.. on fridays we have a few beers in the office and I just can’t make heads or tails out of what this means. grin


                    • 32241
                    • 1,495 Posts
                    Quote from: dogas at Mar 03, 2006, 09:59 PM

                    Can you elaborate a bit more? I admit.. on fridays we have a few beers in the office and I just can’t make heads or tails out of what this means. grin

                    That’s aone of the problem, if you type it without thinking, basically, I didn’t get my message accross grin

                    Btw, if you have both document assigned to the same template, it means that you will have the same template variables, correct?
                    Now, lets call it doc1 and doc2. If you use GetDoc snippet, from doc1 to fetch content data from doc2, it will fetch the whole raw data from doc2 content area and it will be outputed by the GetDoc snippet on doc1.

                    Basically, in doc1, the raw data had been combined together with doc2, so now MODx parser will parsed the raw data and outputed the result to your screen, which means, every tv calling on doc1 and doc2 will be replaced with the value of the corresponding tv on doc1. So now it’s all depend on the needs. If you want the tv to be replaced by the tv value on doc1, then your snippet will not be needed. BUT..., if you want your doc2 tv tag to be replaced with the tv value on doc2, instead of doc1, then it’s time to use your snippet to achieve that.

                    Hope it does make sense with my limitation of ability to explain things in a clear manner and with the right english grammar and vocab grin Heheheee...

                    Anyway, to solve your snippet as well, we can write it like this. It will avoid any missing id parameter when calling the snippet, to output nothing, instead of giving out error message. I also added the [*#tv_name*] fix and avoiding multiple calls to database.
                    // $id = document id to be fetched
                    
                    $content = '';
                    if(isset($id) && is_int((int) $id)) {
                    $tblContent= $modx->db->config['table_prefix'] . 'site_content';
                    $tblTemplate= $modx->db->config['table_prefix'] . 'site_templates';
                    
                    $query = "select template.content from $tblContent content, $tblTemplate template where content.id=$id and template.id = content.template";
                    if (!$rs= $modx->dbQuery($query)) {
                        return '';
                    }
                    
                    $row= $modx->fetchRow($rs);
                    $content = $row['content'];
                    preg_match_all('~\[\*(.*?)\*\]~',$content,$matches);
                    $cnt = count($matches[1]);
                       
                    $value = $modx->getTemplateVarOutput($matches[1], $id, "1");
                    for ($i=0; $i<$cnt; $i++) {
                            $v = $value[$matches[1][$i]];
                            $content = str_replace("[*".$matches[1][$i]."*]",$v,$content);
                            $content = str_replace("[*#".$matches[1][$i]."*]",$v,$content);
                    }
                    }
                    return $content;
                    


                    Hope it works, I haven’t tested this out yet, but if it works, please update your first post with this code. Btw, if you can add more features to it, feel free to add it, and make it as your first snippet contribution to the community grin You can make it to accept another parameter, which will determine which field on the document that will be fetched, instead of the default content field. Good job btw dogas!

                    Btw, have you tested the code yet? It seems to me that the fetchRow has to be $modx->db->fetchRow.
                    It also seems to me that the $content will not receiving any content data at all. Hemm... anyway, please take care of that and post your contribution when you’re done wink
                      Wendy Novianto
                      [font=Verdana]PT DJAMOER Technology Media
                      [font=Verdana]Xituz Media