Wanze

Module: TemplateEngineFactory

Recommended Posts

TemplateEngineFactory

The main idea of this module is to support the developer separating logic from markup.

This is achieved by turning ProcessWire templates into controllers which interact over a new API variable to template engines like Smarty or Twig. The TemplateEngineFactory ships with a default engine "ProcessWire" that uses the internal TemplateFile class to render the templates (some of you may already be familiar with this concept). However, the module is constructed in a way that any template engine can be used, implemented as separate modules. :)

Please check out the readme on GitHub for more details how it works: https://github.com/wanze/TemplateEngineFactory

... or in the modules directory: http://modules.processwire.com/modules/template-engine-factory/

post-582-0-15633700-1404419674_thumb.png

How does it work?

A controller (aka ProcessWire template) can have an associated template file which contains the markup to render. The folder where those templates are stored is configurable for each installed engine. If the Factory finds a template file with the same name as the controller, an instance to access the template is provided with a new API variable (called "view" by default). Over this API variable, you can set the dynamic variables that should be rendered. Hopefully the following example makes things clearer:

// In controller file: /site/templates/home.php

if ($input->post->form) {
  // Do some processing, send mail, save data...
  $session->redirect('./');
}
// Pass variable to the template
$view->set('foo', 'bar');
$view->set('show_nav', true);
$view->set('nav_pages', $pages->get('/')->children());

As you can see, there is no markup echoed out. The corresponding template file is responsible for this task:

// In template file: /site/templates/view/home.php

<h1><?= $page->title ?></h1>
<p>Foo: <?= $foo ?></p>

<?php if ($show_nav): ?>
  <ul>
  <?php foreach ($nav_pages as $p): ?>
    <li><a href="<?= $p->url ?>"><?= $p->title ?></a></li>
  <?php endforeach; ?>
  </ul>
<?php endif; ?>

In the example above, "ProcessWire" is used as engine. If Smarty is the active template engine, the corresponding template file could look like this:

// In template file: /site/templates/smarty/home.tpl

<h1>{$page->title}</h1>
<p>Foo: {$foo}</p>

{if $show_nav}
  <ul>
  {foreach $nav_pages as $p}
    <li><a href="{$p->url}">{$p->title}</a></li>
  {/foreach}
  </ul>
{/if}

Note that the API variable acts as a gateway which connects you to the activated template engine. We can switch the engine behind without changing the controller logic! (I know that this is probably not a very common need, but it's a cool thing anyway :P)

For further information, please check out the readmes on GitHub. Please ask questions if anything makes no sense - sometimes it's hard to get my explanations ;)

Cheers

  • Like 19

Share this post


Link to post
Share on other sites

Oh, this is perfect timing! Taking this for a test drive as soon as possible. Lately, I was aiming at the exact same thing. But this looks perfectly done with the possibility to plug in your favorite template engine! Thanks, Wanze!

  • Like 1

Share this post


Link to post
Share on other sites

Used templateFile class before to build a site. You module should make it even easier... :)

Is it planned to add a template cache (to cache the "views") in the future? 

Don't know how much time caching would be save at a small / medium site...

I'll try your module soon to build a pocketGrid base layout also with sub templates  :)

Share this post


Link to post
Share on other sites

Hi Wanze,

after a short test...

1) My setup seems not supported

// init html basic template / layout
$tpl = new TemplateFile();
$tpl->filename = $config->paths->templates.'views/html.tpl.php';

// here handle page controller / view stuff
[...]
$pagePart = $pageTpl->render();

$tpl->set('subtpl', $pagePart);

echo $tpl->render();

Because you render and output $pagePart module controlled/ inside I can't use it as sub template :(

Could you make this use possible with TemplateEngineFactory module?

2) Get a 500 error if use chunks / sub templates (for example my html layout view...)

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

$chunk = $factory->load('views/html.php');
$chunk->set('test', 'TEST');
echo $chunk->render();
 

At the moment html.php is simple:

<h1><?php echo $page->title; ?></h1>
<p><?php echo $test; ?></p>

Any idea?

Maybe you can make my setup working with your module?

Share this post


Link to post
Share on other sites

Hi pwFoo,

Thanks for your interest :)

Now you're mixing things bit up that's why it does not work. I assume that you've chosen "ProcessWire" as TemplateEngine?

Then the TemplateFile class is also used to render your templates.

1) My setup seems not supported
Because you render and output $pagePart module controlled/ inside I can't use it as sub template  :(
Could you make this use possible with TemplateEngineFactory module?

 

