We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 1761
    • 20 Posts
    I followed the "How to create a blog"MODx" tutorial.
    http://rtfm.modx.com/display/revolution20/Creating+a+Blog+in+MODx+Revolution

    In my homepage I have a getResourcetag call and in several sections (categories) as well.

    Homepage:
    [[!getResourcesTag?
      &elementClass=`modSnippet`
      &element=`getResources`
      &tpl=`blogPost`
      &parents=`2,3,4,5,6,7,10`
      &limit=`15`
      &includeContent=`1`
      &includeTVs=`1`
      &showHidden=`0`
      &hideContainers=`1`
      &pageVarKey=`page`
    ]]
    [[!+page.nav:notempty=`
    <div class="paging">  
    <ul class="pageList">  
      [[!+page.nav]]  
    </ul>  
    </div>
    `]]


    Section / categorie:
    [[!getResourcesTag?
      &element=`getResources`
      &elementClass=`modSnippet`
      &tpl=`blogPost`
      &hideContainers=`1`
      &pageVarKey=`page`
      &parents=`[[*id]]`
      &includeTVs=`1`
      &includeContent=`1`
    ]]
    [[!+page.nav:notempty=`
    <div class="paging">  
    <ul class="pageList">  
      [[!+page.nav]]  
    </ul>  
    </div>
    `]]


    My problem is that my sections use different templates. I would like to have my homepage getResourcetag to make use of those different section/categorie templates.

    If I remove
    &tpl=`blogPost`
    from the homepage I get a lot of code stuff on my website so that doesn't work. [ed. note: AnnysLM last edited this post 14 years, 10 months ago.]
      • 36442
      • 97 Posts
      When you remove &tpl parameter, none of templates is used to format your output, so you get raw output of the array, containing your data.
      If you want to style articles from different categories differently, you should adopt your template chunk (blogPost) to do this. One of the way I think of is to apply a special class to the container of article output depending on it's parent. So your template blogPost might look like this:
      <div class="category-[[+parent]]">
        <h3><a href="[[~[[+id]]]]">[[+pagetitle]]</a></h3>
        <p>[[+introtext]]</p>
      </div>

      Take a look at
      [[+parent]]
      placeholder, it returns id of the parent resource of the article. Then, you can apply different styling via CSS depending on your categories.
        • 1761
        • 20 Posts
        Right now I'm doing that. Not by using
        <div class="category-[[+parent]]">
        but I've attached a tv tag so you can add a class in the resource.

        In this class I've hidden a number of things to create a "small resource / page". This is done with display: none but all the code is still there in the source of the page.

        I was wondering if this can be done in a nicer / cleaner / better way...
          • 9102 ☆ A M B ☆
          • 318 Posts
          I have a snippet called valueToChunk that does something like this. It's very simple -- it just takes a value and an array of value-chunk pairs and outputs the contents of the appropriate chunk. You could pass it the value of the parent ID and an array associating each parent with a different template chunk, that should do what you want. Here is the snippet:

          <?php
          $chunks = array();
          parse_str($chunkNames,$chunks);
          if ($chunks[$value]) {
              return $modx->getChunk($chunks[$value]);
          }else{
              return'';
          }
          


          You would call it like this:

          
          [[!valueToChunk? &value=`[[+parent]]` &chunkNames=`1=template-for-parent-1&2=template-for-parent-2&3=template-for-parent-3`]]
          
          


          Where template-for-parent-1 is the name of the chunk you want to use as your getResources template for children of the resource with ID of 1 etc.

          Alternately, you could pass it the value of the current resource template and use different chunks for each template, or base it on some other parameter such as a TV.
          [ed. note: esnyder last edited this post 14 years, 10 months ago.]
            • 1761
            • 20 Posts
            @Freejung,

            Thank you for your answer. Your response to me is funny because it isn't that simple for me ;-). I have a miniscule knowledge of php. I know how to create a snippet but do I have to add properties?

            How do I combine the call for your snippet with the getResourceTag call I am using right know?

            I tried to use a tv in the getResourceTag call but that didn't work either.

            [[!getResourcesTag?
              &elementClass=`modSnippet`
              &element=`getResources`
              &tpl=`[[+tv.sortPost]]`
              &parents=`2,3,4,5,6,7,10`
              &limit=`15`
              &includeContent=`1`
              &includeTVs=`1`
              &processTVs=`1`
              &showHidden=`0`
              &hideContainers=`1`
              &pageVarKey=`page`
            ]]
            


            Sorry for my lack of knowledge. I'm still learning!
            It's just feels like there is a solution for this so the homepage will "inherit / use" the &tpl from the mentioned &parents.
              • 9102 ☆ A M B ☆
              • 318 Posts
              No worries -- I didn't mean that snippets in general are a simple subject, just that this one is pretty short as snippets go.

              The way you would use my snippet would be to call it in your template. So just use a normal getResourcesTag call with a template called blogPost like you have in the first post. Put the code in the second code block of my post above in the blogPost template. Then put whatever code you want to actually use for your templates in chunks called template-for-parent-1 etc. (or whatever you want to call them).

              You don't have to worry about properties or anything like that. When you call a snippet, whatever parameters you specify in the call are passed into the snippet. So just take the code in my first block above, create a new snippet called valueToChunk, and paste the code into it. You don't have to do anything else, the snippet should just work as-is.

              Edit: at first I thought your approach would work, but the problem is that you're using a TV in the getResourcesTag call, which appears on the homepage, right? So even if you use the correct syntax for the TV, it will just take the template value from the value of that TV for the homepage, so that won't work.

              [ed. note: esnyder last edited this post 14 years, 10 months ago.]
                • 9102 ☆ A M B ☆
                • 318 Posts
                Anyway, putting this all together and applying it to your particular set of parents -- first make the valueToChunk snippet as I described, don't worry about properties or anything like that. Then, on your homepage, just use the getResourcesTag call that you put in your first post.

                Then put this in the blogPost chunk:

                [[!valueToChunk? &value=`[[+parent]]` &chunkNames=`2=blogPost2&3=blogPost3&4=blogPost4&5=blogPost5&6=blogPost6&7=blogPost7&10=blogPost10`]]
                


                Then make chunks called blogPost2, blogPost3 etc. that contain the code you want to use for templates for blog posts for children of each parent respectively. Or, if you already have templates for posts that are children of those parents, replace "blogPost2" etc. in the code above with the names of your templates.

                Does that make sense?
                  • 1761
                  • 20 Posts
                  After reading it a few times it made sense! smiley But it's not working...

                  I've added your snippet. I'm using my getResourceTag call in my homepage and in my section/category pages. Below those are my posts/resources.

                  I've added the valueToChunk call to my blogPost tpl chunk and created the blogPost2, blogPost3, etc. chunks. On the website I see all my resources twice or double. The only post that shows up correctly is the one last added/published.

                  After this I figured that maybe the blogPost chunk should only have the valueToChunk call. Removed everything besided the valueToChunk call and that left me with emty posts on my homepage without content, tv's, etc. Only the last published resource is displayed the way I want!

                    • 9102 ☆ A M B ☆
                    • 318 Posts
                    I'm sorry, maybe I'm overcomplicating this -- you may have been better off without my "help."

                    You're right that it should have only the valueToChunk call.

                    I'm going to test this and get back to you. Maybe I'm skipping a crucial step or something.
                      • 9102 ☆ A M B ☆
                      • 318 Posts
                      OK, I figured out what the problem is -- my snippet is too simple, it doesn't pass the values of the current resource into the chunk.

                      I have a fix that works without TVs -- with TVs it gets a bit more complicated. I'm working on that, I should have it for you in a few minutes.

                      I'm glad this came up, actually, as this new snippet will be much more useful to me as well.