We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 18397
    • 3,250 Posts
    This is an auto-generated support/comment thread for TV Conditionalp.

    Use this forum to post any comments about this addition or any questions you have regarding its use.

    Brief Description:
    Template Variable Conditional is a snippet designed to show a chunk if a template variable is equal (or not equal) to a value.
      • 4095
      • 372 Posts
      I’m not really up with TV’s (I know I’m missing out here) so please forgive me if this is a silly question.

      Could this be used to display a chunk if a certain date condition is present? Say if the current date is equal to or after a specified date?

      I’m still trying to do something like I mentioned in the following thread. Although I now use maketable for Course, I have to add a staff profile to the profile page, but the person doesn’t start till the 25th so on the 25th I have to ensure I log in and create the profile.

      Would much rather do it now while I had the time and it automatically added to the page on the 25th

      http://modxcms.com/forums/index.php/topic,2412.msg16313.html#msg16313
      http://modxcms.com/bugs/task/225?histring=publish&tasks=last
        [img]http://www.emanz.ac.nz/assets/images/logo/emanz-icon_16x16.gif[/img] Emergency Management Academy of New Zealand [br] http://www.emanz.ac.nz[br][br]MODx Sandbox Login: sandbox Password: castle [br]
        Admin Sandbox Login: sandbox Password: castle
        • 19083
        • 15 Posts
        Wow! This is a fantastic snippet. I’ve been trying to accomplish the same thing using PHx, but without any luck. While looking through the resources, I stumbled upon TV Conditional, and I’m glad I did.

        Great work.
          • 21496
          • 225 Posts
          This is a great snippet, however I’m running into a problem: I have a Ditto call in a chunk which is no longer parsed then. Is there a way to handle that?
            René
            • 7231
            • 4,205 Posts
            I end up using two calls to this snippet to reproduce an if / else statement. It would be cool to have this work with two chunks where if the condition were true or false (depending on the mode) you get one chunk and if not you get another.

            So I rolled up the sleeves and brushed off some of the php dust and modified it a bit as follows:

            the call...
            [!TVifelse? &tv=`TV name` &cnd=`value, mode` &rtn=`name of IF chunk to return` &rtn2=`name of ELSE chunk to return`!]


            the snippet...
            $tv = isset( $tv ) ? $tv : "";
             // get tv name
            $cnd = isset( $cnd )? $cnd: "";
             // get conditional
            $rtn = isset( $rtn )? $modx -> getChunk( $rtn ): "";
             // get chunk to return
            $rtn2 = isset( $rtn2 )? $modx -> getChunk( $rtn2 ): "";
             // get chunk to return 
            $id = isset( $id )? $id: $modx -> documentObject['id'];
             // get current page ID
            $tvarray = $modx -> getTemplateVarOutput( $tv, $id, $published = 1 );
             $cnd = explode( ',', $cnd );
             $value = $cnd[0];
             $filtertype = isset( $cnd[1] ) ? $cnd[1] : 2;
             if ( $filtertype == 1 && ( !isset ( $tvarray[$tv] ) || $tvarray[$tv] != $value ) ) {
                 return $rtn;
                 } else if ( $filtertype == 1 && ( !isset ( $tvarray[$tv] ) || $tvarray[$tv] == $value ) ) {
                 return $rtn2;
                  } else if ( $filtertype == 2 && $tvarray[$tv] == $value ) {
                 return $rtn;
                  } else if ( $filtertype == 2 && $tvarray[$tv] != $value ) {
                 return $rtn2;
                 }


            I renamed it to TVifelse so that I can have both snippets available. The mode and value work just as with TV Conditional, only difference is the second chunk.
              [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

              Something is happening here, but you don't know what it is.
              Do you, Mr. Jones? - [bob dylan]
              • 7231
              • 4,205 Posts
              I just noticed that tvConditional doesn’t work with default values or @INHERIT. Is there a way to make these features work with this snippet?

              Edit: upon furthur research it seams that the problem is with the @INHERIT and checkbox type tvs. I will open a new post to address this issue dirrectly. Sorry.
                [font=Verdana]Shane Sponagle | [wiki] Snippet Call Anatomy | MODx Developer Blog | [nettuts] Working With a Content Management Framework: MODx

                Something is happening here, but you don't know what it is.
                Do you, Mr. Jones? - [bob dylan]
                • 16834
                • 93 Posts
                Hello

                I notice this snippet is listed as working with older versions of modx - is that because it has been superceded or is the snippet just no longer maintained? I am using modx 0.9.6.1p2.

                I am trying to get a Chunk displayed when a document has a tag (in the form of a template variable). I was hoping this snippet would do the job.

                I *think* the conditional bit is working however the required chunk is not parsed - the chunk code is actually displayed in the site front-end.

                What am I doing wrong?

                The snippet call is
                [!tvcnd? &tv=`docTags` &cnd=`'',1` &rtn=`tagLink2`!]


                The code to the tagLink2 chunk is being presented.

                Thanks

                L
                  • 40045
                  • 534 Posts
                  First of all, thanks for this snippet, safed me some serious hairpulling =)...

                  It works up to 90%, I just have a problem together with Ditto and I couldn’t solve it myself, this is the reason I’m warming up such an old thread.

                  What am I trying to do?: I think it’s simple but anyways...I try to aggregate articles on the front page. You can see my modx directory/container-structure here:

                  http://picasaweb.google.ch/lh/photo/A1X3bqd7Kq34SE_Bag-czQ?feat=directlink


                  I have containers for the nav called "Home", "Musik" and so on, then I made subcontainers for the articles which have one page for each article. I then make a Ditto-Call
                  [!Ditto? &parents=`65` &tpl=`cfm_content_dto.articleTpl` &orderBy=`menuindex ASC`!]
                  in the content area of "Home" with a special template. This works fine, all the articles are displayed. The problem I had now are some template variables...they are optional, means there can be content (link or image) in it or there can be no content. In case no content is inserted the TV should not be displayed. (see http://picasaweb.google.ch/lh/photo/tsCWGaWnL8_SqxE1ybNlsQ?feat=directlink)


                  here my Ditto-Template (cfm_content_dto.articleTpl) before my try. This displays the articles right with links, images and everything...but if there is no link for the title or no image, placeholders are put in or in case of the title the url of the page is simply inserted instead of <span>[+title+]<span> (without the <a href></a>)...In consequence a header that isn’t ment as a link becomes a link and I wanted to change that, so it stays unlinked:

                  				<div class="article">
                  					<h1 class="article-header png">
                  						<span><a href="[+cfm_article_link+]">[+title+]</a></span>
                  						<small>» [+longtitle+]</small>
                  					</h1>
                  					<a href="[+cfm_article_thumb_link+]"><img src="[+cfm_article_thumb_src+]" alt="Artikel Thumbnail" class="content-thumb" /></a>
                  					[+introtext+]
                  					<div class="mehr">
                  					<a href="[+cfm_article_img_link+]"><img src="[+cfm_article_img_src+]" alt="Bierhübeli Logo" class="content-img" /></a>
                  					[+content+]
                  					</div>
                  					<input type="button" name="mehr" class="mehr-button no-script" value="mehr..." />
                  				</div>



                  So I found this snippet and the extended version from dev_cw

                  $tv = isset( $tv ) ? $tv : "";
                   // get tv name
                  $cnd = isset( $cnd )? $cnd: "";
                   // get conditional
                  $rtn = isset( $rtn )? $modx -> getChunk( $rtn ): "";
                   // get chunk to return
                  $rtn2 = isset( $rtn2 )? $modx -> getChunk( $rtn2 ): "";
                   // get chunk to return 
                  $id = isset( $id )? $id: $modx -> documentObject['id'];
                   // get current page ID
                  $tvarray = $modx -> getTemplateVarOutput( $tv, $id, $published = 1 );
                   $cnd = explode( ',', $cnd );
                   $value = $cnd[0];
                   $filtertype = isset( $cnd[1] ) ? $cnd[1] : 2;
                   if ( $filtertype == 1 && ( !isset ( $tvarray[$tv] ) || $tvarray[$tv] != $value ) ) {
                       return $rtn;
                       } else if ( $filtertype == 1 && ( !isset ( $tvarray[$tv] ) || $tvarray[$tv] == $value ) ) {
                       return $rtn2;
                        } else if ( $filtertype == 2 && $tvarray[$tv] == $value ) {
                       return $rtn;
                        } else if ( $filtertype == 2 && $tvarray[$tv] != $value ) {
                       return $rtn2;
                       }


                  so I made two chunks, one if there is a link in the TV and one if there’s none.

                  cfm_art.header.linked
                  <span><a href="[*cfm_article_link*]">[*pagetitle*]</a></span>


                  cfm_art.header.unlinked
                  <span>[*pagetitle*]</span>


                  then I changed the Ditto-Template to (Notice the Snippet call):

                  				<div class="article">
                  					<h1 class="article-header png">
                  						[[TVifelse? &tv=`cfm_article_link` &cnd=`, 1` &rtn=`cfm_art.header.linked` &rtn2=`cfm_art.header.unlinked`]]
                  						<small>» [+longtitle+]</small>
                  					</h1>
                  					<a href="[+cfm_article_thumb_link+]"><img src="[+cfm_article_thumb_src+]" alt="Artikel Thumbnail" class="content-thumb" /></a>
                  					[+introtext+]
                  					<div class="mehr">
                  					<a href="[+cfm_article_img_link+]"><img src="[+cfm_article_img_src+]" alt="Bierhübeli Logo" class="content-img" /></a>
                  					[+content+]
                  					</div>
                  					<input type="button" name="mehr" class="mehr-button no-script" value="mehr..." />
                  				</div>


                  this does give me "Home" as title for every Article and that’s the pagetitle of the container-page and not the title of the article-page.



                  For the article-pages I have made a specific template (to display every article on its own page if necessary):

                  				<div class="article">
                  					<h1 class="article-header png">
                  						[!TVifelse? &tv=`cfm_article_link` &cnd=`, 1` &rtn=`cfm_art.header.linked` &rtn2=`cfm_art.header.unlinked`!]
                  						<small>» [*longtitle*]</small>
                  					</h1>
                  					<a href="[*cfm_article_thumb_link*]"><img src="[*cfm_article_thumb_src*]" alt="Artikel Thumbnail" class="content-thumb" /></a>
                  					[*introtext*]
                  					<div class="mehr">
                  					<a href="[*cfm_article_img_link*]"><img src="[*cfm_article_img_src*]" alt="Artikel Image" class="content-img" /></a>
                  					[*content*]
                  					</div>
                  					<input type="button" name="mehr" class="mehr-button no-script" value="mehr..." />
                  				</div>


                  I made the same Snippet call (ok, I let it uncached here because it’s not called in a snipptets template like above) and here it works. So when I view the single article pages the titles are linked or not depending if there is something in the cfm_article_link TV...

                  this says to me that there must be a problem with the document ID from which the title is taken, could it be from the TVifelse Snippet

                  $id = isset( $id )? $id: $modx -> documentObject['id'];
                   // get current page ID


                  or is it a Ditto problem because TVs in Ditto are called with [+tvname+] instead of the normal [*tvname*]??

                  I already tried:

                  cfm_art.header.linked
                  <span><a href="[+cfm_article_link+]">[+pagetitle+]</a></span>


                  cfm_art.header.unlinked
                  <span>[+pagetitle+]</span>


                  but this didn’t even output a wrong title (like "Home") it did nothing at all...

                  I really don’t know further and would appreciate every help! Thx in advance.