Jump to content

Resolved - Using PHP in templates


red sun

Recommended Posts

In an effort to solve an issue with the newsletter subscription form not providing a success message I thought a quick solution would be to look for the "subscribed" variable in the url and use php to echo a success message (I know the recommended approach is to create a plug-in but that is beyond my abilities).

 

I followed the instructions in the CubeCart support files for enabling PHP in templates and added the following code in controllers/controller.index.inc.php:

$GLOBALS['smarty']->allow_php_tag = true;

The documentation states

1. For front end skins server from the /skins folder please open the controllers/controller.index.inc.php file

 

However there is no "controllers" directory under "skins", so i modified "controllers/controller.index.inc.php".

 

I then added the smarty php tags as per http://www.smarty.net/docsv2/en/language.function.php.tpl

 

However when I try to add any code, or even just  empty {php} {/php} tags the page fails to load.

 

Has this feature been removed form the latest version of CubeCart or have I missed someting really basic?

 

 

 

 

 

 

Link to comment
Share on other sites

According to the Smarty docs, you can switch out the class instantiation from:

$GLOBALS['smarty'] = new Smarty();

to

$GLOBALS['smarty'] = new SmartyBC();

in the /controllers/controller.index.inc.php file.

(SmartyBC - backward compatibility - extends from Smarty.)

Link to comment
Share on other sites

Hi there - I tried that and also updated the path in autoloader.class.php to the following without any success.

require_once CC_INCLUDES_DIR.'lib'.CC_DS.'smarty'.CC_DS.'SmartyBC.class.php';

Your reply to my other post regarding problems with the newsletter form not showing a response has helped greatly, so I have no need to add custom php to the templates at this time.

 

Many thanks for your help so far. I may re-open this topic if I hit any future problems where custom php would provide a quick fix...

Link to comment
Share on other sites

Might also have needed to change the statement above:

if ($class == 'SmartyBC') {

 

Or, maybe, we could have not changed the controller file, not changed the if statement, and just relied on the changed require_once statement???

Link to comment
Share on other sites

A combination of all three of your suggestions did the trick.

 

In controllers/controller.index.inc.php change

$GLOBALS['smarty'] = new Smarty();

to

$GLOBALS['smarty'] = new SmartyBC();

 

in classes/autoloader.class.php change

if ($class == 'Smarty') {

      require_once CC_INCLUDES_DIR.'lib'.CC_DS.'smarty'.CC_DS.'Smarty.class.php';
      return true;
}

to

if ($class == 'SmartyBC') {
      require_once CC_INCLUDES_DIR.'lib'.CC_DS.'smarty'.CC_DS.'SmartyBC.class.php';
      return true;
}

 

Finally, I popped the following code into the template file box.newsletter.php

{php}
      echo (isset($_GET['subscribed']))?'<p class="confirmation">Thank you for subscribing</p>':'';
{/php}

 

Many thanks bsmither - another problem solved !!!

Link to comment
Share on other sites

This one Smarty statement should do the same as the {php} attempt:

{if !empty($smarty.get.subscribed)}<p class="confirmation">Thank you for subscribing</p>{/if}

 

See: http://www.smarty.net/docs/en/language.variables.smarty.tpl#language.variables.smarty.request

 

Knackering the admin area... maybe. But there is a separate controller file for the admin.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...