We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 29076
    • 615 Posts
    I wonder if I can ask for some help from you PHP-people? I’m not so good at it my self.

    I have a Ditto-template
    <tr>
       <td class="listDataCell"><a href="[~[+id+]~]">[+pagetitle+]</a></td>
       <td class="listDataCell">[+norwegian+]</td>
       <td align="center" class="listDataCell">[+redlist-wrapper:len:gt=`0`:then=`<strong style="color: red;">R</strong>`:else=``+]</td>
       <td class="imageCell" width="30px"><img class="listImgLeft" src="assets/images/species/[+description+]/[+description+]_close_mini.jpg" /></td>
       <td class="imageCell" width="32px"><img src="assets/images/species/[+description+]/[+description+]_hole_mini.jpg" /></td>
    </tr>

    and the Ditto-call:
    [[Ditto?parents=`2` &depth=`4` &tpl=`photoListSort` &display=`all` &sortBy=`pagetitle` &sortDir=`ASC` &hideFolders=`1` &filter=`hidemenu,0|introducedWhere,,1`]]

    The result is like this. But some of the picturefiles doesn’t exist yet, and the HTTP, error_log on my host-server gets filled up with File does not exist messages.

    Is there a way to check if the assets/images/species/[+description+]/[+description+]_close_mini.jpg exist, and if not, put a text instead. (For example).
      I think, thererfor I am! But what I am, and why...?
      • 29076
      • 615 Posts
      I have this snippet:
      <?php
      $filename= $modx->documentObject['description'];
      $filename_close = 'assets/images/species/'.$filename.'/'.$filename.'_close.jpg';
      $filename_hole= 'assets/images/species/'.$filename.'/'.$filename.'_hole.jpg';
      $filename_place= 'assets/images/species/'.$filename.'/'.$filename.'_place.jpg';
      if (file_exists($filename_close)) {
          echo '<td valign="top" width="183px" height="100%"><a href="assets/images/species/'.$filename.'/'.$filename.'_close_full.jpg" title="Click to get a larger image" rel="lightbox">
              <img class="close" src="'.$filename_close.'" />
              <p class="hide">
                Place: [*location-close*]<br />
                © [*photo-close*]
              </p>
              </a>
              </td>';
      } else {
          echo '<td valign="top" width="183px" height="100%" class="noHover">  No picture yet. </td>';
      }
      
      if (file_exists($filename_hole)) {
          echo '
              <td valign="top" width="158px" class="hole"><a href="assets/images/species/'.$filename.'/'.$filename.'_hole_full.jpg" title="Click to get a larger image" rel="lightbox">
              <img class="hole" src="'.$filename_hole.'" />
              <p class="hide">
                Place: [*location-hole*]<br />
                © [*photo-hole*]
              </p>
              </a>
              </td>';
      } else {
          echo '<td valign="top" width="158px" class="noHover">  No picture yet. </td>';
      }
      
      if (file_exists($filename_place)) {
          echo '
             <td valign="top" width="227px" class="place"><a href="assets/images/species/'.$filename.'/'.$filename.'_place_full.jpg" title="Click to get a larger image" rel="lightbox">
             <img class="place" src="'.$filename_place.'" />
             <p class="hide">
              Place: [*location-place*]<br />
              © [*photo-place*]
              </p>
              </a>
              </td>';
      } else {
          echo '<td valign="top" class="noHover">  No picture yet. </td>';
      }
      ?>

      The snippet works when it’s called directly from the template. (Example here) But the
      if (file_exists($filename_close)) {
      doesn’t work when called from a tpl-file in a Ditto call.
      Here is the Ditto tpl:
      <tr>
      <td class="listDataCell"><a href="[~[+id+]~]">[+pagetitle+]</a></td>
      <td class="listDataCell">[+norwegian+]</td>
      <td align="center" class="listDataCell">[+redlist-wrapper:len:gt=`0`:then=`<strong style="color: red;">R</strong>`:else=``+]</td>
      [[getListPictureFiles]]
      </tr>

      and here is the code in getListPictureFiles:
      <?php
      $filename= $modx->documentObject['description'];
      $filename_close = 'assets/images/species/'.$filename.'/'.$filename.'_close_mini.jpg';
      $filename_hole= 'assets/images/species/'.$filename.'/'.$filename.'_hole_mini.jpg';
      
      if (file_exists($filename_close)) {
      echo '
      <td class="imageCell" width="30px"><img class="listImgLeft" src="'.$filename_close.'" /></td>';
      } else {
      echo '<td class="imageCell" width="30px">No picture yet</td>';
      }
      
      if (file_exists($filename_hole)) {
      echo '
      <td class="imageCell" width="32px"><img src="'.$filename_hole.'" /></td>';
      } else {
      echo '<td class="imageCell" width="32px">No picture yet</td>';
      }
      ?>

      Here is the original side, without the file-exists test.
      And here is the side with the file-exists-test that doesn’t work.
        I think, thererfor I am! But what I am, and why...?
        • 29076
        • 615 Posts
        Thanks for answering my question. smiley
        Quote from: ganeshXL at Aug 04, 2008, 09:11 AM

        First of all, I would alternate snippet caching syntax: Both the ditto call and the snippet inside your tpl use [[foo]] syntax. Change one of those to [!foo!]
        I tried both, in all directions, but with no luck.
        Quote from: ganeshXL at Aug 04, 2008, 09:11 AM

        Then I noticed that the image filename / folder uses underscores, but the description uses spaces. Shouldn’t you str_replace() first? i.e.
        $filename= $modx->documentObject[’description’];
        $filename= str_replace(" ", "_", $filename);
        The underscores is a part of my naming standard, so that has to be there.
        Quote from: ganeshXL at Aug 04, 2008, 09:11 AM

        You could also try to write your own custom PHx modifier:
        http://wiki.modxcms.com/index.php/PHx#Custom_modifiers
        ( and add it to this list: http://wiki.modxcms.com/index.php/PHx/CustomModifiers )
        I tried some of the suggestions, but with no luck.

        No, I think it has something to do with the parsing system and the order of the snippetcall in the snippetcall. That’s because the snippet containing
        $filename= $modx->documentObject['description'];
        works fine when called directly from the template, but not when called from within a Ditto template chunk.
          I think, thererfor I am! But what I am, and why...?
          • 29076
          • 615 Posts
          Fantastic! That was the sollution.

          I really have to read more about the MODx commands.

          Thank you very much. grin
            I think, thererfor I am! But what I am, and why...?
            • 28173
            • 409 Posts
            Hi,

            I have the same problem : if a file exist I want to insert a code like <img src="[*id*].jpg" /> and if not, insert a code like <img src="default.jpg" />

            Can you please edit the first post, or post a new one to explain step by step your solution?
            Because it’s not very clear for me... thank a lot if you can find time to help me smiley