ProcessWire - Render Funktionen: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
(Die Seite wurde neu angelegt: „Hier einige Snippets und Links zum Rendern von Files, Feldern und Templates == Links == https://processwire.com/blog/posts/processwire-3.0.7-expands-field-ren…“)
 
 
(18 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...
 +
<syntaxhighlight lang="php">
 +
// $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
  
Beispiel
+
// renderValue usage
 +
$mixed = $page->renderValue(mixed $value);
 +
$mixed = $page->renderValue(mixed $value, string $file = '');
  
/site/templates/fields/body.php
+
// Render a value using site/templates/fields/my-images.php custom output template
 +
$images = $page->images;
 +
echo $page->renderValue($images, 'my-images');
  
<syntaxhighlight lang="php">
+
// In Template .../fields/children.php access $results as $value
<div class='bodycopy'>
+
$results = $pages->find("body%=some text");
  <?= $value ?>
+
echo $page->renderValue($results, 'children');
</div>
+
 
 +
//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
 
</syntaxhighlight>
 
</syntaxhighlight>
 
Ausgabe z.B. mit:
 
  
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
echo $page->render('body');
+
$page->render($filename); // $filename assumed in /site/templates/
echo $page->render->body;
+
$page->render($pathname); // $pathname is full path, but must resolve somewhere in web root
echo $page->_body_; // Nützlicher Shortcut wenn man mehrere Felder mit Template ausgeben möchte.
+
$page->render($options); // array of options and/or your own variables
</syntaxhighlight>
+
$page->render(array('foo' => 'bar')); // same as above
 +
$page->render($filename, $options); // specify filename and options/vars, etc.
  
Es stehen folgende Variablen zur Verfügung:
 
    $value - The value that needs to be rendered
 
    $page - The page that $value lives on
 
    $field - The field representing $value (of class Field) if you want it
 
  
<syntaxhighlight lang="php">
+
$content .= $files->render('teasers', ['items' => $pages->find("template=article")]);
</syntaxhighlight>
 
  
<syntaxhighlight lang="php">
+
foreach ($pages->find("template=article") as $article) {
</syntaxhighlight>
+
    $content .= $article->render('teaser.php');
<syntaxhighlight lang="php">
+
}
 
</syntaxhighlight>
 
</syntaxhighlight>
  
<syntaxhighlight lang="php">
+
== Use Cases und Unterschiede ==
</syntaxhighlight>
+
$page->render('some_field')
<syntaxhighlight lang="php">
 
</syntaxhighlight>
 
  
<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.
</syntaxhighlight>
 
<syntaxhighlight lang="php">
 
</syntaxhighlight>
 
  
<syntaxhighlight lang="php">
+
If you provide a '''filename as a param''', $page->render() will use that file to render given page.
</syntaxhighlight>
 
<syntaxhighlight lang="php">
 
</syntaxhighlight>
 
  
<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'''.
</syntaxhighlight>
 
<syntaxhighlight lang="php">
 
</syntaxhighlight>
 
  
<syntaxhighlight lang="php">
+
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.
</syntaxhighlight>
 
<syntaxhighlight lang="php">
 
</syntaxhighlight>
 
  
== Verschiedenes ==
+
=== $page->render vs $files->render ===
<syntaxhighlight lang="php">
+
PAGE
// New Functions in 3.x
+
$page rendert inkl. head etc. (also eine richtige html Seite
render()
+
Zugriff auf Variablen über $options Array $foo -> $options['foo']
renderValue()
 
  
// Functions for 2.x or for easy snippet inclusion (also possible with render
+
FILES
wireRenderFile() // mostly used for delayed output
+
$files rendert nur das was return zurück gibt (kein html, body, etc)
wireIncludeFile() // for direct output
+
Zugriff auf Variablen direkt über den Namen. Wenn du $foo bleibt $foo
</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