ProcessWire - Render Funktionen: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
 
(16 dazwischenliegende Versionen von 2 Benutzern werden nicht angezeigt)
Zeile 1: Zeile 1:
Hier einige Snippets und Links zum Rendern von Files, Feldern und Templates
+
Hier eine Übersicht über verschiedene Möglichkeiten Files, Felder und Templates zu rendern.
 
== Links ==
 
== Links ==
 +
[[ProcessWire - Page Rendering Functions]]
 +
[[ProcessWire - Rendering Funktionen]]
 +
[[ProcessWire - Rendering Templates mit fields Ordner]]
 
  https://processwire.com/blog/posts/processwire-3.0.7-expands-field-rendering-page-path-history-and-more/#theres-also-page-gt-rendervalue
 
  https://processwire.com/blog/posts/processwire-3.0.7-expands-field-rendering-page-path-history-and-more/#theres-also-page-gt-rendervalue
 +
https://processwire.com/api/ref/page/render-value/
  
== Felder mit Templates rendern ==
+
== Kleine ProcessWire Renderübersicht ==
ProcessWire rendert Felder mit einem Template wenn es in /site/templates/fields/ liegt und richtig benannt ist.
+
Die häufigsten...
 
 
Beispiel
 
 
 
/site/templates/fields/body.php
 
 
 
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
<div class='bodycopy'>
+
// $page->render
  <?= $value ?>
+
$page->render($filename); // $filename (including .php) assumed in /site/templates/
</div>
+
$page->render($pathname); // $pathname is full path, but must resolve somewhere in web root
</syntaxhighlight>
+
$page->render($filename, $myArray); // $myArray can be accessed via $options in Template
 +
$page->render($fieldname) // short for $page->renderField() IF parameter is a fieldname - then pw looks for template file in /fields
  
Ausgabe z.B. mit:
+
// renderValue usage
 +
$mixed = $page->renderValue(mixed $value);
 +
$mixed = $page->renderValue(mixed $value, string $file = '');
  
<syntaxhighlight lang="php">
+
// Render a value using site/templates/fields/my-images.php custom output template
echo $page->render('body');
+
$images = $page->images;
echo $page->render->body;
+
echo $page->renderValue($images, 'my-images');  
echo $page->_body_; // Nützlicher Shortcut wenn man mehrere Felder mit Template ausgeben möchte.
 
</syntaxhighlight>
 
  
Es stehen folgende Variablen zur Verfügung:
+
// In Template .../fields/children.php access $results as $value  
    $value - The value that needs to be rendered
+
$results = $pages->find("body%=some text");
    $page - The page that $value lives on
+
echo $page->renderValue($results, 'children');
    $field - The field representing $value (of class Field) if you want it
 
  
=== Varianten (Custom Template) ===
+
//FILES
Mit einem Zweiten Parameter kann man auch weitere Templates anlegen. Die werden dann passend zum Namen gesucht. Reihenfolge wie im Beispiel
+
// $files->renderUsage
 
+
$files->render('teasers', ['items' => $pages->find("template=article")]); // mostly for delayed output
Beispiel
+
wireRenderFile('my_template', array('pid' => $child->id)); // old version of $files->render
echo $page->render('body', 'custom');
+
wireIncludeFile() // for direct output
Sucht nach templates wie unten und nimmt das erste das gefunden wird. Die erste Variante ergibt für mich am meisten Sinn.
 
 
 
    /site/templates/fields/body/custom.php
 
    /site/templates/fields/custom/body.php
 
    /site/templates/fields/custom.php
 
    /site/templates/fields/body.php (fallback)
 
Man kann es aber auch genau angeben:
 
echo $page->render('body', '/custom'); // use only custom.php
 
echo $page->render('body', '/custom/body'); // use only custom/body.php
 
 
 
=== Felder mit Repeater Matrix ===
 
 
 
Matrix Felder könnte man so handeln:
 
 
 
<syntaxhighlight lang="php">
 
