Author Topic: Using The If Function (Repost)  (Read 919 times)

0 Members and 1 Guest are viewing this topic.

Offline a4evermedia

  • Global Moderator
  • Full Member
  • ***
  • Posts: 122
  • Karma: +25/-0
  • Gender: Male
    • View Profile
    • A4Ever Media
Using The If Function (Repost)
« on: July 30, 2012, 03:54:58 AM »
stock5  Posted: 05-06-2009 05:50pm

--------------------------------------------------------------------------------
If I have a page with two tags (tag1 and tag2), how can I match tag2 using {if}?

Something like this:

{if $page.tags == 'tag2'}

but it doesn't work..  
JMB-Jeremy  Posted: 05-07-2009 07:08am

--------------------------------------------------------------------------------
You will need to use the strpos PHP function to check if a tag is contained in the tags value. Example:


{if strpos($page.tags,'tag2') !== false}
stock5  Posted: 05-25-2009 03:39pm

--------------------------------------------------------------------------------
Thanks :) A related question: Does {if} support greater than, less than operators? Ex:

{if $page_galleries < '2'} do something {/if}  
JMB-Jeremy  Posted: 05-25-2009 07:55pm

--------------------------------------------------------------------------------
Yes, it supports any of the operators that a regular PHP if tag allows.  
stock5  Posted: 05-25-2009 08:14pm

--------------------------------------------------------------------------------
Any idea why this doesn't work:

{if $page_galleries < '2'} do something {/if}

when there's only 1 gallery displayed?  
JMB-Jeremy  Posted: 05-26-2009 07:00am

--------------------------------------------------------------------------------
The $page_galleries is a special template value that is not calculated until after the page has been built, so it is not currently possible to use that value inside an {if} function.  
stock5  Posted: 05-26-2009 11:28am

--------------------------------------------------------------------------------
Would it be possible to use {phpcode}?

For example:


{phpcode}
if ($page_galleries < '2') echo "";
{/phpcode}
JMB-Jeremy  Posted: 05-26-2009 04:54pm

--------------------------------------------------------------------------------
Sorry, that is not currently possible.  
stock5  Posted: 06-19-2009 01:22am

--------------------------------------------------------------------------------
Hmm that's odd, it works with {phpcode} but not with {php} (I forgot htmlspecialchars which I assume is why it wasn't working). So TGPX values can be used in {phpcode} but not with {php}, is this correct?

edit: or is it just that $page.galleries can't be used in {php}, but others can?  
JMB-Jeremy  Posted: 06-19-2009 06:11am

--------------------------------------------------------------------------------
It is not possible to use any of the template values inside a {phpcode} section, since those values are only valid during the building process and cannot be accessed after the page has been built. You can access them inside {php} sections, however because the $page_galleries template value is a special value that is one that will not work.  
stock5  Posted: 06-19-2009 11:22pm

--------------------------------------------------------------------------------
That's odd. I'm seeing the opposite, template values work inside {phpcode} but not {php}. For instance, this code works:


{phpcode}
if ('{$page_galleries|htmlspecialchars}' < '2') echo "
";
{/phpcode}
but this code doesn't


{php}
if ('{$page_galleries|htmlspecialchars}' < '2') echo "
";
{/php}
I've tried it with other templates values with the same results. Any idea why?  
JMB-Jeremy  Posted: 06-20-2009 06:29am

--------------------------------------------------------------------------------
Using the template variables in this manner isn't something that is officially supported by the template system. While you may find that it works in some cases, you should also be aware that you may get unexpected results or it may not work at all in future updates. Put simply: use at your own risk.  
stock5  Posted: 06-20-2009 04:16pm

--------------------------------------------------------------------------------
Hmmm I see. The only reason I ask is because I'm currently using {phpcode} inside stock {foreach} functions, which works fine but I imagine it might stress the server if I had more visitors. But it sounds like it's not possible to use TGPX template values inside {php} so I guess I don't have a choice.

This may sound like a stupid question, but does {if} support the PHP function file_exists? If it does I could just do away with {php} and {phpcode} altogether.  
JMB-Jeremy  Posted: 06-21-2009 08:14am

--------------------------------------------------------------------------------
Yes, you can use the file_exists PHP function in a TGPX {if} template function.  
stock5  Posted: 06-21-2009 09:04am

--------------------------------------------------------------------------------
What am I doing wrong here? I can't seem to get this to work:


{if file_exists('test.php')} file exists {else} file does not exist {/if}
Thanks very much in advance.  
JMB-Jeremy  Posted: 06-22-2009 03:47pm

--------------------------------------------------------------------------------
The template code is fine, however you should use a full directory path to the file since the working directory of the script while it is running the build process may not be the directory you expect.


{if file_exists('/full/directory/path/to/test.php')} file exists {else} file does not exist {/if}
stock5  Posted: 06-29-2009 01:40am

--------------------------------------------------------------------------------
Awesome, that did it. Is it possible to use template variables inside the {if} statement? For instance,


{assign var=$fixed_pageurl value=str_replace('http://www.domain.com/', '', $category.page_url)}
{if file_exists('../../{$fixed_pageurl}')} file exists {else} file does not exist {/if}
And congrats on on the release of tubex :) I think I'll pick up a copy later today.  
JMB-Jeremy  Posted: 06-29-2009 06:37am

--------------------------------------------------------------------------------
Yes, that can be done with a small modification:


{if file_exists('../../' . $fixed_pageurl)}
stock5  Posted: 07-01-2009 03:57am

--------------------------------------------------------------------------------
Thanks a lot, one last question: how do I append a filename to $fixed_pageurl? For example,


{if file_exists('../../' . $fixed_pageurl'index.php')}

if that makes sense..  
JMB-Jeremy  Posted: 07-01-2009 09:05am

--------------------------------------------------------------------------------
The period character is used for string concatenation in PHP, so that would look like this:


{if file_exists('../../' . $fixed_pageurl . 'index.php')}

Unofficial JMB Support Forums

Using The If Function (Repost)
« on: July 30, 2012, 03:54:58 AM »


Tags:
 

Related Topics

  Subject / Started by Replies Last post
0 Replies
924 Views
Last post July 30, 2012, 03:48:07 AM
by a4evermedia
0 Replies
856 Views
Last post July 30, 2012, 04:01:13 AM
by a4evermedia
0 Replies
930 Views
Last post July 30, 2012, 04:10:46 AM
by a4evermedia
0 Replies
758 Views
Last post November 02, 2013, 08:57:30 AM
by Huge
1 Replies
1111 Views
Last post June 10, 2014, 12:31:58 PM
by ne0shell