Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.
Dedicated server web hosting provided by Guru-host.eu.
Internal_Compile required or not?

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
emt
Smarty Regular


Joined: 10 Dec 2009
Posts: 41

PostPosted: Wed May 02, 2012 12:04 pm    Post subject: Internal_Compile required or not? Reply with quote

I need to implement functionality that COMPILES into the template, with tags like this:

Code:
{useraccess module="admin"}
This is Content user has access to!
{useraccesselse}
User sees this when he does NOT have access.
{/useraccess}

I have this working perfectly as I have created a class like this:

Code:
class Smarty_Internal_Compile_Useraccess extends Smarty_Internal_CompileBase { ... }


Reading the documentation I could not see how to create this with a compiler plugin as I needed the {useraccesselse} part.

So the problem is how I load the class when it's needed for the compilation of the template.

I do NOT want to store it in the sysplugins directory. First of all, it's messy when my code gets mixed in with an external library. Secondly, the Smarty directory is an external SVN in my repository so I cannot add the file into my own SVN.

My inefficient way of doing it is to use an autoloader which checkes whether the class 'Smarty_Internal_TemplateCompilerBase' is being loaded. If so, I load my class as well so it's available if the tag exists in the template. Very inefficient.

My questions are:

1) Am I implementing my functionality in the correct way by extending the Smarty_Internal_CompileBase?

2) If not, what is the recommended way of extending the compiler?

3) From your knowledge of Smarty internals, is there a better way to load my class for template compilation?

Thaks for any help.
Back to top
View user's profile Send private message
rodneyrehm
Administrator


Joined: 30 Mar 2007
Posts: 698
Location: Germany, border to Switzerland

PostPosted: Wed May 02, 2012 12:19 pm    Post subject: Re: Internal_Compile required or not? Reply with quote

emt wrote:
3) From your knowledge of Smarty internals, is there a better way to load my class for template compilation?


you can add several plugin directories with $smarty->addPluginsDir('/path/to/your/plugins'). This allows smarty to do auto-loading and enables you to separate your plugins from smarty's core-plugins. (This is the preferred way, anyaways…)
_________________
Twitter
Back to top
View user's profile Send private message Visit poster's website
emt
Smarty Regular


Joined: 10 Dec 2009
Posts: 41

PostPosted: Wed May 02, 2012 12:44 pm    Post subject: Re: Internal_Compile required or not? Reply with quote

rodneyrehm wrote:
you can add several plugin directories with $smarty->addPluginsDir('/path/to/your/plugins').


Problem is, this only works for plugins during template execution. It does not work at compile time.

At least, when my class is stored in the plugins directory it doesn't get loaded at compile time.
Back to top
View user's profile Send private message
U.Tews
Administrator


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

PostPosted: Wed May 02, 2012 2:53 pm    Post subject: Reply with quote

1. Remove the _Internal_ from your class name
Code:
class Smarty_Compile_Useraccess extends Smarty_Internal_CompileBase { ... }


2. Store it as file compiler.useraccess.php in your plugins folder.
Back to top
View user's profile Send private message
emt
Smarty Regular


Joined: 10 Dec 2009
Posts: 41

PostPosted: Wed May 02, 2012 3:08 pm    Post subject: Reply with quote

Tried that - the message I got was:

Code:
Fatal error: Uncaught exception 'SmartyException' with message 'Plugin "useraccess" not callable' in J:\wamp\www\internal\myframework\protected\class\smarty31\sysplugins\smarty_internal_templatecompilerbase.php on line 330


HOWEVER, I just tried to change my class name to use "compiler" instead of "compile", and it worked!

Code:
class Smarty_Compiler_Useraccess extends Smarty_Internal_CompileBase { ... }


So thanks for putting me on the right track, Uwe!
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    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