We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8694
    • 61 Posts
    Hi,

    I have an snippet that should read the Template-ID of the current resource.
    I try this:

    $template = $modx->resource->get('template');

    But this will work only when I call this snippet in a "regular" document.

    e.g. When I call the snippet inside a chunk for e.g. getResources (I want read the templates from all listed documents)
    that outputs me only the Templat-ID from then Dokuments where the getResources call ist located and not from all the specific
    documents that are listet.

      • 28215
      • 4,149 Posts
      That’s because $modx->resource is *always* the currently loaded document. If you’re calling a snippet in getResources, you need to pass in the current ID in the iteration:

      [[!mySnippet? &resourceId=`[[+id]]`]]
      


      And then access it via:

      $resource = $modx->getObject('modResource',$resourceId);
      

        shaun mccormick | bigcommerce mgr of software engineering, former modx co-architect | github | splittingred.com
        • 8694
        • 61 Posts
        Hm...okay. And how can I read the template-ID?
          • 8694
          • 61 Posts
          This prints nothing out:

          $resource = $modx->getObject('modResource',$resourceId);
          echo $resource;


          It stays empty.
            • 31037
            • 358 Posts
            $resource = $modx->getObject('modResource',$resourceId);
            $template = $resource->get('template');
            echo $template;
              • 8694
              • 61 Posts
              The point is that $resource stays empty.

              I call it so:
              [[!chkTemp1? &resourceId=`[[+id]]`]]

              Snippet:
              $resource = $modx->getObject('modResource',$resourceId); 
              
              $doc = $modx->getObject('modResource', $resource); 
              $template = $doc->get('template'); 
              
              
                • 31037
                • 358 Posts
                Sorry, just checking, do you return the value? It is not in the snippet you show.
                return $template;
                The code in your snippet works for me.

                Another way is to use the add-on getResourceField.
                [[!getResourceField? &id=`[[+id]]` &field=`template`]]
                I use it all the time. (I see there is an add-on called getValue, perhaps that one does the same, haven’t checked.)

                  • 8694
                  • 61 Posts
                  Still not working:

                  I try many things. E.g.
                  $resource = $modx->getObject('modResource',$resourceId);
                  $template = $resource->get('template');
                  echo $template;
                  echo $resource;
                  


                  $template bring me the current template (*)
                  $ resource is still empty

                  (*) But wehen I put this snippet inside a chunk for e.g. getResources it prints me out the Template from the documente where
                  the getResources call is located. But I want the Template-IDs from all listet documents.
                    • 8694
                    • 61 Posts
                    No I can not use GetResourceField in this case.
                    I work with a search snippet. The Search Results have to be filtered what template the result has.
                    The Problem ist that the SearchSnippet dont work with the IF-Snippet or the Input/Output filters.
                    (statement from the programmer of the snippet)

                    So the solution is to write a snippet and locate it inside the chunk that show the results:
                    e.g the chunks look like this

                    <a [[snippet]] href="blabla.html">Blabla</a>

                    The snippet should do that:
                    Pseudo-Code:
                    If Template = 10 then
                    return 'class="pop"'
                    
                      • 8694
                      • 61 Posts
                      --