ProcessWire - Hanna Code (Module): Unterschied zwischen den Versionen

Aus Wikizone
Wechseln zu: Navigation, Suche
Zeile 223: Zeile 223:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
 +
=== Content einer beliebigen Seite rendern ===
 +
Hier am Beispiel einer Seite die Charts rendert (Template chart).
 +
[[chart pageid=1234]]
 
<syntaxhighlight lang="php">
 
<syntaxhighlight lang="php">
 +
<?php namespace ProcessWire;
 +
// if in repeater or repeaterMatrix find the first parent ProcessWire\Page Page
 +
if (!function_exists('getParentPage')) {
 +
    function getParentPage($myPage, $level=1){
 +
        $maxLevel=3;
 +
        if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
 +
            $level +=1;
 +
            if($level > $maxLevel) return false;
 +
            $forPage = $myPage->getForPage();
 +
            //var_dump( $myPage->getParentPage());
 +
            //echo("<p>next Level: ".get_class($forPage)."</p>");
 +
            return $myPage = getParentPage($forPage);
 +
        }else if( get_class($myPage) == 'ProcessWire\Page'){ 
 +
            return $myPage;
 +
        }
 +
    }
 +
}
 +
 +
$parent = getParentPage($page);
 +
$parent->hasChart = true;
 +
$chart = $pages->get($pageid); // this page contains our content. You should ommit appendFile in it's template because we don't want <head> etc.
 +
$out ='';
 +
$out .= $chart->render();
 +
 +
echo $out;
 
</syntaxhighlight>
 
</syntaxhighlight>

Version vom 15. Juni 2023, 21:03 Uhr

TextformatterHannaCode

Easily insert any complex HTML, Javascript or PHP output in your ProcessWire content by creating your own Hanna code tags.

https://github.com/ryancramerdesign/ProcessHannaCode#using-hanna-code-from-the-api

Hanna Code Snippets

Image from RepeaterMatrix or Repeater

<?php
// include a image from the parent page
if (!function_exists('getImagePage')) {
    
    function getImagePage($myPage, $level=1){
        // if in repeater or repeaterMatrix find the first parent ProcessWire\Page Page
        $maxLevel=5;
        if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
            $level +=1;
            if($level > $maxLevel) return false;
            $forPage = $myPage->getForPage();
            //var_dump( $myPage->getForPage());
            //echo("<p>next Level: ".get_class($forPage)."</p>");
            return $myPage = getImagePage($forPage);
           
        }else if( get_class($myPage) == 'ProcessWire\Page'){
            
            return $myPage;
        }
    }
}
$out = '';
$imagePage = getImagePage($page);
$images = $imagePage->images;
$imgTag = '';

if($images){
    if(isset($file)){
        isset($width) ? $image =  $images->get($file)->width($width) : $image = $images->get($file);
        $imgTag .= "<img src='$image->url'"; 
        if(isset($width)) $imgTag .= " width='$width'";
        if(isset($class)) $imgTag .= " class='$class'";
        if(isset($style)) $imgTag .= " style='$style'";
        $imgTag .= '>';
        $out = $imgTag;
    }else{
        $out .= "<p>Gib einen Bildnamen an z.B.</p>";
        foreach($images as $image){
            $out .= '<p>[[image file="'.$image->name.'"]]</p>';
        }
    }
}else{
   $out .= '<p>Keine Bilder auf der Seite gefunden</p>';
    
}
echo $out;

Templates

<?php
echo(urls()->templates);

Video

Von chargercube - todo security / video field statt dateifeld

<?php

// include a image from the parent page
if (!function_exists('getImagePage')) {
    
    function getImagePage($myPage, $level=1){
        // if in repeater or repeaterMatrix find the first parent ProcessWire\Page Page
        $maxLevel=5;
        if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
            $level +=1;
            if($level > $maxLevel) return false;
            $forPage = $myPage->getForPage();
            //var_dump( $myPage->getForPage());
            //echo("<p>next Level: ".get_class($forPage)."</p>");
            return $myPage = getImagePage($forPage);
           
        }else if( get_class($myPage) == 'ProcessWire\Page'){
            
            return $myPage;
        }
    }
}
$out = '';
$imagePage = getImagePage($page);
//var_dump($imagePage->name);
$files = $imagePage->files;
$videoTag = '';
$videoValid = false;
$styles = '';

