Jump to content

Template Twig Replace


marco
 Share

Recommended Posts

I am converting a current project to twig templates (with data providers of course :D)

An having the following issue when calling properties on some PW objects like page:

in PHP

$page->template->name

in Twig, that does not work:

page.template.name

throws:

Fatal error: Exception: An exception has been thrown during the rendering of a template ("Method Page::template does not exist or is not callable in this context")

This however works:

page.get('template').get('name')

I guess this has something to do with the fact that those properties are added dynamically (internally stored in the data array/object, and set/get via magic methods)

How to fix this?

Link to comment
Share on other sites

I am converting a current project to twig templates (with data providers of course  :D)

Yay: Glad to hear people using this! :)

Sorry for the late reply: Just saw that you've posted - I follow this thread now to get notifications on updates.

You're right. Some propertys don't load automatically and you need to "get" them the way you've stated.

There currently isn't a "fix" for this as we didn't classified this behaviour as an error.

Link to comment
Share on other sites

  • 6 months later...

An having the following issue when calling properties on some PW objects like page:

in PHP

$page->template->name

in Twig, that does not work:

page.template.name

throws:

Fatal error: Exception: An exception has been thrown during the rendering of a template ("Method Page::template does not exist or is not callable in this context")

I'm also facing this (I'm new to Twig in ProcessWire). Of course setting each variable first in the "controller" file, like $view->template, $view->template->name (eg. in "services.php" for services.twig) fixes this, but this overly complicates things (and surely there's a better solution).

It's the second time I'm trying my head around Twig but I still can't see the benefits over Latte :)

Link to comment
Share on other sites

Hi tpr

which version of processwire are you using? This should be fixed in 2.6.

Are you using TemplateDataProviders, also?

The main reasons we are using twig are:

  • Easier to understand for "non techies" (or people coming from other systems like symfony etc.)
  • It's ability to do "recursive" template overwrites via blocks/extend is just plain awesome (if you're interested i can post some examples how we structure our templates)
  • Seperation of concerns: Trying to keep "Business logic" (Getting/Setting Data) away from the views which makes them a lot easier to read and maintain. As a rule of thumb we're only doing basic control structures (if/else) and loops inside our views. Everything else is kept outside of the view, inside the controllers.
  • Syntax hiliting is much better if you get rid of <?php echo '<li><a href="' . $xyz .'... inside your templates :)
Link to comment
Share on other sites

I see now how Twig Replace can be used with TemplateDataProviders, thanks!

Actually I was using the module TemplateEngineFactory (with PW 2.6) where I got that error, using Twig Replace it is gone.

And thanks for the Twig benefits explanation, that's exactly why I'm using Latte :)

What I was try to tell is that I see no real benefits of Twig over Latte. Even with TemplateDataProviders it sounds less convenient (for me, at least). There are some hand html element macros like "n:if", "n:class", "n:inner-foreach", etc, and even PHP can be used in .latte files (if you don't get separation of concerns too strict).

Link to comment
Share on other sites

  • 8 months later...

I'm just trying this out for the first time and wondering how to do the following things in Twig.

$modules->isInstalled("AdminLinksInFrontend") ? $modules->get("AdminLinksInFrontend")->render() : "";
<?php echo AIOM::CSS('../../assets/css/style.css'); ?>

Also, when trying to do

<script>{% include "../../assets/js/fonts.min.js" %}</script>

I get

Error: Exception: Looks like you try to load a template outside configured directories (../../assets/js/fonts.min.js) in "_layout.php" at line 23. (in /site/modules/processwire-template-twig-replace/Twig-1.18.0/Twig/Loader/Filesystem.php line 235)

Is there a place to configure directories? Or another way to allow directory traversal?

Link to comment
Share on other sites

  • 1 month later...

Hi Felix, other Twig users, I realised I posted the below in the wrong thread previously:

Would it be possible (in theory at least) to use PW's new Page() functionality whilst still using the Twig module?

 

I've had a fair bit of joy with most of it so far 

{{ session.redirect('/about') }}

 for example works great. This was leading me to hope I may be able to use the full power of PW still whilst keeping things clean with Twig.

 

Has anyone managed to do this successfully or would I have to revert to pure PHP to carry out certain PW tasks?

 

Thanks!

Link to comment
Share on other sites

  • 3 years later...

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

×
×
  • Create New...