Jump to content

render a partial with $otherPage->render


Alex Parra
 Share

Recommended Posts

Hello everyone.

I've found PW recently and I've been exploring/testing it...

The everything is a Page is slowly sinking in.

I'm currently trying to render a page in another (like a partial) like below:

$shop = $pages->get('/shops/mr-big');
$mainContent = $shop->render('partials/shop-intro.php');

I also have set an $config->appendTemplateFile.

How can I prevent the appendFile to be included for this partial rendering.

I've search the forums and docs but could not find an answer...

Thank you

Link to comment
Share on other sites

Hello LostKobrakai,

That worked!

Thinking some method of doing this should exist.

Is this Module playfield or am I doing it wrong and there are more appropriate ways?

My goal is to render a "snippet" of an existing page.

Think latest news intro or blog list post intro..

Thanks for your reply.

Link to comment
Share on other sites

I you're searching for a more flexible way to include snippets / not always whole sites, maybe this is a more appropriate function:

// returns the output of snippets/contactform.php
// the second argument is optional and will be passed to 
// the called file as multiple variables with the name of the key
// also all api varables are available by default

wireRenderFile("snippets/contactform", array("stuff" => $something ));
  • Like 4
Link to comment
Share on other sites

Ah! That looks much better... complete code below

/**
 * site/config.php
 */
$config->appendTemplateFile = '_layout.php';
/**
 * site/templates/home.php
 */
$title = $page->title;
$shop = $pages->get('/shops/mr-big');
$mainContent = $shop->render('partials/shop-intro.php', ['appendFile'=>null]);
/**
 * site/templates/partials/shop-intro.php
 */
<div class="shop-intro">
<h2><?= $page->title ?></h2>
<?php foreach( $page->shop_photos AS $photo ) : ?>
  <img src="<?= $photo->url ?>" /><br/>
<?php endforeach; ?>
<div class="shop-desc">
  <?= html_entity_decode($page->shop_description) ?>
</div>
</div>
/**
 * site/templates/_layout.php
 */
<!DOCTYPE html>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title><?= $title; ?></title>
<link rel="stylesheet" type="text/css" href="styles.css" />
</head>
<body>
<h1><?= $title; ?></h1>
<div class="container">
<?= $mainContent ?>
</div>
</body>
</html>
 

I like it like this...

If anyone has a better way, feel free to jump in.

  • Like 2
Link to comment
Share on other sites

thanks for wireRenderFile

until now i was doing this like it is done in the blog profile: https://github.com/ryancramerdesign/BlogProfile/blob/cca31ebb7ec10f7614044156161abe3042abbbed/templates/categories.php#L20

drawback was, that the api variables where not directly accessible in the template file ( wire('page') instead of $page ). is there any reason why the blog profile uses a different approach?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...