If you are in your /templates/basic_page.php controller, then the factory has loaded for you already the corresponding template file ine /templates/views/basic_page.php.

Your code loading the html.tpl.php file and echo out it's markup will not have any effect, because the factory will use the /templates/views/basic_page.php file for the output. You'd want to pass the variables to that template file with $view->set();

 

2) Get a 500 error if use chunks / sub templates (for example my html layout view...)

 

In the module's setting of TemplateEngineProcesswire you define where your template files are stored.

When loading a file with the factory, it looks inside that folder. So I guess maybe it wil work so:

// Template files are in /site/templates/views/

$chunk = $factory->load('html.php'); // File is in /site/templates/views/html.php
$chunk2 = $factory->load('chunks/html.php'); // File is in /site/templates/views/chunks/html.php

Now again, if you are in a controller where a corresponding template file exists, echo out will have no effect. You'd need to pass 

the markup to your template:

// In /site/templates/basic_page.php

$view->set('chunk', $chunk->render());

// In /site/templates/views/basic_page.php

<p><?= $chunk ?></p>

Hope I could make things clearer? :)

Cheers

Share this post


Link to post
Share on other sites

Hi wanze, 

I don't think I'll use the wrong path to the template / chunk, but double check ;)

Don't know my planned setup is clear so far...

I would NOT output html.php inside basic_page view..., but try to output basic_page view inside html.php view!  :P

Won't work, I found your hook into render at source code... ;)

html.php

<html>
  <head>
[...]
  </head>
  <body>
    <p><?php echo $basic_page; ?></p>
  </body>
</html>

Hope the simplified example should show what I need ;)

$basic_page is the part of current page done by your module.

Maybe I could cutting my html.php to something like html_start.php and html_end.php to get it around basic_page...

Maybe views should be not .php because it's confusing to have TWO identical files open at editor / IDE.

I'll like to use .tpl or .tpl.php to differ controller and view / template.

Share this post


Link to post
Share on other sites

I think I understand, this is a little different need. You'd have to tell the factory that your main template file is now html.php.

Could you try something like this (not tested!):

// In basic_page.php controller

$factory = $modules->get('TemplateEngineFactory');
$basic_page = $view; // Save basic_page template which is currently behind the $view API variable
$main = $factory->load('html.php'); // Load the main markup
wire()->set('view', $main); // Set the context of wire view variable to be the main template
$view = wire('view'); // Reassign to locally scoped $view variable - not sure if this is necessary, but I think so...
$view->set('basic_page', $basic_page->render()); // Pass output of basic_page to main template

If this should work, you could enable $config->prependTemplateFile an put the code in your prepended controller file, e.g. _init.php.

Maybe views should be not .php because it's confusing to have TWO identical files open at editor / IDE.

I'll like to use .tpl or .tpl.php to differ controller and view / template.

I'll add support for that so that you can define the suffix. Actually this is supported by the Smarty and Twig additions, don't know why I did not add this for the ProcessWire engine ;)

Share this post


Link to post
Share on other sites

So I'll test it soon. Thanks for help :)

Maybe a option for module configuration?

Define a global view to be used as main view?

I'll add support for that so that you can define the suffix. Actually this is supported by the Smarty and Twig additions, don't know why I did not add this for the ProcessWire engine  ;)

Great!  :biggrin:

Share this post


Link to post
Share on other sites

Maybe a option for module configuration?

Define a global view to be used as main view?

I updated the module to version 1.0.1.

All engines now have a setting Global template file where you can enter the filename of a template file that is used as "main template" behind the API variable.

This is useful if you have a main template that contains all the markup and you need to fill certain variables per controller.

Note that Smarty and Twig already support this feature with template inheritance.

Also the TemplateEngineProcesswire module now supports custom suffixes for the template files. Change this setting if you'd like to name the templates different than the controller, e.g "home.tpl.php".

Cheers

  • Like 1

Share this post


Link to post
Share on other sites

Hi Wanze,

global template works with following example.

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

$subtpl = $factory->load('basic-page'); // current page template / view basic-page.tpl
$subtpl->set('subtplvar', $page->body); 

// global template / view
$view->set('title', $page->title);
$view->set('subtpl', $subtpl->render()); // render sub template to global template variable

I think that's how it should be used?

How did it work with smarty / twig? Was it a include inside a template to load a sub template?

If I'm right template engine can't simply switched between Processwire and Smarty/Twig because template variables seems to be handled different?

PW -> load sub template & fill it like your chunk example

