Textpattern CMS support forum
You are not logged in. Register | Login | Help
- Topics: Active | Unanswered
Parsing php in section pages
I have a block of php which loads at top of page where i set the variable $mailsent
<txp:php>
...
$mailsent = 0;
if (isset ($_GET['action'])) { ... cool stuff ... }
...
</txp:php>
1. I am try to pass the value to txp so that i can use txp:tags to manipulate page. I tried the code underneath but it just stops the rendering of all html children that follow?
Why do you think that is?
<txp:variable name="mailsent" value='<txp:php >$mailsent</txp:php>' />
<h1><txp:variable name="mailsent" /></h1>
2. Even this simple snippet give an error?
<txp:php >$mailsent</txp:php>
<!-- Heres the error -->
Parse error: syntax error, unexpected $end in /home/mysite/textpattern/publish/taghandlers.php(3127) : eval()‘d code on line 1
Last edited by Timid&friendly (2010-06-16 18:44:36)
I think, therefore I AM, … … er … I think :-?
Offline
#2 2010-06-16 18:45:13
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Parsing php in section pages
Does closing the tag help?
<txp:variable name="mailsent" value='<txp:php >$mailsent</txp:php>' />
Edit: I see you noticed ;)
What about removing the space? <txp:php>
Last edited by els (2010-06-16 18:47:49)
Offline
Re: Parsing php in section pages
no difference afraid
<txp:variable name="mailsent" value="<txp:php>$mailsent</txp:php>" />
<h1><txp:variable name="mailsent" /></h1>
I think, therefore I AM, … … er … I think :-?
Offline
#4 2010-06-16 18:57:33
- els
- Moderator
- From: The Netherlands
- Registered: 2004-06-06
- Posts: 7,458
Re: Parsing php in section pages
Maybe someone should look at your php code, if even only the <txp:php>...</txp:php>
part causes an error.
Offline
Re: Parsing php in section pages
rgr
I think, therefore I AM, … … er … I think :-?
Offline
Re: Parsing php in section pages
Timid&friendly
You’re not far off but you don’t do it like that in PHP. Here’s a quick rundown that should help you get underway:
- If you want to share variables outside the block of code with other blocks of PHP you need to declare the variables as global, e.g.
global $mailsent
. - No need to use the raw
isset($_GET['action'])
. Use TXP’s built-ingps
function like this:if (gps('action')) { ... cool stuff ... }
- If you want to use variables inside a
<txp:variable />
later in the page you can approach it differently. First of all import$variable
from the global scope, then assign ‘mailsent’ as an entry in the$variable
array. Like this:
<txp:php>
global $variable;
$variable['mailsent'] = 0;
</txp:php>
You can then use <txp:if_variable name="mailsent" value="0">cool stuff</txp:if_variable>
later in the page.
Alternatively if you want to do it your way, make sure that $mailsent
is declared global in your PHP block and then:
<txp:variable name="mailsent" value='<txp:php>echo $mailsent;</txp:php>' />
<h1><txp:variable name="mailsent" /></h1>
The echo
simply outputs the value of the variable. And you need to have a semicolon after every statement / group or the parser gets mad.
Hope that helps.
Last edited by Bloke (2010-06-16 19:15:48)
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
Re: Parsing php in section pages
This is all new to me so this really helps a lot.
I’ll take a look tomorrow after a nights sleep :-) Then i will be better able to digest these hearty morsels. Thx for you time.
I think, therefore I AM, … … er … I think :-?
Offline
Re: Parsing php in section pages
That helped immensely.
The main actions all work thx so much.
I am now busy with the finishing touches. What i have tried to do is capture the user entered email address when form is submitted and place the value in a global variable. Then txpify the value so that i can tell users that a confirmation email has been sent to <txp:variable name=“emailadr” />
No matter what i try i am unable to pass the php value to txp grrrrrr. But i don’t have enough knowledge to see the variable is not being set.
<!-- error message -->
Tag error: <txp:variable name="emailadr" /> -> Notice: Undefined index: emailadr on line 3724
<!-- form code -->
<txp:php>
require_once('mailerfunction.php');
global $variable;
$variable['mailsent'] = 0;
global $emailadr;
$emailadr['email'] = 0;
if (isset ($_GET['action'])) {
if ($_GET['action'] == 'post') {
...
$message .= "E-mail address: ".$_POST['email']."\n";
...
// send to user
$M2 = new MailMessage;
$M2->setTo($_POST['email']);
$M1->setMessage($message);
$M2->sendtheMailfunction();
//
$emailadr['email'] = $_POST['email'];
$variable['mailsent'] = 1;
}
}
</txp:php>
I think, therefore I AM, … … er … I think :-?
Offline
Re: Parsing php in section pages
Well, you haven’t set $variable['emailadr']
anywhere in the code you’ve posted. That’s what the error message is telling you.
Code is topiary
Offline
Re: Parsing php in section pages
Ok. I thought that $emailadr['email'] = $_POST['email'];
was setting it?
I changed it to $variable['email'] = $_POST['email'];
and now it works. Cheers
Is $variable
a keyword in txp or does it possess secret powers i am unaware of?
Last edited by Timid&friendly (2010-06-17 17:49:27)
I think, therefore I AM, … … er … I think :-?
Offline
Re: Parsing php in section pages
$variable
is a Txp global variable. It’s an array and it contains the variables used by the variable
and if_variable
tags.
<txp:variable name="foo" value="bar />
is exactly the same as:
<txp:php>
global $variable;
$variable['foo'] = 'bar';
</txp:php>
is exactly the same as:
<txp:php>
variable(array('name'=>'foo','value'=>'bar'));
</txp:php>
Code is topiary
Offline
Re: Parsing php in section pages
Ah HA! I thought that “bloke” was being generic in his suggestion of what i thought was an arbitrary variable name. This is extremely important to know for all us non programmers. THX
I think, therefore I AM, … … er … I think :-?
Offline