We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 13671
    • 3 Posts
    Hi all,
    I have been using MODx for several days now (really impressed), however im now a little bit stuck. Basically I have created a snippet to retrieve various template variables from templates which match a certain criteria (i.e where discipline = digital media),I am then assigning the results of the query to several arrays, these arrays in turn are being stored in several placeholders. In the template, I then want to be able to iterate through the array elements stored in the placeholder values and display the content. My end result being a dynamically populated page displaying n amount of projects. I have had a good search for an answer but to no avail im afraid, any help would be greatly appreciated.

    snippet code (select_project), the value for $discipline is actually posted from a previous page
    $discipline = $_REQUEST['discipline'];
    
    $sql = "SELECT distinct contentid FROM suburb_site_tmplvar_contentvalues WHERE value='$discipline'";
    $result = $modx->db->query($sql);
    
    while($row = mysql_fetch_array($result)){
     $contentid[] = $row['contentid'];
    }
    
    for($i=0; $contentid[$i]!=null;$i++){
     $tvoutput = $modx->getTemplateVarOutput(array('introtext','project_title','project_copy','project_thumbnail'), $contentid[$i], 1);
     $introtext[] = $tvoutput['introtext'];
     $project_title[] = $tvoutput['project_title'];
     $project_copy[] = $tvoutput['project_copy'];
     $project_thumbnail[] = $tvoutput['project_thumbnail'];
    }
    
    $modx->setPlaceholder('introtext',$introtext[]);
    $modx->setPlaceholder('project_title',$project_title[]);
    $modx->setPlaceholder('project_copy',$project_copy[]);
    $modx->setPlaceholder('project_thumbnail',$project_thumbnail[]);
    


    This is the sample of code from the template
    [[select_project]]
    [+introtext+]
    [+project_title+]
    [+project_copy+]
    <img src="[+project_thumbnail+]">
      • 10487 MODX Staff
      • 1,535 Posts
      Hi jaybo,

      You won’t be able to manipulate placeholders like you want from within templates.

      Rather than pushing the array output of the snippet into placeholders, you would be better off parsing the required display template on each pass of the ’for’ loop (using a series of str_replace functions) - and then return the full parsed output at the end of the snippet.

      Hope that helps,
      Garry
        Garry Nutting
        Senior Developer
        MODX, LLC

        Email: [email protected]
        Twitter: @garryn
        Web: modx.com
        • 13671
        • 3 Posts
        Thanks for the fast reply Garry,
        Im a bit unclear of what you mean, could you possibly show me an example?
          • 10487 MODX Staff
          • 1,535 Posts
          Well, here is an example based on the code you posted above - I haven’t tested this, only just typed it out so might need a few tweaks wink Hopefully, you’ll get the idea.

          $discipline = $_REQUEST['discipline'];
          
          $template = '
          [+introtext+]
          [+project_title+]
          [+project_copy+]
          <img src="[+project_thumbnail+]">
          ';
          
          $output = '';
          
          $sql = "SELECT distinct contentid FROM suburb_site_tmplvar_contentvalues WHERE value='$discipline'";
          $result = $modx->db->query($sql);
          
          while($row = mysql_fetch_array($result)){
           $contentid[] = $row['contentid'];
          }
          
          for($i=0; $contentid[$i]!=null;$i++){
           $tvoutput = $modx->getTemplateVarOutput(array('introtext','project_title','project_copy','project_thumbnail'), $contentid[$i], 1);
           $replacementArray = array(
           'introtext' => $tvoutput['introtext'],
           'project_title' => $tvoutput['project_title'],
           'project_copy' => $tvoutput['project_thumbnail'],
           'project_thumbnail' => $tvoutput['project_thumbnail']
           );
           
           $parsedTemplate = $template;
           foreach ($replacementArray as $key => $value) {
               $parsedTemplate = str_replace('[+'.$key.'+]',$value,$parsedTemplate);
           }
          
           $output .= $parsedTemplate;
          }
          
          return $output;
          


          EDIT: Just added a missing apostrophe in the example.
            Garry Nutting
            Senior Developer
            MODX, LLC

            Email: [email protected]
            Twitter: @garryn
            Web: modx.com
            • 13671
            • 3 Posts
            Excellent,
            Cheers for that Garry, works a treat. Only bit I had to ammend was
            ’project_copy’ => $tvoutput[’project_thumbnail’], to
            ’project_copy’ => $tvoutput[’project_copy’],

            Thanks again for your time