Jump to content

Module: TemplateEngineFactory


Wanze

Recommended Posts

  • 2 weeks later...
  • 4 months later...

Hi there.

Not sure if my problem is specific to the twig engine or to this factory module.

If I enable automatic rendering, I have no issues, however if I turn it off (and call the render function as follows), I can't use any '%' character in the template or in Processwire fields.

The render function is called like this:

Quote

 

$factory = $modules->get('TemplateEngineFactory');

printf($factory->render($page->template->name, $twigVars));

 

$twigVars is just an array I'm passing it (and tested working fine).

I have tried using Twig with raw php (no processwire) and the templates render fine (obviously without ProcessWire functionality), so I believe the $factory->render function may be trying to interpret the '%' character in some way.

The % character is fine to use within twig tags (including using {% and %}), but anywhere else the method just returns null.

I tried replacing these lines with calls to twig directly, which rendered the template fine, however it is unfortunately not a workaround for me as my templates are making calls to other page render functions (this is why I can't use auto render) and those are not getting the php template files called correctly as twig obviously doesn't know about this.

Link to comment
Share on other sites

  • 3 weeks later...

Hey @Wanze. I am wondering if I could request an additional empty "config" array in the render method. This would let TemplateEngine modules do some more logic their end, eg override module config settings.

The reason for this is for whatever reason I seem to find myself wanting to render template partials without worrying about or needing the global data object (just explicitly pass params), however I would also like the global data object populated generally. Eg...

<?php $factory->render('partial', ['foo' => 'bar'], ["global_data" => false] ));

 

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...

Hi,

I'm using the excellent Template Engine Factory from @Wanze and Smarty. One thing I can not figure out is how to use pw's `__('…')` translate function within my templates.

This is how I tried:

/site/templates/views/fundus-index.tpl

<a href="{$page->httpUrl}">__("zurück")</a>
<a href="{$page->httpUrl}">{__("zurück")}</a>

I would expect to find `fundus-index.tpl` in translatable files. Unfortunately it does not show up even after refreshing the list (screenshot attached). 
When trying to enter the file directly I get a warning saying: ProcessLanguageTranslator: That file has no translatable phrases

After that I've tried to setup a new php file `/site/translations.php` with content `__("zurück")`. This file shows up and I can successfully translate the string which I would expect that this string is globally available across all files (missing knowledge regarding the usage of translations ;) However my template file does not care about this newly created translation.

So long story short. How can I use translations in smarty templates?

Best regards,

Toni

Bildschirmfoto 2020-04-12 um 22.17.03.png

  • Like 1
Link to comment
Share on other sites

10 hours ago, toni said:

this can be closed (cannot find a close button 😕 time to shut down the computer.)

Hey @toni!