foreach($page->test_matrix as $item) {
 
  if($item->type == 'content') {
 
    echo "
 
      <h3>$item->headline</h3>
 
      <p>$item->summary</p>
 
    ";
 
  } else if($item->type == 'quote') {
 
    echo "
 
      <blockquote>
 
        <p>$item->quote</p>
 
        <cite>$item->cite</cite>
 
      </blockquote>
 
    ";
 
  } else if( ... ) {
 
    // and so on
 
  }
 
}
 
</syntaxhighlight>
 
 
 
Über den Field Template Ansatz kann man die Matrix eleganter rendern. Wenn man sie so anlegt...
 
  
/site/templates/fields/test_matrix.php
+
//$page->render() is intended for rendering a specific page (optionally using a specific file)
<syntaxhighlight lang="php">
+
//wireRenderFile() renders a specific file without the need for a Page object
foreach($value as $item) {
 
  echo $item->render();
 
}
 
 
</syntaxhighlight>
 
</syntaxhighlight>
  
... sucht die render Funktion im nächsten Unterordner für jeden Typ das Templatefile...
 
 
/site/templates/fields/test_matrix/content.php
 
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
<h3><?= $page->headline ?></h3>
+
$page->render($filename); // $filename assumed in /site/templates/
<p><?= $page->summary ?></p>
+
$page->render($pathname); // $pathname is full path, but must resolve somewhere in web root
</syntaxhighlight>
+
$page->render($options); // array of options and/or your own variables
/site/templates/fields/test_matrix/quote.php
+
$page->render(array('foo' => 'bar')); // same as above
<syntaxhighlight lang="php">
+
$page->render($filename, $options); // specify filename and options/vars, etc.
<blockquote>
 
  <p><?= $page->quote ?></p>
 
  <cite><?= $page->cite ?></cite>
 
</blockquote>
 
</syntaxhighlight>
 
 
 
So kann man im Template ganz einfach aufrufen:
 
  
<syntaxhighlight lang="php">
 
echo $page->render('test_matrix');
 