Twig/ Smarty -> include sub templates and fill all the template and sub template variables via api variable?

Don't know I'm right or if it's possible. But would be nice to get it filled the same way  :cool:

At the moment I'm only working with pw template class, but maybe I'll could be switch it later... So it would be a great feature have template parser independent code ;)

Share this post


Link to post
Share on other sites

I think that's how it should be used?

Yep, looks fine to me!

Another approach could be to use ProcessWire's prepend template file option. This way, you can populate the variables in your global template and overwrite them in the controllers, if needed.

Let's assume that we have a variable 'content' and 'sidebar' in our global template.

// Populate default values in prepended template file, e.g. _init.php
$view->set('content', $page->body);
$sidebar = $factory->load('chunks/sidebar');
$sidebar->set('a_variable', 'a_value');
$view->set('sidebar', $sidebar->render());

Now in the home controller, the content variable is more complex, we overwrite it:

// In home.php the content variable is pouplatet differently
$home = $factory->load('home');
$home->set('images', $pages->find('template=gallery,sort=-created,limit=3');
$view->set('content', $home->render());

How did it work with smarty / twig? Was it a include inside a template to load a sub template?

If I'm right template engine can't simply switched between Processwire and Smarty/Twig because template variables seems to be handled different?

It works the same, switching the template engine is possible as long as you use the same template filenames (the ending/suffix can be different) and identical variable names. This is possible because behind the API variable is not directly the engine like Smarty or Twig but sort of a 'gateway class' that talks to those engines. Notice that in my examples above I loaded the template files without the suffix, just the filename. Depending on the active filename, the correct template is loaded.

As an example, you could (though this is probably a very rare need) store your templates like this:

  • For the ProcessWire engine: In folder /site/templates/views/
  • For the Smarty engine: In folder /site/templates/smarty/

Now if ProcessWire is the active engine and you load the template 'home', a file in /site/templates/views/home.tpl.php gets loaded (whatever you've configured as template file suffix). If Smarty is active, the file /site/templates/smarty/home.tpl is loaded.

Don't know I'm right or if it's possible. But would be nice to get it filled the same way   :cool:

At the moment I'm only working with pw template class, but maybe I'll could be switch it later... So it would be a great feature have template parser independent code

It works as long as you have your template filenames and variable names consistent :)

However, when switching to a template engine like Smarty or Twig you should check out template inheritance. In my opinion, this is the most powerful feature and one of the main reasons to use a template engine. For smarty, I've written a little section how this could work: 

https://github.com/wanze/TemplateEngineSmarty#best-practices

Cheers

Share this post


Link to post
Share on other sites

Really awesome module, great work!

I just have one question, 

If in one controller I need to render two different templates

how can it be done?

Example home controller

and two themes, one for night other for day.

So the controller check the time and load the corresponding view.

Share this post


Link to post
Share on other sites

Really awesome module, great work!

I just have one question, 

If in one controller I need to render two different templates

how can it be done?

Example home controller

and two themes, one for night other for day.

So the controller check the time and load the corresponding view.

Thanks!

If the HTML markup is the same, maybe you could solve it with loading different CSS files. If markup is different, you'd need to dynamically set the template that is behind the API variable ($view or your custom name).

Something like this, not tested:

$theme_template = isDay() ? 'day' : 'night';
$theme = $factory->load($theme_template); // Load the correct theme
wire()->set('view', $theme); // Set the context of view variable to be the correct theme template
$view = wire('view'); // Reassign to locally scoped $view variable - not sure if this is necessary, but could be

This must happen before you pass any variables to $view. If you need this check on the whole site, you could also use ProcessWire's prependTemplateFile option and put this code in your prepended template file (controller).

Share this post


Link to post
Share on other sites

Wanze, this is a very nice module :) I am playing around with it now

Thanks :)

Today I pushed a little update (v.1.0.2) with some improvments. If you update to this version and already installed/used Smarty or Twig, make sure to update those modules as well.

What's new?

When loading a template file with the factory, you can optionally tell the module to use the given file behind the API variable:

$factory = $modules->get('TemplateEngineFactory');
$view = $factory->load('pw_rocks', true);

Setting the second parameter to true, the template file 'pw_rocks' is now used as active template behind the API variable.

In the example above, the template file is also assigned to '$view' which reflects this change also to the $view variable which is locally scoped in controllers. Whatever template is set behind the $view variable is used by the module when rendering the output of a page.

  • Like 1

Share this post


Link to post
Share on other sites

Hi Wanze,

Any direction how to configure this with $config->ajax?

