Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-10-11 21:11:57

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Strainge problems with inline PHP not outputting TXT tags

I have this snippet:

<txp:article>
  <div class="product">
    <h2 class="title is-2"><txp:title /></h2>
    <div class="columns">
      <div class="column is-one-third">
        <div>
          <txp:images>
            <txp:if_first_image id='<txp:article_image />'>
                <div class="large-image">
                  <txp:image />
                </div>
              <txp:else />
                <div class="image">
                  <txp:image />
                </div>
            </txp:if_first_image>
          </txp:images>
        </div>
      </div>
      <div class="column is-one-third">
        <h3>About</h3>
        <txp:body />
      </div>
      <div class="column is-one-third">
        <div class="add-button">
          <button type="button" class="button is-default" name="button">Add To Cart</button>
        </div>
        <txp:article_id />
        <?php

          $id = article_id([]);
          echo 'ID'.$id.'<br>';
          $list = $colors->getColors('<txp:article_id />');

        ?>

      </div>
    </div>
  </div>
</txp:article>

Within the php block, I am have strange issues working with the tags or functions.
The article_id([]) function returns nothing, not even null. I can however, output the actual tag by echoing it `echo ‘<txp:article_id />’; `
I can get a value with global functions like `section` or `category`, but not article tags.

While this should be fine passing the TXP version, when I pass the TXP tag to my function, I cannot for some reason render it as a number.
If I cast it as an integer `(int) $id` within my function, it returns 0. If I add 1 to it, `$id + 1`, it return 1. If use it in a query, the result is empty.

I’m a bit confused what’s happening.
Any ideas?
Thank you!
Rich

Last edited by richtestani (2018-10-11 21:13:47)

Offline

#2 2018-10-12 00:42:09

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Re: Strainge problems with inline PHP not outputting TXT tags

I find, when I turn on debugging, I get an error:

Textpattern Notice: Article tags cannot be used outside an article context. while parsing form None on page default

Now sure why its saying its out of context. Using the normal textpattern tag returns it properly.

Offline

#3 2018-10-12 02:03:37

kuopassa
Plugin Author
From: Porvoo, Finland
Registered: 2008-12-03
Posts: 228
Website

Re: Strainge problems with inline PHP not outputting TXT tags

Have you tried replacing <?php with <txp:php> and ?> with </txp:php>? ;-)

Offline

#4 2018-10-12 07:46:27

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,595
Website

Re: Strainge problems with inline PHP not outputting TXT tags

richtestani wrote #314552:

If you are in individual article context (you could also wrap everything in if_individual_article), you should be able to do:

<txp:php>
          global $thisarticle;
          $id = $thisarticle['thisid'];
          echo 'ID'.$id.'<br>';
          $list = $colors->getColors($id);
<txp:php>

or in your php only output if $thisarticle['thisid'] is set if you want to prevent errors.


TXP Builders – finely-crafted code, design and txp

Offline

#5 2018-10-12 08:43:14

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,270
Website GitHub

Re: Strainge problems with inline PHP not outputting TXT tags

If you want to use the tag directly, try this:

$list = $colors->getColors(parse('<txp:article_id />'));

Not sure if it needs the extra parse() call – Txp should run it through parse() automatically at least once, but PHP blocks may be exempt, I can’t remember offhand.

I suspect that since you can get section and category data but not article info, you might have a logic issue somewhere. You should check you are definitely in article context as jakob suggests by testing $thisarticle. If that isn’t the case, quite why the <@txp:article id />@ tag returns the correct article ID is a little baffling. It should do the same as the function.

I concur with kuopassa: replace your raw ?php tags. Raw tag support has been removed from Txp. Further, if you want to use $colors then remember to import it into your PHP block from the global scope:

global $colors;
...

Otherwise, when you do get the rest working, you might not be operating on the object you expect.


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

#6 2018-10-12 12:35:04

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Re: Strainge problems with inline PHP not outputting TXT tags

<txp:php>
          global $thisarticle;
          $id = $thisarticle['thisid'];
          echo 'ID'.$id.'<br>';
          $list = $colors->getColors($id);
        </txp:php> ->  Textpattern Notice: PHP code is disabled for pages. while parsing form None on page default

