Last active
October 20, 2017 10:31
-
-
Save bigin/bee6e92ccf49963cfdbb8464bcd54f4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Renders the current page content involving the HTML | |
* markup defined in the chunk field. | |
* | |
* NOTE: If you have multiple categories with different | |
* fields, you'll need to extend the function accordingly. | |
* Or create a separate rendering function for each category. | |
* | |
* @param $slug - The page slug that should be rendered | |
* | |
*/ | |
function getContent($slug) { | |
// Your chunk field name (default: markup) | |
$chunk_name = 'markup'; | |
// Your sidebar field name (default: sidebar) | |
$sidebar_name = 'sidebar'; | |
// Your file field name (default: images) | |
$image_name = 'images'; | |
// Let's go and find our item which belongs to the page | |
$item = imanager()->getItemMapper()->findItem('name='.$slug); | |
// Check if an item is assigned to the page | |
if(empty($item)) return 'This page has no item assigned to it!'; | |
// Get the page content | |
$content = returnPageContent($slug); | |
// Get page title | |
$title = get_page_title(false); | |
// Now, replace the image placeholders in the page content | |
$content = placeImages($item->fields->$image_name, $content); | |
// Ok, now we assemble the page output | |
return imanager()->getTemplateEngine()->render( | |
$item->fields->$chunk_name->value, | |
array( | |
'page_title' => $title, | |
'page_content' => $content, | |
'page_sidebar' => $item->fields->$sidebar_name->value | |
) | |
); | |
} | |
/** | |
* The function loops throught all images and replaces the | |
* placeholders of the page content with real image tags. | |
* | |
* @param $images | |
* @param $content | |
* | |
* @return mixed | |
*/ | |
function placeImages($images, $content) { | |
$engine = imanager()->getTemplateEngine(); | |
$i = 0; | |
foreach($images->fullurl as $fullurl) { | |
$content = $engine->render($content, | |
array( | |
'image_'.($i++) => '<img alt="" src="'.$fullurl.'">' | |
) | |
); | |
} | |
return $content; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment