Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2007-09-05 00:57:10

WebKat
Member
Registered: 2007-01-16
Posts: 301

raw php in page templates

Here is what I need to put in…

at the top of the page:

<?php
    require 'moon-phase.cls.php';
    $mp = new moonPhase(''); // get moon-phase for today
    $moonImg = "moon".round($mp->getPositionInCycle() * 18).".jpg";
?>

and some inside an image tag:

<img src="./images/<?=$moonImg?>">

I tried using the <txp:php> … </txp:php> tag but I didn’t do it right, I guess. I’m rather lost at the whole php thing—I’m a designer, not a programmer—so can someone please tell me the proper way to format this? Thanks.



WebKat

Offline

#2 2007-09-05 01:46:55

rsilletti
Moderator
From: Spokane WA
Registered: 2004-04-28
Posts: 707

Re: raw php in page templates

Anything that works this way <?php stuff ?> will work this way <txp:php>stuff</txp:php>, the first thing to check would be your preference settings.

And this of course

Perhaps something like this, given $moonImg is a valid image pathname. If this is dependent on a set of images referenced
by selecting one by current phase as calculated by the code, you may have to tailor your path to find it.

<txp:php>
    require 'moon-phase.cls.php';
    $mp = new moonPhase(''); // get moon-phase for today
    $moonImg = "moon".round($mp->getPositionInCycle() * 18).".jpg";
    echo '<img src='.$moonImg.' />';
</txp:php>

Last edited by rsilletti (2007-09-05 03:40:10)

Offline

#3 2007-09-05 17:38:09

WebKat
Member
Registered: 2007-01-16
Posts: 301

Re: raw php in page templates

Yeah that is what I tried on my own, and it barfed out errors. And I already checked that I have the preferences set to allow raw php. I have other raw php code working, though it’s just a simple string that spits out the current year in my copyright notice.



WebKat

Offline

#4 2007-09-05 17:52:02

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: raw php in page templates

Enclose it in <notextile>

Offline

#5 2007-09-05 19:22:17

WebKat
Member
Registered: 2007-01-16
Posts: 301

Re: raw php in page templates

Ok I’ll try that.



WebKat

Offline

#6 2007-09-05 20:28:34

WebKat
Member
Registered: 2007-01-16
Posts: 301

Re: raw php in page templates

Here is what it spits out now:

<notextile><img src="/images/<br />
<b>Parse error</b>:  syntax error, unexpected '=' in <b>/home/jugglerd/public_html/dcmidnight_test/textpattern/publish/taghandlers.php(2738) : eval()'d code</b> on line <b>1</b><br />
"></notextile>


WebKat

Offline

#7 2007-09-05 21:01:27

ruud
Developer Emeritus
From: a galaxy far far away
Registered: 2006-06-04
Posts: 5,068
Website

Re: raw php in page templates

And you’ve done this: ?

<notextile><txp:php>
    require 'moon-phase.cls.php';
    $mp = new moonPhase(''); // get moon-phase for today
    $moonImg = "moon".round($mp->getPositionInCycle() * 18).".jpg";
    echo '<img src='.$moonImg.' />';
</txp:php></notextile>

Offline

#8 2007-09-05 21:29:52

Gocom
Developer Emeritus
From: Helsinki, Finland
Registered: 2006-07-14
Posts: 4,533
Website

Re: raw php in page templates

And for clarify, don’t use <img src="./images/<?=$moonImg?>">. Thought you did, so don’t. Use what rsilletti and ruud gave you.

Cheers!

Last edited by Gocom (2007-09-05 21:31:21)

Offline

#9 2007-09-05 21:58:09

WebKat
Member
Registered: 2007-01-16
Posts: 301

Re: raw php in page templates

Gocom, no, I wasn’t using that: I had it in two separate parts, the first part was ruud said, minus the echo line, which was up at the top of the page, then I put down in the page
<img src="./images/<txp:php>=$moonImg</txp:php>">
both with and without the notextitle tags. I also tried it without the = sign. That was the part that I couldn’t get to work.

I tried putting the whole chunk ruud gave down in the page (I didn’t know you could do that— thought that ‘require’ stuff had to be at the top of the file, which is why I kept it separate before—which is how it was in the original that I’m adapting this from) and now it seems to be pulling up the right filename but the tag is just <img src=moon0.jpg />

The problem now is that I don’t know the whole echo thing or whatever well enough to make it spit out <img src="/images/moon0.jpg" /> (I know it’s going to react weird to the quotes, and probably to the slashes)



WebKat

Offline

#10 2007-09-06 02:05:58

rsilletti
Moderator
From: Spokane WA
Registered: 2004-04-28
Posts: 707

Re: raw php in page templates

<img src=”./images/<txp:php>echo $moonImg;</txp:php>”> maybe?
The “=” before $moonImg may be giving you trouble. If I were using it in a different location on the page I would code it this way. (I’m a little fuzzy on the scope handling with this)

<notextile><txp:php>
global $moonImg;
 echo '<img src="./images/'.$moonImg.'" />';
</txp:php></notextile>

Offline

#11 2007-09-06 02:11:09

rsilletti
Moderator
From: Spokane WA
Registered: 2004-04-28
Posts: 707

Re: raw php in page templates

<img src=”./images/<txp:php>echo $moonImg;</txp:php>”> maybe?
The “=” before $moonImg may be giving you trouble. If I were using it in a different location on the page I would code it this way. (I’m a little fuzzy on the scope handling with this)

<notextile><txp:php>
global $moonImg;
 echo '<img src="./images/'.$moonImg.'" />';
</txp:php></notextile>

Offline

#12 2007-09-06 02:57:13

WebKat
Member
Registered: 2007-01-16
Posts: 301

Re: raw php in page templates

Yeah I tried it with and without the = and I tried something similar to your echo line as well after seeing ruud’s chunk. I will try what you put and see if that works.



WebKat

Offline

Board footer

Powered by FluxBB