ProcessWire - Snippets: Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 1: Zeile 1:
 +
Hier findest du ein paar Basis Snippets. Codebeispiele zu Feldern findest du z.B. über die Seiten zu den Field Types.
 
== Basics ==
 
== Basics ==
 
=== Bild in Template einbinden ===
 
=== Bild in Template einbinden ===

Version vom 25. Januar 2019, 10:34 Uhr

Hier findest du ein paar Basis Snippets. Codebeispiele zu Feldern findest du z.B. über die Seiten zu den Field Types.

Basics

Bild in Template einbinden

<img src="<?php echo $config->urls->templates?>img/menu.png" alt="Home">

Link zu Seite

<a href="<?php echo $pages->get('/kontakt/')->url; ?>">

Wert aus Textfeld

<div class="<? echo $page->get("content_class")?>">

Bilder

Advanced Image Manipulation

Processwire - Advanced Image Manipulation

Einzelbild einfügen

Bild aus Backend als Hintergrund einfügen

(Feldname hier: main_image)

if($page->main_image){$image = $page->main_image->size(640,960);}
else $image = NULL;
...
if($image){
  echo "
  <div class='a-3-4 team-image' style='background-image:url({$image->url})'>
    <div>{$myContent}</div>
  </div>
  ";
}

n-tes Bild einfügen

$page->images->eq(1); // second image

Mehrere Bilder aus dem Backend

$myImages='';
if(count($page->images)) {
  foreach($page->images as $image) {
    $myImages .=  '<img class="img-responsive" src="'.$image->url.'">';
  }
}

Beispiel 2

echo $page->images->implode(function($item) {
  $large = $item->size(1200,800);
  $thumb = $item->size(400,300);
  return "<li><a href='$large->url'><img src='$thumb->url' alt='$item->description'></a></li>";
}, array('prepend' => '<ul>', 'append' => '</ul>'));

Einfache Gallerie

$gallery = '<div class="gallery">';
$images = $page->gallery;

// add thumbnail to gallery
if(count($page->thumbnail)){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$page->thumbnail->url.'"><img src="'. $page->thumbnail->getThumb('thumbnail') .'" title="'. $image->description .'"></a>';
}

foreach($images as $image){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$image->url.'"><img src="'. $image->getThumb('thumbnail') .'" title="'. $image->description .'"></a> ';
}
$gallery .= '</div>';

Slider mit Processwire

Processwire - Slider

Videos

Processwire - Working with Video

File Uploads

ProcessWire - Uploads

Navigation

ProcessWire - Navigation Snippets

Seiten

http://processwire.com/api/variables/pages/

Inhalt einer Seite mit $page->get()

Beispiel

<p>Visit us at: 
<?php$contact = $pages->get("/about/contact/");
echo $contact->address;
?>
</p>

Kürzer:

<p>Visit us at: <?=$pages->get("/about/contact/")->address?></p>

Seiten Filtern find()

"Featured" Checkbox im Backend angehakt

<ul>
<?php$features = $pages->find("featured=1");
foreach($features as $feature)
   echo "<li><a href='{$feature->url}'>{$feature->title}</a></li>";
?>
</ul>

Beispiel: Limitierte Ausgabe, Sortieren und in Unterverzeichnis

$pages->find("parent=/about/press/, featured=1, limit=3, sort=-date");

oder

$pages->get("/about/press/")->children("featured=1, limit=3, sort=-date");

Finde in Kategorie

$pages->get("/about/press/")->find("featured=1, limit=3, sort=-date");

Seiten mit Template xyz

$pages->find("template=press_release, featured=1, limit=3, sort=-date");

Ausführliches Beispiel mit Ausgabe

<ul>
<?php$features = $pages->find("template=press_release, featured=1, limit=3, sort=-date");
foreach($features as $feature) {
  echo "<li>" .
    "<h3><a href='{$feature->url}'>{$feature->title}</a></h3>" .
    "<span class='date'>{$feature->date}</span>" .
    "<p>{$feature->summary}</p>" .
    "</li>";}
?></ul>

Felder

Ist ein Feld leer ?

if($page->summary_de) { ... }
// check whitespace
if(trim($page->summary_de)) { ... }

Datumsfelder formatieren

 
// aus Datumsfeldern
echo date("Y-m-d", $page->getUnformatted("closing")); 
// aus Unix Timestamp mit Vanilla PHP
echo date("Y-m-d", $news->created);

setlocale(LC_TIME, "de_DE.utf8");
$weekday = strftime ( '%A' , $page->getUnformatted("date") );
$date = date("d.m.Y", $page->getUnformatted("date"));

Searchbar

	<!-- search form-->
	<form class='search' action='<?php echo $pages->get('template=search')->url; ?>' method='get'>
		<input type='text' name='q' placeholder='Search' value='<?php echo $sanitizer->entities($input->whitelist('q')); ?>' />
		<button type='submit' name='submit'>Search</button>
	</form>

Editor Link / Login Logout Link

_main.php

