ProcessWire - Rendering Funktionen: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 41: Zeile 41:
 
  echo $pages->get($child->id)->render(array('mobile' => $mobile));
 
  echo $pages->get($child->id)->render(array('mobile' => $mobile));
  
=== Check if File is called directly or via wireRenderFile ===
+
=== Check if File is called directly or via wire->render->file ===
 
  https://processwire.com/talk/topic/20915-wirerenderfile-question-about-page/
 
  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:
 
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));
+
wireRenderFile('my_template', array('pid' => $child->id));
 
And in my_template, to access it's own page fields, etc. I use
 
And in my_template, to access it's own page fields, etc. I use
  
Zeile 53: Zeile 53:
  
 
d'oh of course, it's as simple as:
 
d'oh of course, it's as simple as:
 
+
<pre>
 
  if(isset($pid)) {
 
  if(isset($pid)) {
 
     $thisPage = pages()->get($pid); // we get called with wireRenderFile
 
     $thisPage = pages()->get($pid); // we get called with wireRenderFile
Zeile 59: Zeile 59:
 
     $thisPage = $page; // business as usual
 
     $thisPage = $page; // business as usual
 
  }
 
  }
 +
</pre>
  
 
== Rendering von _main (AppendFile) verhindern ==
 
== Rendering von _main (AppendFile) verhindern ==

Version vom 18. Juni 2023, 12:17 Uhr

In den Template Strategien 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

ProcessWire - Page Rendering Functions (ältere Seite)
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)
https://processwire.com/api/ref/inputfield-checkboxes/render/
https://processwire.com/api/ref/page/render-field/
https://processwire.com/api/ref/page/render-value/ (mit Markup File)
https://processwire.com/docs/front-end/output/delayed/ (delayed output)
https://processwire.com/talk/topic/13958-help-understanding-render-and-rendervalue/

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 wire->render->file

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
 }

Rendering von _main (AppendFile) verhindern

Manchmal möchte man das automatische Einbinden von _main.php das man vorher in config.php angelegt hat verhindern. Z.B. Wenn man nur einen Teilbereich mit einer Templatedatei render möchte.

Es gibt verschiedene Strategien:

1. Template Einstellungen

In neueren Versionen kann man appendFile (und prependFile) direkt im Template unter dem Tab Datei deaktivieren

2. In _init.php Prüfvariable anlegen

/* * Whether to include the _main.php markup file? 
For example, your template * file would set it to false when generating a page 
for sitemap.xml * or ajax output, in order to prevent display of the main <html> 
document. * */

$useMain = true;

In _main.php abfragen:

if(!$useMain) return;

In Templates die _main.php nicht nutzen sollen:

$useMain = false;

3. Man kann auch mit Funktionen das append Steuern:

$tpl = $templates->get("location");
$tpl->noAppendTemplateFile = 1;
$tpl->appendFile = "";
$tpl->save();