Smarty Forum Index Smarty
WARNING: All discussion is moving to https://reddit.com/r/smarty, please go there! This forum will be closing soon.

php include in smarty

 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
mark2kooki
Smarty n00b


Joined: 07 Nov 2012
Posts: 1

PostPosted: Wed Nov 07, 2012 1:05 pm    Post subject: php include in smarty Reply with quote

Hi all

we use a cms called newscoop - very good but im struggling with what i thought would be a simple emaill form with recaptcha

i have the form sorted in PHP in a folder called contact_form

now in the tpl file i used to use - <?php include('contact_form/form.php'); ?>

obviously cannot do that now

i have read on here about creating a plug in which i tried but cannot get it to show at all

file function.emailplugin.php

<?php
function smarty_function_emailplugin(array $params, Smarty_Template_Instance) {
include 'contact_form/form.php';
}
?>

and in the template have {emailplugin}

any ideas what i am doing wrong
Back to top
View user's profile Send private message
cbj4074
Smarty Regular


Joined: 10 Nov 2011
Posts: 49

PostPosted: Mon Nov 12, 2012 8:48 pm    Post subject: Reply with quote

I would take a completely different approach. You are defeating Smarty's fundamental purpose when you include a PHP file from within a template.

The first step to take is to move all of your markup (HTML) from form.php into the Smarty template file. form.php should contain your PHP logic only.

Because I'm a nice guy, I've taken the time to put together a brief example of the PHP logic file (this uses re-CAPTCHA):

Code:

<?php

//This script assumes that the Smarty library is already loaded and that an instance
//of the Smarty object is assigned to $smarty.

session_start();

if (!isset($_SESSION['misc']['captchaPassed']) || $_SESSION['misc']['captchaPassed'] !== TRUE) {
   require('recaptcha-php-1.11/recaptchalib.php');
   
   $captchaHtml = recaptcha_get_html(RECAPTCHA_PUBLIC_KEY, NULL, TRUE);
   
   $smarty->assign('captchaHtml', $captchaHtml);
}

if (!empty($_POST) {
   if (!isset($_SESSION['misc']['captchaPassed']) || $_SESSION['misc']['captchaPassed'] !== TRUE) {
      if (isset($_POST['recaptcha_challenge_field']) && isset($_POST['recaptcha_response_field'])) {
         $resp = recaptcha_check_answer(RECAPTCHA_PRIVATE_KEY, $_SERVER['REMOTE_ADDR'], $_POST['recaptcha_challenge_field'], $_POST['recaptcha_response_field']);
         
         if (!$resp->is_valid) {
            $areErrors = TRUE;
            $errorCaptcha = 'Incorrect letters entered. Please try this new puzzle.';
            $app->smarty->assign('errorCaptcha', $errorCaptcha);
         }
         else {
            //Set this persistant flag so that users aren't asked
            //to complete the CAPTCHA more than once per browser session.
            
            $_SESSION['misc']['captchaPassed'] = TRUE;
            
            //This is necessary to prevent the CAPTCHA from being displayed
            //when it was solved successfully but other validation errors
            //occurred.
            
            unset($app->smarty->tpl_vars['captchaHtml']);
         }
      }
   }
}

$smarty->display('form.tpl');


Then, the template file (form.tpl) might look something like this:

Code:

<form name="create-account" method="post" action="{$smarty.server.REQUEST_URI}">

{if !empty({$captchaHtml})}
   <div>
      <script type="text/javascript">
         var RecaptchaOptions = {
            theme : 'white'
         };
      </script>
      {if !empty($errorCaptcha)}<div class="formError">{$errorCaptcha}</div>{/if}
      <div>{$captchaHtml}</div>
   </div>
{/if}

<input name="btnSubmit" type="submit" value="Create Account" />

</form>
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP