We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6511
    • 60 Posts
    danny_kay1710 Reply #81, 16 years ago
    OK, I have managed to get something up and running using Template Variables.
    <?php
    $output = $modx->documentOutput;
      
    $rStart = strpos($output, '<!-- Keyword Replacement Start -->');
    $rStop =  strpos($output, '<!-- Keyword Replacement End -->');
    
    $rLength = $rStop - $rStart;  
      
    if (($rStop !== false) && ($rStart !== false))
    {   
        if (!function_exists('str_replace_once')) {
            function str_replace_once($needle , $replace , $haystack){
                // Looks for the first occurence of $needle in $haystack
                // and replaces it with $replace.
                $pos = strpos($haystack, $needle);
                if ($pos === false) {
                    // Nothing found
                    return $haystack;
                }
                return substr_replace($haystack, $replace, $pos, strlen($needle));
            }
        }
        
        $subStr = substr($output, $rStart, $rLength);
        $rSubStr = $subStr ;
          
          //$keywords = "machinery safety|machinery-safety.html,European Directive|european-directives.html,CE Marking|ce-marking.html"; // move whats in the speech marks to a template variable called relinklist
        $keywords = $modx->getTemplateVarOutput(array("relinklist"));  
        $array = explode(",", $keywords['relinklist'] );  
        
          
        foreach ($array as &$value) {
          
          $myValues = explode("|", $value);     
          $replacementText = ' <a href="'.$myValues[1].'">'.$myValues[0].'</a> ';
          
          if (strpos($rSubStr, $replacementText ) === false)
             $rSubStr = str_replace_once(' '.$myValues[0].' ', $replacementText, $rSubStr);          
        }
          
        $modx->documentOutput = str_replace($subStr, $rSubStr, $output);
    }
    ?>​
    


    Basically create a template variable called "relinklist" (no quotes) and link it to the template you use.

    Then on every page under the Template Variables tab you will see this listed as a field put your keywords in this in the format of "word1|link1,word2|link2" similar to before and save it.

    This allows for page individual keywords.
      • 3071
      • 99 Posts
      This will then appear on every page when I create it, assuming it is using that template? Isn’t there a way of making it a module maybe so you only have to see it when updating it? Just an idea. Thanks, I am trying it now!
        • 6511
        • 60 Posts
        danny_kay1710 Reply #83, 16 years ago
        Yeah that’s correct, on every page with that template you will have this field on the Template Variables tab.

        It isn’t a perfect solution by any stretch of the imagination but it is still better than editing code and can be customised per page now as well.

        I will look at turning it into a module and adding some more advanced features but that will take time.
          • 3071
          • 99 Posts
          [s]I tried that one and it wouldn’t work using the template variable for some reason, so went back to the other code. I noticed on this one that I entered:

              $keywords = "keyword1|http://www.example.co.uk,keyword2|http://www.example.co.uk/keyword2";
              $array = explode(",", $keywords );


          keyword 1 is fine and the first one on each page hyperlinks, however, keyword2 is hyperlinked on every single instance. So it is like the code only does this action for the first keyword and then doesn’t run that on the rest.[/s]

          SCRATCH THAT, WORKING NOW!
            • 6511
            • 60 Posts
            danny_kay1710 Reply #85, 16 years ago
            Cool glad it’s all working
              • 3071
              • 99 Posts
              Yes it’s all good thanks a lot for your expertise, restores my faith in the human race wink I still cannot get the TV to work, even tried a chunk as well. Not tried on the newer Modxs yet, only on the 9.063 version so maybe it will work on those. No big drama anyway. Is this code generic as well, could I run it on a non-modx site if I change the bit at the top
              $output = &$modx->documentOutput;
              Someone told me I would have to find the php output variable on the particular site, but as usual, I have no clue.!
                • 6511
                • 60 Posts
                danny_kay1710 Reply #87, 16 years ago
                You could use the code yes but any lines with $modx-> in probably wouldn’t work in fact where it does $modx->documentOutput = $output you could in theory just echo it out but then you would need to put all of your content in the $output variable first and echo it out after this code.
                  • 3071
                  • 99 Posts
                  Well, there’s a mission for me! Was gonna test it out on another site just for the learning experience, but I got no idea where I find that particular variable on this site. Was told I would need to call whoever built the CMS to ask them the name of the output variable. Seems a bit long winded!
                    • 18913
                    • 654 Posts
                    Make sure that the template variable you created is assigned to the template used by the page on which you are calling this snippet. In other words, the snippet is being invoked on some document resource, most likely. That resource has a template associated with it. That template needs to be checked off in the configuration of the particular template variable containing your keyword list, otherwise the snippet won’t find anything to process.
                    Hope this helps,
                    Matt
                      • 3071
                      • 99 Posts
                      Yeah, I have done that but still no joy. It’s not the end of the world, I shouldn’t need to change the keywords that regularly. Thanks for the help anyway. grin