</syntaxhighlight>
 
  
=== Felder sinnvoll gruppieren ===
+
$content .= $files->render('teasers', ['items' => $pages->find("template=article")]);
Mit geschickter Strategie lassen sich zum Beispiel geschickt Varianten erzeugen. Z.B. verkürzte Absätze oder Thumbnails statt der Vollansicht:
 
  
'''/site/templates/fields/short/body.php'''
+
foreach ($pages->find("template=article") as $article) {
 
+
    $content .= $article->render('teaser.php');
<syntaxhighlight lang="php">
 
// render the first found paragraph only
 
if(preg_match('!(<p>.+?</p>)!', $value, $matches)) {
 
  echo $matches[1];
 
 
}
 
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''/site/templates/fields/short/images.php'''
+
== Use Cases und Unterschiede ==
 +
$page->render('some_field')
  
<syntaxhighlight lang="php">
+
does indeed '''call renderField(), but only if the given param is a field name'''. Take a look at PageRender for more details.
// render a thumbnail of the first image
 
if(count($value)) {
 
  $image = $value->first()->width(200);
 
  echo "<img src='$image->url' alt='$image->description'>";
 
}
 
</syntaxhighlight>
 
  
'''/site/templates/fields/short/title-link.php'''
+
If you provide a '''filename as a param''', $page->render() will use that file to render given page.
  
<syntaxhighlight lang="php">
+
If you provide a '''filename (actual filename, not just something like "file")''' to $page->render() '''as the first param''', you can provide an '''array as the second param''', and the values stored in that array can then be accessed via '''$options'''.
// render a linked title
 
echo "<a href='$page->url'><h2>$page->title</h2></a>";
 
</syntaxhighlight>
 
Im Haupttemplate kann man das z.B. für eine Zusammenfassung nutzen:
 
 
 
<syntaxhighlight lang="php">
 
foreach($page->children as $child) {
 
  echo $child->render('title', 'short/title-link');
 
  echo $child->render('images', 'short/');
 
  echo $child->render('body', 'short/');
 
}
 
</syntaxhighlight>
 
  
 +
It seems to me that you're trying to use '''$page->render()''' to something it's really not meant for: $page->render() is intended for rendering a specific page (optionally using a specific file), while '''wireRenderFile()''' renders a specific file without the need for a Page object. Those are two different use cases.
  
 +
=== $page->render vs $files->render ===
 +
PAGE
 +
$page rendert inkl. head etc. (also eine richtige html Seite
 +
Zugriff auf Variablen über $options Array $foo -> $options['foo']
  
<syntaxhighlight lang="php">
+
FILES
</syntaxhighlight>
+
$files rendert nur das was return zurück gibt (kein html, body, etc)
<syntaxhighlight lang="php">
+
Zugriff auf Variablen direkt über den Namen. Wenn du $foo bleibt $foo
</syntaxhighlight>
 
 
 
<syntaxhighlight lang="php">
 
</syntaxhighlight>
 
<syntaxhighlight lang="php">
 
</syntaxhighlight>
 
 
 
 
 
== Verschiedenes ==
 
<syntaxhighlight lang="php">
 
// New Functions in 3.x
 
render()
 
renderValue()
 
 
 
// Functions for 2.x or for easy snippet inclusion (also possible with render
 
wireRenderFile() // mostly used for delayed output
 
wireIncludeFile() // for direct output
 
</syntaxhighlight>
 

Aktuelle Version vom 4. Dezember 2024, 12:51 Uhr

Hier eine Übersicht über verschiedene Möglichkeiten Files, Felder und Templates zu rendern.

Links[Bearbeiten]

ProcessWire - Page Rendering Functions
ProcessWire - Rendering Funktionen
ProcessWire - Rendering Templates mit fields Ordner
https://processwire.com/blog/posts/processwire-3.0.7-expands-field-rendering-page-path-history-and-more/#theres-also-page-gt-rendervalue
https://processwire.com/api/ref/page/render-value/

Kleine ProcessWire Renderübersicht[Bearbeiten]

Die häufigsten...

// $page->render
$page->render($filename); // $filename (including .php) assumed in /site/templates/
$page->render($pathname); // $pathname is full path, but must resolve somewhere in web root
$page->render($filename, $myArray); // $myArray can be accessed via $options in Template
$page->render($fieldname) // short for $page->renderField() IF parameter is a fieldname - then pw looks for template file in /fields

// renderValue usage
$mixed = $page->renderValue(mixed $value);
$mixed = $page->renderValue(mixed $value, string $file = '');

// Render a value using site/templates/fields/my-images.php custom output template
$images = $page->images;
echo $page->renderValue($images, 'my-images'); 

// In Template .../fields/children.php access $results as $value 
$results = $pages->find("body%=some text");
echo $page->renderValue($results, 'children'); 

//FILES 
// $files->renderUsage
$files->render('teasers', ['items' => $pages->find("template=article")]); // mostly for delayed output
wireRenderFile('my_template', array('pid' => $child->id)); // old version of $files->render
wireIncludeFile() // for direct output

//$page->render() is intended for rendering a specific page (optionally using a specific file)
//wireRenderFile() renders a specific file without the need for a Page object
$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');
}

Use Cases und Unterschiede[Bearbeiten]

$page->render('some_field') 

does indeed call renderField(), but only if the given param is a field name. Take a look at PageRender for more details.

If you provide a filename as a param, $page->render() will use that file to render given page.

If you provide a filename (actual filename, not just something like "file") to $page->render() as the first param, you can provide an array as the second param, and the values stored in that array can then be accessed via $options.

It seems to me that you're trying to use $page->render() to something it's really not meant for: $page->render() is intended for rendering a specific page (optionally using a specific file), while wireRenderFile() renders a specific file without the need for a Page object. Those are two different use cases.

$page->render vs $files->render[Bearbeiten]

PAGE
$page rendert inkl. head etc. (also eine richtige html Seite
Zugriff auf Variablen über $options Array $foo -> $options['foo']
FILES
$files rendert nur das was return zurück gibt (kein html, body, etc)
Zugriff auf Variablen direkt über den Namen. Wenn du $foo bleibt $foo