ProcessWire - Rendering Funktionen
In den Template Strategiene sind einige Methoden beschrieben wie man Seiten in ProcessWire rendern kann. PW stellt aber auch einige Funktionen bereit, mit denen man Dateien oder Seiten direkt rendern kann. Diese arbeiten auch gut mit dem WireCache zusammen. So kann man Teile der Ausgabe rendern und gleich Cachen.
Links
https://processwire.com/api/ref/wire-file-tools/render/ https://processwire.com/api/ref/functions/wire-render-file/ https://processwire.com/api/ref/page/render-field/ https://processwire.com/talk/topic/20797-clean-syntax-for-rendering-pages-with-specific-template/ https://processwire.com/talk/topic/3145-multiple-views-for-templates/page/2/?tab=comments#comment-32876 (ergiebiger thread)
Functions
$files->render // modern version for wireRenderFile $page->render https://processwire.com/api/ref/page/render-field/
Snippets
$page->render($filename); // $filename assumed in /site/templates/
$page->render($pathname); // $pathname is full path, but must resolve somewhere in web root
$page->render($options); // array of options and/or your own variables
$page->render(array('foo' => 'bar')); // same as above
$page->render($filename, $options); // specify filename and options/vars, etc.
$content .= $files->render('teasers', ['items' => $pages->find("template=article")]);
foreach ($pages->find("template=article") as $article) {
$content .= $article->render('teaser.php');
}
Pass global vars
You can pass variable via render like:
echo $pages->get($child->id)->render(array('mobile' => $mobile));
Check if File is called directly or via wireRenderFile
https://processwire.com/talk/topic/20915-wirerenderfile-question-about-page/
Is there a way to check in my included template file if this child page is called directly, or whether it has been included from somewhere else? A simple check, so I can use the template in both scenarios (stand-alone view for just this page, or in cases I use wireRenderFile somewhere else). Right now I am passing the child page's ID like this:
wireRenderFile('my_template', array('pid' => $child->id)); And in my_template, to access it's own page fields, etc. I use
$thisPage = pages()->get($pid); $myField = $thisPage->foo; // etc.
Is that the way to go, or do I overlook something obvious?
d'oh of course, it's as simple as:
if(isset($pid)) {
$thisPage = pages()->get($pid); // we get called with wireRenderFile
} else {
$thisPage = $page; // business as usual
}