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

Smarty 3.12 and Source Guardian

 
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
kepitingmerah
Smarty n00b


Joined: 06 Nov 2014
Posts: 2

PostPosted: Thu Nov 06, 2014 6:57 am    Post subject: Smarty 3.12 and Source Guardian Reply with quote

Hi

I'm having difficulty using smarty 3 and source guardian,

so far i've changed
File Name : smarty_internal_write_file.php

if (!file_put_contents($_tmp_file, $_contents)) {
error_reporting($_error_reporting);
throw new SmartyException("unable to write file {$_tmp_file}");
}

to

if (function_exists('sg_load_file')) {
sg_encode_file($_tmp_file, $_contents);
} else {
if (!file_put_contents($_tmp_file, $_contents)) {
error_reporting($_error_reporting);
throw new SmartyException("unable to write file {$_tmp_file}");
}
}

and it is able to produced encoded compiled template (xxx.tpl.php)

my problem is regarding reading from the compiled template :
i've changed :

Filename : smarty_internal_resource_file.php
if ($source->timestamp) {
return file_get_contents($source->filepath);
}

to
if ($source->timestamp) {
if (function_exists('sg_load_file')) {
return sg_load_file($source->filepath);
}
return file_get_contents($source->filepath);
}


and in Filename : smarty_internal_templatebase.php
$code = file_get_contents($_template->compiled->filepath);

to :
if (function_exists('sg_load_code')) {
$code = sg_load_file($_template->compiled->filepath);
} else {
$code = file_get_contents($_template->compiled->filepath);
}

and
include($_template->compiled->filepath);

to
if (function_exists('sg_load_code')) {
sg_eval(sg_load_file($_template->compiled->filepath));
} else {
include($_template->compiled->filepath);
}

but it always gives me this error :
Notice: Undefined variable: _smarty_tpl in C:\Windows\Temp\php142 on line

Anyone can direct me to the right direction?

Thanks
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1785

PostPosted: Fri Nov 21, 2014 12:26 am    Post subject: Reply with quote

Please learn to use code blocks in your forum posts.
Also, you might find http://www.smarty.net/docs/en/plugins.prefilters.postfilters.tpl useful… (Read: you're doing it wrong.)
Back to top
View user's profile Send private message
kepitingmerah
Smarty n00b


Joined: 06 Nov 2014
Posts: 2

PostPosted: Sat Nov 22, 2014 1:36 am    Post subject: Reply with quote

I've tried using

Code:

function smarty_prefilter($source, Smarty_Internal_Template $template)
{
    if (function_exists('sg_load_file')) {
        return sg_decode_string($source);
    } else {
        return $source;
    }
}

function smarty_postfilter($compiled, Smarty_Internal_Template $template)
{
    if (function_exists('sg_load_file')) {
        $compiled = sg_encode_string($compiled);
    }
    return $compiled;
}
$smarty->registerFilter('pre', 'smarty_prefilter');
$smarty->registerFilter('post', 'smarty_postfilter');


and i get Fatal error: in D:\apache\test\libs\smarty3\sysplugins\smarty_internal_templatecompilerbase.php on line 1
Back to top
View user's profile Send private message
U.Tews
Administrator


Joined: 22 Nov 2006
Posts: 5068
Location: Hamburg / Germany

PostPosted: Sat Nov 22, 2014 12:46 pm    Post subject: Reply with quote

Pre- and postfilter is the wrong place to do it.

The prefilter runs on the template source code before it gets passed to the compiler.
The postfilter does run on the compiled code. But it's output must be executable PHP code so you can't run sg_encode_string there.

Your problem is the replacement of include($_template->compiled->filepath); in smarty_internal_templatebase.

The compiled template file contains a piece of code which is executed when it get included. It does need the local variable $_smarty_tpl, but this is not visable inside sg_eval.

Solution:
Code:
if (function_exists('sg_load_code')) {
      $code = sg_load_file($_template->compiled->filepath);
      eval("?>" . $code);
     unset($code);
} else {
     include($_template->compiled->filepath);
}


Note that you are loosing a lot of performance. Normally there is a good chance that the included compiled template is executed out of a PHP cache like APC or others.
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