Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
#421 2014-09-19 19:15:56
Re: smd_if: Generic multiple if condition tests
Hi,
I’m trying to make an articlecount with this plugin, this is my code
<txp:variable name="numart">
<txp:mdn_count section='<txp:section />' status="4,5" />
</txp:variable>
<txp:variable name="numart" /> <!-- renders 4 -->
<txp:smd_if field="txpvar:numart" operator="gt" value="1">
<h2>You might also like: </h2>
<txp:article form="shortsummary" /> <!-- renders nothing -->
</txp:smd_if>
Thanks for any help
Offline
#422 2014-09-19 20:06:10
Re: smd_if: Generic multiple if condition tests
Manaus wrote #283904:
I’m trying to make an articlecount with this plugin
Two potential things are scuppering your efforts:
- Your variable name has spaces in it. More to the point, it has a newline, two spaces, then the number, then a newline between the start and end txp:variable tags. To use it as a comparison you need to do one of two things:
- Remove the spaces so everything’s on one line:
<txp:variable name="numart"><txp:mdn_count section='<txp:section />' status="4,5" /></txp:variable>
. Or; - Add
:TRIM
to smd_if’sfield
attribute:field="txpvar:numart:TRIM"
which will remove leading and trailing whitespace before testing.
- Remove the spaces so everything’s on one line:
- You probably need to consider use
<txp_article_custom section='<txp:section/>' />
instead of a vanilla article tag. Since your counter is using the current section, a plain article tag will work in this case if you are in a list context. But if you’re trying to display other articles in the same section when you are viewing an individual article, you’ll just get the same article you are currently viewing in the list! So you might like to just alter it now to save you any potential head scratching in future.
Hope that helps.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#423 2014-09-19 20:18:06
Re: smd_if: Generic multiple if condition tests
Thanks Bloke,
removing the whitespace in txp:variable
did the trick! :)
Offline
#424 2015-02-26 18:40:23
Re: smd_if: Generic multiple if condition tests
I’m hoping smd_if can solve a problem.
I have access to a url (not generated by Textpattern) that contains very simple status info.
On line 1 of the page it either states ACTIVE or DOWN. Based on that info, I’d like smd_if to show either the active or down images.
All the docs (as my small brain is understanding them) on smd_if appear to seek info from a TXP field. In this case the data is from an external url.
Can smd_if take input from an external url? If so, is there an example?
Offline
#425 2015-02-26 20:07:12
Re: smd_if: Generic multiple if condition tests
towndock wrote #288624:
Can smd_if take input from an external url?
No, smd_if is local only. But all is not lost. If you can get the URL contents into a txp:variable then smd_if can test if for you. Depending on your host, the simplest route is to use file_get_contents like this:
<txp:variable name="url_content"><txp:php>
return file_get_contents('http://example.com/some/url');
</txp:php></txp:variable>
But some hosts disable that. If so, you might have to fall back on cURL
<txp:variable name="url_content"><txp:php>
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'http://example.com/some/url');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
$data = curl_exec($ch);
curl_close($ch);
return $data;
</txp:php></txp:variable>
Either way, sometime later in your page, smd_if can check your variable in the usual way:
<txp:smd_if field="txpvar:url_content" operator="contains" value="ACTIVE">
Yay, site is up!
<txp:else />
Boo, site is down :-(
</txp:smd_if>
You may not want to get the entire page if it’s going to be big, so you might use elect to fetch the first few hundred bytes (or something). Look at CURLOPT_RANGE and add it as a line to the second example above if you want to try that, with a suitable byte range to extract from the full page.
Depending on the amount of traffic the site gets, you may also want to cache the results so you’re not hitting the remote server every page. We can explore that later if it’s a requirement.
Hope that helps.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#426 2015-02-27 13:58:23
Re: smd_if: Generic multiple if condition tests
Stef,
Thank you – this seems like a great simple solution. I have access to the url that provides status info – it provides simple info.
http://wave.marineweather.net/test/test.php
But somehow, this always returns a negative result (the term “Active” is definitely returned by the url):
<txp:variable name="url_content"><txp:php>
return file_get_contents('http://wave.marineweather.net/test/test.php');
</txp:php></txp:variable>
<txp:smd_if field="txpvar:url_content" operator="contains" value="Active">
Yay, site is up!
<txp:else />
Boo, site is down :-(
</txp:smd_if>
Is there some obviously wrong here? I can easily change how the url reports status if needed.
Offline
#427 2015-02-27 14:21:52
Re: smd_if: Generic multiple if condition tests
towndock wrote #288635:
somehow, this always returns a negative result
OK, a couple of things to check. After you assign the output of the URL to the variable, display what is actually in the variable using this snippet:
RESULT:<txp:variable name="url_content" />:END RESULT
If all you see is RESULT::END RESULT
then my money’s on your host disabling allow_url_fopen
. You can check this in your Txp Admin->Diagnostics panel.
If that’s disabled, it’ll scupper your attempts to get the URL via file_get_contents()
and you’ll need to go the cURL route (and hope that’s also not disabled!)
If, however, you are getting stuff in your variable then we need to look closer at your smd_if tag. I tried it on mine with the raw output from the URL and it worked so it should pick it up, but there are some caveats when using the contains
operator so you may need to add :NOSPACE
after it to modify the string prior to testing (but that’s a long shot).
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#428 2015-02-27 14:34:51
Re: smd_if: Generic multiple if condition tests
The variable output is OK – it shows the url content.
Where exactly would the :NOSPACE go?
Offline
#429 2015-02-27 15:24:10
Re: smd_if: Generic multiple if condition tests
towndock wrote #288637:
Where exactly would the :NOSPACE go?
operator="contains:NOSPACE"
But I can’t see how that’ll make any difference. Weird it’s not matching.
If the very first thing in the returned output is the word “Active” you could try the begins
operator. I can’t test that out right now since the URL is returning output that begins with Data not recent
.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#430 2015-02-27 15:42:11
Re: smd_if: Generic multiple if condition tests
Hmmmm. NOSPACE … no change.
FYI, the url will now again respond with Active.
I’ve even tried it on a different TXP install… same result. If you’re seeing it work on your TXP install, I’m pleased. There must be some simple error at my end – the url content is definitely delivering a match. The code all appears to make sense.
I’ll drink more coffee and look again.
Offline
#431 2015-02-27 16:06:48
Re: smd_if: Generic multiple if condition tests
towndock wrote #288639:
If you’re seeing it work on your TXP install, I’m pleased.
Well, I’m seeing it work if I hard-code the result of the URL into a variable:
<txp:variable name="url_content">Active Active Active
Active Active Active
Active Active Active Message date: Wed, 25-Feb-2015 10:10am EST...</txp:variable>
I can’t test it using file_get_contents()
because my host has disabled allow_url_fopen
:-(
So maybe there’s something in the way it’s assigned to the variable or coming back from the URL that’s borking it? I can only think of some hidden characters or that it’s encoded in something that smd_if can’t understand, even though it “looks” OK to the naked eye.
If you add debug="2"
to smd_if, does it give you any useful diagnosis of what it’s testing?
Also, try going back to basics. Hard-code your <txp:variable>
with the word Active
and check that smd_if is doing its job properly. Then alter the contents of the variable and check it either passes/fails your tests as expected, and work up from there to the full-blown URL content. Baffling.
The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.
Txp Builders – finely-crafted code, design and Txp
Offline
#432 2015-02-27 17:53:44
Re: smd_if: Generic multiple if condition tests
If I hard code in “Active” – SMD_IF works fine.
Here is the debug code (in this example the variable is “gyx_status”):
TEST 1
array (
0 => 'txpvar',
1 => 'gyx_status',
)
array (
0 => 'contains',
)
array (
0 =>
array (
0 => 'Active',
),
)
@$replacements['{smd_if_gyx_status}'] = $variable["gyx_status"];
@$replacements['{smd_if_len_gyx_status}'] = strlen($variable["gyx_status"]);
$out[0] = (isset($variable["gyx_status"]) && strpos(strtolower($variable["gyx_status"]), strtolower('Active')) !== false) ? 'true' : 'false';
RESULT:
array (
0 => 'false',
)
REPLACEMENTS:
array (
'{smd_if_val1}' => 'Active',
'{smd_if_len_val1}' => 6,
'{smd_if_gyx_status}' => '<?php
return file_get_contents(\'http://wave.marineweather.net/test/test.php\');
?>',
'{smd_if_len_gyx_status}' => 86,
)
It does seem like it almost has to be hidden character(s). I’m trying to check with other tools…
Offline