wanze / TemplateEngineFactory

🏭Provides ProcessWire integration for various template engines such as Twig.
MIT License
26 stars 11 forks source link

Adds setMultiple and renderChunk methods #1

Closed justb3a closed 8 years ago

justb3a commented 8 years ago

Added a setMultiple method inside the factory to be able to submit more than one property at once:

$view->setMultiple(array(
  'posts' => $posts,
  'hello' => 'world'
));

Added a renderChunk method to be able to render and reuse template partials.

template file: /site/templates/view/template.twig

<?php foreach ($nav_pages as $p): ?>
  <?php $page->renderChunk('chunks/nav-item', array('page' => $p)); ?>
<?php endforeach; ?>

/site/templates/chunks/nav-item.php

<?php namespace ProcessWire;

$foo = 'do some logic // something with the contextual data';
$author = $this->page->createdUser;

$view->setMultiple( array(
  'author' => $author,
  'item' => $this->page,
  'foo' => $foo
));

/site/templates/view/chunks/nav-item.tpl

Author: <?= $author ?> <br />
<a href="<?= $item->url ?>" title="<?= $foo ?>">
  <?= $item->title ?>
</a>

Note: I had to access \ProcessWire\TemplateFile. I guess, this won't work using the 2.x branch because of it's lacking namespace support.