Smarty?

35 views
Skip to first unread message

DK

unread,
Oct 7, 2011, 1:26:02 AM10/7/11
to allo...@googlegroups.com
Hello,

I spent this evening reading through your docs and code, and I really like the patterns.  I have a large enterprise project coming up that has a requirement for using Smarty templating engine.  What would be the best way to integrate Smarty into Alloy and do see any major draw backs? 

In addition, for this project, it might also be a benefit to have all the Smarty template files (views) in their own directory structure.  I realize this separates the excellent 'modular' design of Alloy, but it could be a problem having designers sifting through folders of php files to work on their presentation layer.  Is there a possible solution for this?

Finally, I like to have folder outside of the application folder hierarchy for storing files and resources that are not to be part of the code repository, but are used by the application (like generated feeds, cache, uploads, etc).  Do you have a recommendation for the best way to give Alloy access to this folder in an integrated fashion?

Thank you so much for your time.  Alloy looks like a fabulous starting point with just the flexibility and light weight I was looking for.  Nice job!

DK

unread,
Oct 15, 2011, 9:04:30 PM10/15/11
to allo...@googlegroups.com
Ok, well here is what I have come up with:

Create a Plugin in the apps dir as:
app/Plugin/Smarty
app/Plugin/Smarty/lib

Drop Smarty into the new lib folder just created

Create the following Plugin.php and place it in the new Smarty folder just created

<?php
namespace Plugin\Smarty;
use Alloy;

/**
 * Smarty Plugin
 * Adds the Smarty templating system
 *
 * @author DK
 * @since 2011-10-15
 */
class Plugin
{
    public function __construct(Alloy\Kernel $kernel)
    {
        #Not using Alloy's loader due to incompatibility with Smarty class
        #naming and file structure.  Smarty uses it's own loader from here on
        require('lib/Smarty-3.1.3/Smarty.class.php');
       
        #Instantiate it and set some config values
        $smarty = new \Smarty();
        $dPath = $kernel->config('app.path.data'); #using custom data folder for files that should not be in the respository
        $smarty->setCompileDir($dPath . 'smarty_templates_c');
        $smarty->setCacheDir($dPath . 'smarty_cache');
        $smarty->setTemplateDir(dirname(__FILE__) . '/templates');
        $smarty->setConfigDir(dirname(__FILE__) . '/configs');
       
        #Add it to the kernal
        if(!isset($kernel->smarty))
            $kernel->smarty = $smarty;
       
    }
}


Had trouble getting the Alloy loader to play nice (as noted) and feel like there is a smarter way to add it to the kernel (tried addMethod and setInstance but had issues), so any comments would be much appreciated.

Note that you can use $kernel->smarty->fetch instead of $kernel->smarty->display if you want to return Smarty's results back through Alloy.

Vance Lucas

unread,
Oct 18, 2011, 11:19:05 AM10/18/11
to Alloy
Sorry for the long delay in posting a reply - I have been extremely
busy lately with work.

A plugin is exactly the way to do it. The only difference from what
you have done is you should use the "addMethod" Kernel method with a
callback instead of setting a new property on the Kernel.

So instead of this:
--

#Add it to the kernal
if(!isset($kernel->smarty))
$kernel->smarty = $smarty;

Do this:
--

$kernel->addMethod('smarty', function() use($smarty) {
return $smarty;
});


Then call it where you want to use it like this:

return $kernel->smarty()->fetch('sometemplate.tpl');


--
Vance Lucas


On Oct 15, 8:04 pm, DK <uncledproducti...@gmail.com> wrote:
> Ok, well here is what I have come up with:
>
> *Create a Plugin in the apps dir as:*
> app/Plugin/Smarty
> app/Plugin/Smarty/lib
>
> *Drop Smarty into the new lib folder just created*
>
> *Create the following Plugin.php and place it in the new Smarty folder just
> created*

DK

unread,
Oct 19, 2011, 8:15:57 AM10/19/11
to Alloy
Vance,

Thanks so much, works like a charm. Not totally clear how the
'use($smarty)' works, but I'm sure it will sink in soon. I appreciate
the guidance. I have another question when you get a chance, which I
have started a new thread for.

Vance Lucas

unread,
Oct 19, 2011, 10:35:07 AM10/19/11
to allo...@googlegroups.com
The "use" keyword in a PHP closure imports variables from the current local scope for use inside the closure. It is similar in effect to creating a function and declaring a variable "global" inside it, only it is much more safe and acceptable in a closure because the variable will maintain its last current state.

I found an article that explains this a bit more and runs some tests as well:
http://www.toosweettobesour.com/2008/07/21/php-53-and-closures/

Reply all
Reply to author
Forward
0 new messages