<?php
//we use the page title as template
$template = $sanitizer->name($page->title);

/* render load body */
$body = $factory->load('custom-templates/' . $template. '.php');

if ($config->ajax) {

$view = $factory->load('custom-templates/_Ajax.php', true);
return;
}

/* render custom-templates */
$view->set('Body', $body->render());

I tried the code above but I dont get the desired result. I am setting the global template with my setup. Need a bit of help :) Thanks :D

Share this post


Link to post
Share on other sites

Hi peterfoeng,

What is the content of your _Ajax.php file?

Does it work if you do echo ou the template and the exit if you do not have to process any additional logic?

if ($config->ajax) {
  $view = $factory->load('custom-templates/_Ajax.php', true);
  echo $view->render();
  exit;
}

Cheers

Share this post


Link to post
Share on other sites

Hi Wanze,

I figured it out, this is a silly mistake of not supplying the correct path LOL!

<?php
//we use the page title as template
$template = $sanitizer->name($page->title);

/* render load body */
$body = $factory->load('custom-templates/' . $template. '.php');

if ($config->ajax) {
  $view = $factory->load('_Ajax.php', true);
}

/* render custom-templates */
$view->set('Body', $body->render());

So, I am rendering the body with the global template option and if there is an ajax request, the global template is overridden with ajax.php or empty template using $factory->load() :D

Silly mistakes!

  • Like 1

Share this post


Link to post
Share on other sites

hi Wanze, this system is awesome but I'm getting some issues working with the "Global template file" option, probably i didn't get the right purpose of this.

my setup is:
"templates/views/" for the path
"tpl" for the extensions
this works good as long as i use files like home.php + home.tpl
but when i set
"main" for the Global template file (or should write main.php??)
i get just blank pages.
Can you please provide an example of the correct use of this option and file structure ?
 

Share this post


Link to post
Share on other sites

Hi Sevarf2,

Sorry for my late response...!

"main" for the Global template file (or should write main.php??)

Just "main" or "main.tpl" if you've set "tpl" as the extension.

i get just blank pages.
Can you please provide an example of the correct use of this option and file structure ?

What is the content of your main.tpl? You should see this content rendered by the selected engine.

This option is useful if your controllers (aka Pw-Templates) all render the same markup. With this option, you'd define this markup in your "main.tpl" file, and home.php would render it. If you don't use the global template file, home.php would render "home.tpl".

Does it make sense?

Cheers

Share this post


Link to post
Share on other sites

This looks very nice module Wanze, thanks.

One thing i don't understand.

Why people hook into Page::render instead of TemplateFile::render, which is actual ultimate output buffer for templates.

Forked your module and trying to figure out this and that, i like it mostly, just few things i wanted to test out.

Sorry if this post is on wrong forum section, feel free to move it to proper place.

Share this post


Link to post
Share on other sites

Hi Stikki,

Thanks!

Please report any issues or new features you'd like to see here!

Why people hook into Page::render instead of TemplateFile::render, which is actual ultimate output buffer for templates.

Never thought about that, have you tried it? :)

Share this post


Link to post
Share on other sites

This option is useful if your controllers (aka Pw-Templates) all render the same markup. With this option, you'd define this markup in your "main.tpl" file, and home.php would render it. If you don't use the global template file, home.php would render "home.tpl".

Hi Wanze!

I'm having trouble understanding the file structure yet.  

What happens with PW's _main.php and _init.php?

Where the views get the project's css/js/etc.?

What am I missing? :)

Share this post


