plates icon indicating copy to clipboard operation
plates copied to clipboard

Passing data to insert() inside layout()

Open zaxwebs opened this issue 1 year ago • 2 comments

Demo structure:

/layouts
    main.php
/partials
    sidebar.php
index.php

I "extend" layouts/main.php from index.php, how do I change something like a heading inside sidebar.php from index.php?

zaxwebs avatar Dec 12 '23 21:12 zaxwebs

Update: I'm currently passing data from index up to layouts/main & down to partials/sidebar in a similar fashion to the stacked layouts in the docs, which feels a bit like prop drilling. Wondering if that's the optimal way.

zaxwebs avatar Dec 12 '23 23:12 zaxwebs

Hey! I hope you were able to use plates in your project. Here's an example of how I'm using it. The home.php view calls the head.php file which is in the "html" folder in the root directory used by plates.

I'm just using $this->insert and $this->section See the example code:

// plates config
$templates = new Engine();
$templates->setDirectory($path);    
return $templates->render($this->page, $this->templateData);
    
// home.php
<?= $this->insert('_html/head', ['title' => 'page title' ]) ?>
<?= $this->insert('_html/head') ?>

// head.php
<?= $this->section("content") ?>
<!DOCTYPE html>
<title><?=$this->e($title)?></title>

t-hauck avatar Jun 24 '24 00:06 t-hauck