Using PHP scripts

Talk about writing modules and plugins for CMS Made Simple, or about specific core functionality. This board is for PHP programmers that are contributing to CMSMS not for site developers
Post Reply
pdedecker
Forum Members
Forum Members
Posts: 28
Joined: Sun Apr 16, 2006 8:25 am

Using PHP scripts

Post by pdedecker »

Is it possible to insert PHP code in a page? (not just predefined PHP-code, I want to able to edit the code right from the Page editor)
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm
Location: Fernie British Columbia, Canada

Re: Using PHP scripts

Post by calguy1000 »

you can use {literal}{/literal}

or write them once in a user defined tag
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
mikemcvey
Forum Members
Forum Members
Posts: 61
Joined: Tue May 02, 2006 4:08 am

Re: Using PHP scripts

Post by mikemcvey »

Hey, just a note.. the {literal} tags didn't work for me.
Just printed the contents in html code tag.

Best to just make a new tag anyway
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Using PHP scripts

Post by Ted »

You CAN use {php}echo "blah";{/php} tags, but you have to turn the option on in config.php.  I feel it's a little too unsafe to just leave wide open.
rcp

Re: Using PHP scripts

Post by rcp »

I've used {php} code snippets several times and they worked OK but I think I just found a bug in the latest CMSMS beta!
I have a page with a random background -


The preview in the admin works as expected but accessing the page outside admin doesn't, the {php} tags are ignored and this shows up:


Any idea why this happens? Thanks in advance!

(yup, have "$config['use_smarty_php_tags'] = true;" in config.php)
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Using PHP scripts

Post by Ted »

Is that in the page or the template?

Thanks!
rcp

Re: Using PHP scripts

Post by rcp »

Ted wrote: Is that in the page or the template?
Template.
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Using PHP scripts

Post by Ted »

Interesting.  I see it being a problem with the config file in content not getting parsed...

Open up lib/content.functions.php.  Look for

Code: Select all

	function content_get_template($tpl_name, &$tpl_source, &$smarty_obj)
	{
		global $gCms;
		$pageinfo = &$gCms->variables['pageinfo'];
and add this line to make it:

Code: Select all

	function content_get_template($tpl_name, &$tpl_source, &$smarty_obj)
	{
		global $gCms;
		$config =& $gCms->GetConfig();
		$pageinfo = &$gCms->variables['pageinfo'];
Then clear the cache and see if it helps any.  Thanks!
rcp

Re: Using PHP scripts

Post by rcp »

Wow, that solved the problem, thanks a lot.
1 less bug to go ;)
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Using PHP scripts

Post by Ted »

Excellent.  Thanks.  That'll be in the next release.
rcp

Re: Using PHP scripts

Post by rcp »

Seems like there are more places to apply that patch:

in a template - {php} $variable='value'; {/php}

in a page (that uses the template) - {php} echo $variable; {/php} - doesn't output anything. Tried putting global $variable everywhere but doesn't work.
Again, all works fine in preview in the admin tool.
Ted
Power Poster
Power Poster
Posts: 3329
Joined: Fri Jun 11, 2004 6:58 pm
Location: Fairless Hills, Pa USA

Re: Using PHP scripts

Post by Ted »

Actually, that isn't really a fixable thing.  templates and page aren't in the same scope, and you can't count on the order of which they'll get processed.  Chances are that declaring as global MIGHT work, but I wouldn't count on it.

A better bet would be to use smarty to do this.

{capture assign='variable'}value{/capture}

and then

{$smarty.capture.variable}

Though, I would some testing to make sure they're always used processsd in the same order.
Post Reply

Return to “Developers Discussion”