Link to post
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


  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By daniels
      General
      This is a lightweight alternative to other newsletter & newsletter-subscription modules.
      It can subscribe, update, unsubscribe & delete a user in a list in Mailchimp with MailChimp API 3.0. It does not provide any forms or validation, so you can feel free to use your own. To protect your users, it does not save any user data in logs or sends them to an admin.
      This module fits your needs if you...
      ...use Mailchimp as your newsletter / email-automation tool ...want to let users subscribe to your newsletter on your website ...want to use your own form, validation and messages (with or without the wire forms) ...don't want any personal user data saved in any way in your ProcessWire environment (cf. EU data regulation terms) ...like to subscribe, update, unsubscribe or delete users to/from different lists ...like the Mailchimp UI for creating / sending / reviewing email campaigns You can find it here: https://github.com/danielstieber/SubscribeToMailchimp
      Let me know what you think and if I should add it to the Modules Directory.
      Setup
      Log into your Mailchimp account and go to  Profile > Extras > API Keys. If you don't have an API Key, create a new one. Copy your API Key and paste it in the module settings (Processwire > Modules > Site > SubscribeToMailchimp). Back in Mailchimp, go to the list, where you want your new subscribers. Go to Settings > List name and defaults. Copy the List ID an paste it in to the module settings.
      Usage
      To use the module, you need to load it into your template:
      $mc = $modules->get("SubscribeToMailchimp"); Now you can pass an email address to the module and it will try to edit (if the user exists) or create a new subscriber in your list.
      $mc->subscribe('john.doe@example.com'); You can also pass a data array, to add additional info.
      $mc->subscribe('john.doe@example.com', ['FNAME' => 'John', 'LNAME' => 'Doe']); You can even choose an alternative list, if you don't want this subscriber in your default list.
      $mc->subscribe('john.doe@example.com', ['FNAME' => 'John', 'LNAME' => 'Doe'], 'abcdef1356'); // Subscribe to List ID abcdef1356 If you want to unsubscribe a user from a list, you can use the unsubscribe method.
      $mc->unsubscribe('john.doe@example.com'); // Unsubscribe john.doe@example.com from the default list $mc->unsubscribe('john.doe@example.com', 'abcdef1356'); // Unsubscribe john.doe@example.com from the list abcdef1356 If you want to permantly delete a user, you can call the delete method. Carefully, this step cannot be undone
      $mc->delete('john.doe@example.com'); // Permanently deletes john.doe@example.com from the default list $mc->delete('john.doe@example.com', 'abcdef1356'); // Permanently deletes john.doe@example.com from the list abcdef1356  
      Important Notes
      This module does not do any data validation. Use a sever-sided validation like Valitron Make sure that you have set up your fields in your Mailchimp list. You can do it at Settings > List fields and *|MERGE|* tags Example
      Example usage after a form is submitted on your page:
      // ... validation of form data $mc = $modules->get("SubscribeToMailchimp"); $email = $input->post->email; $subscriber = [ 'FNAME' => $input->post->firstname, 'LNAME' => $input->post->lastname, ]; $mc->subscribe($email, $subscriber);  
      Troubleshooting
      In case of trouble check your ProcessWire warning logs.
      I can't see the subscriber in the list
      If you have enabled double opt-in (it is enabled by default) you will not see the subscriber, until he confirmed the subscription in the email sent by Mailchimp
      I get an error in my ProccessWire warning logs
      Check if you have the right List ID and API Key. Check if you pass fields, that exist in your list. Check if you pass a valid email address. Go to Mailchimps Error Glossary for more Information
      How To Install
      Download the zip file at Github or clone directly the repo into your site/modules If you downloaded the zip file, extract it in your sites/modules directory. You might have to change the folders name to 'SubscribeToMailchimp'. Goto the modules admin page, click on refresh and install it  
      Changelog
      0.0.2
      Note: You can update safely from 0.0.1 without any changes in your code
      New Features
      Added 'Unsubscribe' method $mc->unsubscribe($email, $list = "") Added 'Delete' method $mc->delete($email, $list = "") Bug Fixes and compatibility changes
      Removed type declarations to be compatible with PHP 5.1+* (thanks to wbmnfktr) Other
      Changed the way, the base url for the api gets called *I have only tested it with PHP 7.x so far, so use on owners risk
    • By BitPoet
      As threatened in the Pub sub forum in the "What are you currently building?" thread, I've toyed around with Collabora CODE and built file editing capabilities for office documents (Libre-/OpenOffice formats and MS Office as well as a few really old file types) into a PW module.
      If you are running OwnCloud or NextCloud, you'll perhaps be familiar with the Collabora app for this purpose.
      LoolEditor
      Edit office files directly in ProcessWire
      Edit your docx, odt, pptx, xlsx or whatever office files you have stored in your file fields directly from ProcessWire's page editor. Upload, click the edit icon, make your changes and save. Can be enabled per field, even in template context.
      Currently supports opening and saving of office documents. Locking functionality is in development.
      See the README on GitHub for installation instructions. You should be reasonably experienced with configuring HTTPS and running docker images to get things set up quickly.
      Pull requests are welcome!
      Here is a short demonstration:

    • By Robin S
      An Images field allows you to:
      Rename images by clicking the filename in the edit panel or in list view. Replace images, keeping metadata and filename (when possible) by dropping a new image on the thumbnail in the edit panel. Introduced here. But neither of these things is possible in File fields, which prompted this module. The way that files are renamed or replaced in this module is not as slick as in the Images field but it gets the job done. The most time-consuming part was dealing with the UI differences of the core admin themes. @tpr, gives me even more respect for the work that must go into AdminOnSteroids.
      Most of the code to support the rename/replace features is already present in InputfieldFile - there is just no UI for it currently. So hopefully that means these features will be offered in the core soon and this module can become obsolete.
       
      Files Rename Replace
      Allows files to be renamed or replaced in Page Edit.

      Usage
      Install the Files Rename Replace module.
      If you want to limit the module to certain roles only, select the roles in the module config. If no roles are selected then any role may rename/replace files.
      In Page Edit, click "Rename/Replace" for a file...
      Rename
      Use the text input to edit the existing name (excluding file extension).
      Replace
      Use the "Replace with" select to choose a replacement file from the same field. On page save the file will be replaced with the file you selected. Metadata (description, tags) will be retained, and the filename also if the file extensions are the same.
      Tip: newly uploaded files will appear in the "Replace with" select after the page has been saved.
       
      https://github.com/Toutouwai/FilesRenameReplace
      http://modules.processwire.com/modules/files-rename-replace/
    • By d'Hinnisdaël
      So I decided to wade into module development and created a wrapper module around ImageOptim, a service that compresses and optimizes images in the cloud. ImageOptim currently handles JPG, PNG and GIF files and, depending on the settings you use, shaves off between 15% and 60% in filesize. Great for bandwidth and great for users, especially on mobile.
      This module handles the part of uploading images to ImageOptim via their official API, downloading the optimized version and storing it alongside the original image.
       

       
      Download & Info
      GitHub / Module directory / Readme / Usage
       
      Why ImageOptim?
      There are other image optimization services out there, some of them free, that have outstanding ProcessWire modules. A few things make ImageOptim the best tool for most of my customers: It's not free, i.e. it will probably be around for a while and offers support. However, it's cheaper than some of the bigger competitors like Cloudinary. And it does PNG compression better than any of the free services out there, especially those with alpha channels.
       
      Installation
      Install the module like any other ProcessWire module, by either copying the folder into your modules folder or installing it via the admin. See above for downloads links on GitHub and in the module directory.
       
      Requirements
      To be able to upload images to the service, allow_url_fopen must be set on the server. The module will abort installation if that's not the case.
      I have only tested the module on ProcessWire 3.x installations. I don't see why it shouldn't work in 2.x, if anyone wants to try it out and report back.
       
      ImageOptim account
      To compress images, you first need to sign up for an ImageOptim account. They offer free trials to try the service.
       
      Usage (manual optimization)
      Images can be optimized by calling the optimize() method on any image. You can pass an options array to set ImageOptim API parameters.
      $image->size(800,600)->optimize()->url $image->optimize(['quality' => 'low', 'dpr' => 2]) // Set quality to low and enable hi-dpi mode
      Automatic optimization
      The module also has an automatic mode that optimizes all image variations after resizing. This is the recommended way to use this module since it leaves the original image uncompressed, but optimizes all derivative images.
      $image->size(800,600)->url // nothing to do here; image is optimized automatically
      To change compression setting for single images, you can pass an options array along with the standard ImageResizer options. Passing false disables optimization.
      $image->size(800, 600, ['optimize' => 'medium']) $image->size(800, 600, ['optimize' => ['quality' => 'low', 'dpr' => 2]]) $image->size(800, 600, ['optimize' => false])
      For detailed usage instructions and all API parameters, see the usage instructions on GitHub.
       
      Filenames
      Optimized images will be suffixed, e.g. image.jpg becomes image.optim.jpg. You can configure the suffix in the module settings.
       
      Roadmap
      Asynchronous processing. Not really high on the list. Image variations need to be created anyway, so waiting a few seconds longer on first load is preferable to adding complexity to achieve async optimization. Optimize image variations created by other modules. CroppableImage comes to mind. I don't use any of these, so if somebody wants to help out and submit a pull request — all for it! Add a dedicated page in the setup menu with a dashboard and detailed statistics. ImageOptim's API is very barebones for now, so not sure if that's feasible or even necessary.
      Stability
      I've been using this module on production sites for some time now, without hiccups. If you do notice oddities, feel free to comment here or investigate and submit PRs.
    • By PWaddict
      Module for hiding pages for non-superusers.
      Download
      HidePages
      Requirements
      ProcessWire 3.x Changelog
      1.0.1 (31 March 2018)
      Module renamed to prevent confusion 1.0.0 (30 March 2018)
      Initial release