There's no close action on the forum, but I'm going to merge this topic with the TemplateEngineFactory support board — the recommended approach is that if you have questions regarding specific module, you should post them on the support board for that module. You can find the correct board via the modules directory (there's a "support board" link in the sidebar for each module) 🙂

Link to comment
Share on other sites

  • 3 weeks later...
On 4/13/2020 at 8:57 AM, teppo said:

Hey @toni!

There's no close action on the forum, but I'm going to merge this topic with the TemplateEngineFactory support board — the recommended approach is that if you have questions regarding specific module, you should post them on the support board for that module. You can find the correct board via the modules directory (there's a "support board" link in the sidebar for each module) 🙂

Nice! Thank you @teppo

Link to comment
Share on other sites

  • 2 months later...
  • 1 month later...

Having a rough go with performance and figuring out how to diagnose issues. I noticed that as I was building out a template that the load times on my local dev environment continued to get slower and slower. Basically slower than browsing the internet. I recently picked up the Pro DevTools modules and started working from the Profiler and the results have been pretty bad. I'm using the Twig Template Engine.

It is taking ~1.5 seconds for the server to render the page before returning anything to the browser. I started to work backwards through my code and I cannot find where there would be issues that should be causing this much of a slowdown, I wasn't able to get speed differences by disabling modules and I don't think that I'm running anything that would conflict.

I went through my templates and partials and started commenting out loops. I have a few loops on the home page to render picture grids, a blog feed, and navigation in both the footer and the header. There aren't any calls to modules or special functions that are happening inside these loops. When studying the output of Profiler there's no indication that anything is booting up during the page rendering process either. By commenting out all of the loops it dropped the render time from ~1.5s to .3-.5s, which is still slow for ProcessWire. I've attached two screenshots of the Profiler output for comparison. Everything below the cutoff in the picture had zero execution time. I'm not experiencing any slowdown on other local sites.

Appreciate any help!

Currently running:
ProcessWire 3.0.164 Master
TemplateEngineFactory v2
TemplateEngineTwig v3
PHP 7.3.13

 

code_with_loops.png

code_without_loops.png

Link to comment
Share on other sites

  • 2 months later...

I'm trying to change template on the fly (when there is a urlSegment in homepage) and I'm almost there (I guess), the behaviour is strange, the markup is working but if I print the template name I get the home page, something is not working properly
The code is inside a custom module. Thanks for the help. I'm using v1 (can't use v2)
 

public function ready() {   
	$this->addHookBefore('Page::render', $this, 'hookProfiles');
}
protected function hookProfiles($event){
        $page = $event->object;
       
        if($page->template == 'home' && wire('input')->urlSegment1 != ''){
            $page->template->set("filename","/site/templates/author.php");
            $factory = $this->modules->get('TemplateEngineFactory');
            $theme   = $factory->load('author.tpl');
            wire()->set('view', $theme);
        }
        
}
Link to comment
Share on other sites

  • 3 weeks later...

Hi! Does anyone know how to handle throwing a Wire404Exception so that PW renders the 404 page as it normally does?

Getting:

Fatal Error: Uncaught Wire404Exception

I am using the Smarty Engine. Both the engine factory and smarty engines are on their latest versions. 

I am using url segments to change the url structure of the site I'm working on.

Doing something like this:

if($input->urlSegment1) {
   $pageOnSegment = getPageToRender(); 
   $view->set('page', $page);
} else{
   throw new Wire404Exception();
}

 

Link to comment
Share on other sites

Hi elabx,

On one of my sites, I have:

if ($input->urlSegment1){
  // unknown URL segment, send a 404
  throw new Wire404Exception();
}

and in another place I was using:

// Don't let search engines index non-existing pages
// see https://processwire.com/blog/posts/processwire-2.6.18-updates-pagination-and-seo/
// if(!count($paginated_calendar_postings) && $input->pageNum > 1) {
if(!count($paginated_calendar_postings) && $input->pageNum > 1) {

  // I'm not sure why this line is needed, but it is
  // $session->redirect($pages->get($config->http404PageID)->httpurl());
  throw new Wire404Exception();
}

I think I used to have to do this, before a bug was fixed in this module?

$session->redirect($pages->get($config->http404PageID)->httpurl());

Hope that helps

Link to comment
Share on other sites

Hi @gmclelland! I did found the issues you came across, but didn't solve them and also the redirect shouldn't happen before the 404 😞

I read on this github issue that this shouldn't happen anymore in version 2 of the module so I went for updating everything but didn't work either still can't throw the exception on the controller. 

Link to comment
Share on other sites

Hmm... I'm not sure then.  Are you using TemplateEngineFactory with TemplateEngineTwig?  Did you upgrade TemplateEngineTwig as well?  Did you upgrade your Twig libraries with composer?  Did you clear your site caches with Modules > Refresh?  Just some thoughts...

If I remember right..after upgrading to 2.x

throw new Wire404Exception();

most of the time it would work, but one time it didn't.  I just can't remember the use case.  So maybe that bug isn't fully fixed?  Perhaps you can provide Wanze your code so he can replicate and fix the problem?

Link to comment
Share on other sites

  • 4 months later...

I am looking for an output strategy for repater fields or Repater Matrix fields (Pro fields) in my case.

I would like to replicate the approach Ryan described in the blog post https://processwire.com/blog/posts/more-repeaters-repeater-matrix-and-new-field-rendering/ , but using Template Engine Factory (because I want to use Smarty or another template language). 

image.thumb.png.9a57858e1d4ae2601355614fd8a23b02.png  

In short: Instead of a foreach loop that then uses many if clauses to distinguish the type, I would like to have a separate controller and template for each repeater matrix item.
The controller should be used to modify the output before.

Here is my working code, but I wonder if it might be better to use hooks like Template Engine Factory's hookBeforePageRender, or something else?

//_init.php
$factory = $this->modules->get('TemplateEngineFactory');
// check if the actual page has a modules_repeater (Repeater Matrix field)
// and provide the output of the different modules as a variable
if ($this->page->hasField('modules_repeater')) {
  // now get the output based on the type
  foreach ($this->page->modules_repeater as $module) {
    // Get the controller, to render a repeater item.
    $controller = $factory->controller("modules_controllers/{$module->type}.php", "modules/{$module->type}.tpl");
    // You might pass some data via to the controller file, which you find at site/templates/modules_controller/nameOfTheRepeaterMatrixItem.
    // We assign the module repeater item to the $page variable so we can use it in our template and controller like this
    // $page->hero_headline
    $controller->page = $module;
    $controller->execute();
    $output = $factory->render("modules/{$module->type}", ['page' => $module]);
    // Executing the controller renders the associated template via template engine.
    $this->view->set('modules_repeater', $output);
  }
}

Any suggestions?

  • Like 1
Link to comment
Share on other sites

  • 2 months later...

HELLO!

Trying to use Pug templating engine and have downloaded and installed both TemplateEngineBase and pug module, however pug itself seems to be missing as the module throughs an error saying Phug isn't found. How should I install pug itself??

Link to comment
Share on other sites

  • 9 months later...

It looks like there's a compatibility issue between modules TemplateEngineFactory and TemplateEngineTwig in current PW master and dev versions.

I use the "Add Module From Directory" form:

* With TemplateEngineFactory and it installs version 1.1.3 (no update available)

* With TemplateEngineTwig, it chooses version 1.1.0 and reports a compatibility issue:

Quote

Requires TemplateEngineFactory 1.1.3 >= 2.0.0 

So I can't install it.

 

I'll try to install it from the sources but I did want to report that issue.

Edited by da²
  • Like 1
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
×
×
  • Create New...