Jump to content

Module: Twig for the TemplateEngineFactory


Wanze
 Share

Recommended Posts

TemplateEngineTwig

This module adds Twig as engine to the TemplateEngineFactory.

Screenshot of the available configuration options:

post-582-0-86298800-1404421957_thumb.png

Project on GitHub: https://github.com/wanze/TemplateEngineTwig

Project in modules directory: http://modules.processwire.com/modules/template-engine-twig/

Only Twig related things should be discussed in this thread.

For common problems/features/questions about the Factory, use the TemplateEngineFactory thread.

  • Like 8
Link to comment
Share on other sites

  • 2 years later...

Hi @Wanze,

Many thanks for providing this fantastic module!

I have just installed the module but immediately hit a problem that is not of your making in any way, but a code change in TemplateEngineTwig.module would make my life very much easier and possibly other future users of the module! :)

Basically, I already use Twig for some TemplateFile rendering (Field level templates) using my own custom module. I've decided to use your Controller/View approach for my regular ProcessWire templates but when trying to install your module, I get the following error:

"Cannot redeclare class Twig_Autoloader".

Obviously, this is because my module has already loaded this class and TemplateEngineTwig.module is trying to do the same thing again. The following code change at the top of TemplateEngineTwig.module will not hurt anyone else, but will help me enormously. If you could modify the module to make this check, I'd really appreciate it!

if (!class_exists('Twig_Autoloader')) {
   require_once(__DIR__ . '/vendor/twig/twig/lib/Twig/Autoloader.php');
}

Many thanks indeed :)

  • Like 1
Link to comment
Share on other sites

Hi Gazley,

Sorry I missed your post. Thanks for the suggestion, I'll add this to the next version so you are safe when you upgrade the module!

Edit: Just saw that your post was pretty new haha :lol:

Cheers

  • Like 2
Link to comment
Share on other sites

Hello,

I'm trying out a template engine conversion of a website. In a foo.twig page I'm calling a isValidUser function, which I required in a foo.php template page. But the engine returns the function in unknown. How do I include functions in a twig template?

Thanks!

Link to comment
Share on other sites

Hi Manaus, which Template Engine Module do you use? Template Engine Factory or Template Twig Replace, or something different?

Please post the code of the twig file here. Please give more information about your setup (ProcessWire Version, etc).

Normally you would call a function in twig with 

{% myFunc(params) %}

 

Link to comment
Share on other sites

Thanks jmartsch,

I'm using Template Engine Fatory, with 2.7.
I think the problem resides in passing values to the twig templates. If I do

$login = $pages->get('/login/');

And then in the twig template:

<p>{{ login.url }}</p>

I don't see any value. How do I pass values?

Thanks!

Link to comment
Share on other sites

So you found the answer yourself. Yes, you need to assign your variables to the view variable like this:

$view->set('login', $login);

All processwire API variables (page, input, config etc.) are available (per setting in Template Engine Twig). So you can use

{{ page.title }}

directly.

Link to comment
Share on other sites

Hi @Manaus,

If you want to call your isValidUser function in your Twig template, you'll have to extend Twig by hooking the ___initTwig method in TemplateEngineTwig.module. Assuming, I've called the hook method 'hookInitTwig', you'd do something like below (not tested!!!):

 
public function hookInitTwig(HookEvent $event)
{

    $twig = $event->arguments('twig');

    $function = new \Twig_SimpleFunction('isValidUser', function (...) {
        
        // Assuming isValidUser function is in scope ...

        return isValidUser(...);

    });

    $twig->addFunction($function);

}

Obviously, if you don't want to add the function directly into Twig, you can evaluate your isValidUser in the ProcessWire template (Controller) and assign its result to a variable that can be referenced inside the Twig template (view).

Link to comment
Share on other sites

Hi @Gazley,

yes -- or I think so --: I have added these lines on top of the calling php page:

$twig = new Twig_Environment($loader, array(
    'debug' => true,
));
$twig->addExtension(new Twig_Extension_Debug());

But still {{ dump(var) }}  is not recognized...

Link to comment
Share on other sites

You would have to add that code in the ___initTwig method in the Twig module. The $twig variable you create in the calling page is not connected to the template engine in any way - it's just a random, isolated variable. See my earlier post for and idea of where/how this is done: 

 

Link to comment
Share on other sites

I tweaked the function like this:

protected function ___initTwig(Twig_Environment $twig)
{
   $loader = new Twig_Loader_Filesystem($this->getTemplatesPath()); // unless 'loader' unknown error
   $this->twig = new Twig_Environment($loader, array(
       'debug' => true,
   ));
}

But I get a unknown "dump" function.
I see there is also a initEngine function having

'debug' => $this->wire('config')->debug,

So if debug is set to true, it should work...

Thanks

Link to comment
Share on other sites

Yep, it seems that the module configures 'debug' (and therefore "dump") based on the value on config.php so, no additional setup required.

You can remove the code from ___initTwig for the above reason. However, you do not add code directly to ___initTwig - you just hook ___initTwig and the code is elsewhere (see ProcessWire hooks).

 

Next obvious question, do you have 'debug' set to true in your site/config.php file? If not, set it to true.

Link to comment
Share on other sites

I added

wire()->addHook("TemplateEngineTwig::___initTwig", function($event) {
	$twig = new Twig_Environment($loader, array(
	    'debug' => true,
	));
	$twig->addExtension(new Twig_Extension_Debug());
});

to a _init.php file I prepend to pages, but alas, dump is still unknown....

Yes $config->debug is set to true in the config file...

Link to comment
Share on other sites

You don't need the above code in the hook because, like I said earlier, it seems that the Twig module is already doing this. However, despite not needing the hook code, you have not implemented it correctly as you would need to access the $twig variable from the $event parameter.

$twig = $event->arguments('twig');

Either way, remove your hook code from your _init.php file.

If $config->debus is true, then I'm all out of ideas on this. Sorry.

Link to comment
Share on other sites

Just one other thing, have you actually tried using var_dump in PHP on the value you are trying to dump?

It may be that var_dump is outputting nothing natively and if so, the Twig dump function (being just a wrapper around var_dump) will likewise output nothing.

Link to comment
Share on other sites

Just a heads-up, I've just tried to use the dump function in my own code and get:

Exception: Unknown "dump" function in "_header.html.twig" at line 31. 

Looks like I'll have to check this further!!! :) 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...