<div class="editor">
<?php 
  if($user->isLoggedin()) {
    // if user is logged in, show a logout link
    echo "<a href='{$config->urls->admin}login/logout/'>Logout ($user->name)</a>";
  } else {
    // if user not logged in, show a login link
    echo "<a href='{$config->urls->admin}'>∆</a>";
  }
  // output an "Edit" link if this page happens to be editable by the current user
  if($page->editable()) echo "<li class='edit nav'><a href='$page->editUrl'>Edit</a></li>";
?>
</div>

Siehe auch ProcessWire - User Login / Logout

Conditions

Bedingungen durch Oder Operator in Feldern

Wenn das eine Feld leer ist nimm das nächste...

$t = $page->get("long_title|title");

Conditions für Navigation

// If the page has children, then render navigation to them under the body.
// See the _func.php for the renderNav example function.
if($page->hasChildren) {
	$content .= renderNav($page->children);
}


// if the rootParent (section) page has more than 1 child, then render 
// section navigation in the sidebar
if($page->rootParent->hasChildren > 1) {
	$sidebar = renderNavTree($page->rootParent, 3) . $page->sidebar; 
}

$page Object

Zugriff auf $page in Funktionen

Beispiel:

function getTitle() {
    $page = wire("page");
    $t = $page->title;

oder einfach übergeben.

Copy and Paste

Copyright / Published Date

$published_date = date('d.m.Y',$page->created);
<?= '©'.$published_date ?>

Stylesheets und Skripte mit AIOM

<script src="<?php echo AIOM::JS(array('scripts/jquery-2.2.4.min.js','scripts/jquery.chocolat.js','scripts/jquery.fitvids.js','scripts/jquery.flexslider-min.js', 'scripts/main.js')); ?>"></script>
<link rel="stylesheet" type="text/css" href="<?php echo AIOM::CSS('styles/main.less'); ?>">
<?php echo $homepage->site_title .' '. Date('Y'); ?>

Link zur Seite anhand URL-Pfad

<?php echo $pages->get('/ueber-uns/')->url; ?>

Feld einer Seite

$content_left = $page->get('content_left');

Bild

if($page->main_image){$image = $page->main_image->size(640,960);}
else $image = NULL;

Bild von der Homepage

in _init.php

$homepage = $pages->get('/');

im Template

if($homepage->config_logo){$logo = $homepage->config_logo->width(280);}
else $logo = NULL;

Simple Navigation

<ul class='nav-list'>
  <?php
  // Childrens
  foreach(($page->children) as $item) {
    if($item->id == $page->rootParent->id) {
      echo '<li class="current">';
    } else {
      echo '<li>';
    }
    echo '<a class="ajax-link" href="'.$item->url.'">'.$item->title.'</a></li>';
  }
  ?>
</ul>

Auflistung mit div. Feldern aus Kindseiten

/**
 * Publications list template
 *
 */
$publications = '';
$children = $page->children;
foreach($children as $child){
  $publications .= '<div class="box">';
  $publications .= '<h2><a class="ajax-link" parent="'. $child->parent->title .'" name="'.$child->title.'" href="'. $child->url. '">'. $child->title . '</a></h2>';
  $publications .= $child->body;
  $publications .= '</div>';
}

$content = $page->body . $publications;

Minigalerie

Thumbnails / Child Pages

$works = '<div class="gallery">';
$children = $page->children;
foreach($children as $child){
  $works .= '<a class="ajax-link" parent="'. $child->parent->title .'" name="'.$child->title.'" href="'. $child->url. '">';
  $works .= '<h2>'. $child->title . '</h2>';

  if(count($child->thumbnail)){
      $works .= '<div class="thumbnail"><img src="'. $child->thumbnail->getThumb('thumbnail') .'" title="'. $image->description .'"></div>';
  }

  $works .= '</a>';
}
$works .= '</div>';


$content = $page->body . $works . '<br/>' . $page->video;

Page Navigation

// add next button if there is another page next
if($page->next->id){
    $next = '<div class="next"><i>Next:</i>&nbsp;<a class="ajax-link" name="'.$page->next->title.'" href="'. $page->next->url .'">'. $page->next->title .'<span class="fa fa-arrow-right"><span></a></div>';
}

$content = '<h2>'. $page->title . '</h2>' . $page->body . '<br/><div class="video">' . $page->video .'</div>' . $next;

Simple Gallery with next Page

$gallery = '<div class="gallery">';
$images = $page->gallery;

// add thumbnail to gallery
if(count($page->thumbnail)){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$page->thumbnail->url.'"><img src="'. $page->thumbnail->getThumb('thumbnail') .'" title="'. $image->description .'"></a>';
}

foreach($images as $image){
    $gallery .= '<a class="chocolat-image gallery-item" href="'.$image->url.'"><img src="'. $image->getThumb('thumbnail') .'" title="'. $image->description .'"></a> ';
}
$gallery .= '</div>';

// add next button if there is another page next
if($page->next->id){
    $next = '<div class="next"><i>Next:</i>&nbsp;<a class="ajax-link" name="'.$page->next->title.'" href="'. $page->next->url .'">'. $page->next->title .'<span class="fa fa-arrow-right"><span></a></div>';
}


$content = '<h2>'. $page->title . '</h2>' . $page->body . '<br/>' . $gallery . '<br/><div class="video">' . $page->video .'</div>'. $next;

ProcessWire - Debugging Snippets

ProcessWire - Debugging Snippets