if($files){
    if(isset($file)){
        $file = $files->get($file); // we need the file object not the file name
        if(isset($width)) $styles = 'width: '.$width.'; max-width: 100%; height: auto;'; 
        else $styles = 'width: 100%; height: auto;';
        if($file){
            $videoValid = true;
            $videoTag .= '
            <video style="'.$styles.'" preload="preload" playsinline="" controls="" uk-video="autoplay: false" aria-hidden="false">
                <source src="'.$file->url.'" type="video/mp4">
                Ihr Browser unterstützt kein Video Tag
            </video>
            ';
            $out = $videoTag;
        }
    }
    if(!$videoValid){
        $out .= "<p>Gib einen Dateinamen an. Dateien auf dieser Seite:.</p>";
        foreach($files as $file){
            $out .= '<p>[[video file="'.$file->name.'"]]</p>';
        }
    }
}else{
   $out .= '<p>Keine Dateie auf der Seite gefunden</p>'; 
}

echo $out;

Heading

Genbänkle

Attributes

tag=h2
heading
<?php
echo($heading);
?>

Next Events

Genbänkle

<?php
$today = strtotime(date('Y-m-d'));
$eventPage = $pages->get('/termine/');
$mySelector = "template=event,limit=2,sort=event_date,event_date>=$today";
$events = $pages->find($mySelector);
$eventListMarkup = '';

foreach($events as $event){
    $eventListMarkup .= '<div class="date uk-text-muted">'.$event->event_date.'</div>';
    $eventListMarkup .= '<p><a href="'.$event->url().'">'.$event->title.'</a></p>';    
}

echo $eventListMarkup;

Childnavigation

<?php namespace ProcessWire;
$out = '';
($attr['parent']) ? $parent = $pages->get($attr['parent']) : $parent = $page;
$children = $parent->children();
$out .= '<ul class="childnav">';
foreach ($children as $child){
    $out .= '<li class="nav-item"><a class="nav-link" href="'.$child->url.'">'. $child->title . '</a></li>';
}
$out .= '</ul>';
echo $out;

Social Media Icons über Repeater

<?php namespace ProcessWire;

// SOCIALMEDIA
$socialmedia = '';
$p = $pages->get('/site-configuration/');
//bd($p->r_socialicons);
foreach($p->r_socialicons as $item){
    if($item->link){
	    $socialmedia .= '<li><a href="'.$item->link.'" target="_blank" description="'.$item->text.'"><img src="'.$item->single_image->width(48)->url.'" uk-tooltip="'.$item->text.'" alt="'.$item->text.'" title="'.$item->text.'"></a></li>';
    }
}

return '<ul class="nav socialicons uk-flex uk-flex-center@m nmb">'.$socialmedia.'</ul>';

Repeater Matrix Item von beliebiger Seite rendern

lb pageid="2965" itemid="" list="1"

Attributes

pageid
itemid
list=1
<?php namespace ProcessWire;

bd($pageid,'page id');
bd($itemid,'item id');
$p = $pages->get($pageid);
$out ='';
if($list){
    // show list of items on the page

    bd($p);
    $out .= '<p>Selected Page: '.$p->title.'</p>';
    $out .= '<h4>Layout Blocks</h4>';
    $out .= '<ul>';
    foreach($p->layout_blocks as $item){
        bd($item);
        $out .= '<li>'.$item->headline.' <br>[[lb pageid='.$p->id.' itemid='.$item->id.' list=0]] </li>';
    }
    $out .= '</ul>';
    $out .= '<p>Next step: Choose a item id, set param itemid equal to this id and set list parameter to 0 to render the selected item in place.</p>';
}else if($pageid && $itemid){
    if($p){
        $item = $p->layout_blocks->findOne("id=".$itemid);
        $item ? $out .= $item->render(): $out .= '<p class="uk-alert">Invalid Item ID</p>';
    }else{
        $out .= '<p class="uk-alert">Invalid Page<p>';
    }
}else{
    $out .= '<p class="uk-alert">To use this Hanna Code you need the parameters pageid (see admin page tree) and itemid. To tet the available item ids you can set the parameter list=1.</p>';
}
echo $out;

Content einer beliebigen Seite rendern

Hier am Beispiel einer Seite die Charts rendert (Template chart).

chart pageid=1234
<?php namespace ProcessWire;
// if in repeater or repeaterMatrix find the first parent ProcessWire\Page Page
if (!function_exists('getParentPage')) {
    function getParentPage($myPage, $level=1){
        $maxLevel=3;
        if( get_class($myPage) == 'ProcessWire\RepeaterMatrixPage' || get_class($myPage) == 'ProcessWire\RepeaterPage') {
            $level +=1;
            if($level > $maxLevel) return false;
            $forPage = $myPage->getForPage();
            //var_dump( $myPage->getParentPage());
            //echo("<p>next Level: ".get_class($forPage)."</p>");
            return $myPage = getParentPage($forPage);
        }else if( get_class($myPage) == 'ProcessWire\Page'){  
            return $myPage;
        }
    }
}

$parent = getParentPage($page);
$parent->hasChart = true;
$chart = $pages->get($pageid); // this page contains our content. You should ommit appendFile in it's template because we don't want <head> etc. 
$out ='';
$out .= $chart->render();

echo $out;