For some reason it tell me php code is turned off, though it is not and there is only a single user with the publisher role.

If I pass a number literal bc. $colors->getColors(5);
The results are passed back properly. Here is a few images showing the difference in results:

images showing passing a number or the article_id

Also, some tests on the item always returns false.

   echo '<txp:article_id />'; //5
          var_dump('<txp:article_id />'); //string(18) "5"
          var_dump('<txp:article_id />' == 5); //false
          var_dump('<txp:article_id />' === 5); //false
          var_dump('<txp:article_id />' == '5'); //false
          var_dump('<txp:article_id />' === '5')//false;

A little idea of what I am doing here, so you don’t think my setup is out of the box.

From the default page, I am loading a file and passing the route to load up supporting data. The route decides which template to load up, which the views have TXP tags and do get rendered by TXP, since TXP is still handling the final output. But having a weird issue with this one.

I will test out a normal template and see if I get the same issue. Unless someone see what is happening otherwise.

Thank you for the help and suggestions.
Rich

Offline

#7 2018-10-12 12:49:25

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Re: Strainge problems with inline PHP not outputting TXT tags

I setup a normal page in TXP and sent a section to that page, and using <txp:php>, I could output the article id, but not using standard <?php tags.

<?php echo article_id([]); //does not output ?>
<txp:php> echo article_id([]); //works just fine </txp:php>

I’ll keep at this today and tinker with my setup maybe I’ll stumble on something.
Thank you

Rich

Offline

#8 2018-10-12 13:05:46

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Strainge problems with inline PHP not outputting TXT tags

richtestani wrote #314567:

Also, some tests on the item always returns false.

echo '<txp:article_id />'; //5
var_dump('<txp:article_id />'); //string(18) "5"

Textpattern parses pages twice. On the first pass you get

<txp:article_id />
string(18) "<txp:article_id />"

which on the second pass becomes (on an individual article page only)

5
string(18) "5"

Offline

#9 2018-10-12 15:06:23

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Re: Strainge problems with inline PHP not outputting TXT tags

@etc

Would you say it’s not getting parsed a second time?

Offline

#10 2018-10-12 15:39:33

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Strainge problems with inline PHP not outputting TXT tags

richtestani wrote #314576:

Would you say it’s not getting parsed a second time?

Nope, it’s not parsed the first time (in your example). Inside <txp:php>...</txp:php>, '<txp:article_id />' is just a string, nothing more. If you want it to be parsed, say it:

var_dump(parse('<txp:article_id />') == 5); //must return true

Offline

#11 2018-10-12 15:56:32

richtestani
Plugin Author
Registered: 2009-11-08
Posts: 128

Re: Strainge problems with inline PHP not outputting TXT tags

etc wrote #314577:

Nope, it’s not parsed the first time. Inside <txp:php>...</txp:php>, '<txp:article_id />' is just a string, nothing more. If you want it to be parsed, say it:

var_dump(parse('<txp:article_id />') == 5); //must return true...

So that helped, I was echoing my body from my own response, so instead I wrapped in parse.

echo parse($response->getBody());

So now within php tags:

<txp:php>
   echo article_id([]); //outputs the proper id
</txp:php>

I still cannot use raw php tags with article_id, and get the out of context error. But I can get the ID. But, a new issue formed. Why cannot not access my own variables defined in the txp:php tags but I can with raw php tags?

<txp:php>
   global $colors;
   print_r($colors) //not defined
</txp:php>

then:

<?php
   print_r($colors) //is defined
?>

how can I pull in $colors which is available on the page otherwise?

Thank you
Rich

Last edited by richtestani (2018-10-12 16:01:46)

Offline

#12 2018-10-12 16:02:38

etc
Developer
Registered: 2010-11-11
Posts: 5,053
Website GitHub

Re: Strainge problems with inline PHP not outputting TXT tags

I think raw php support was deprecated and removed in recent versions, you must use <txp:php />. As for global variables, as soon as they are declared so in each block, it should work:

<txp:php>
   global $colors;
   $colors = ['blue', 'red'];
</txp:php>
...
<txp:php>
   global $colors;
   print_r($colors);
</txp:php>

Offline

Board footer

Powered by FluxBB