Using Plates in PHP

2.1k Views Asked by At

I'm new to using Plates template library for PHP. I have a few questions about how to use it.

I'm looking through the docs I don't see a way to set a global layout. Is there not a way to do that?

I'm using it inside of Codeigniter. Ideally, I'd like to set the layout in the MY_Controller file for most of the site and change when needed in a controller extending MY_Controller; for instance, setting the main site layout and then for the admin panel setting the admin layout in the Auth_Controller that all of the other admin controllers extend.

Changing the layout of a particular set of templates I'd have to go through and edit all of those files. This doesn't seem ideal. Or even just passing a sidebar data for a particular layout has to be done by passing the sidebar data to each template and from each template to the layout in every file. This seems very redundant. Am I missing something?

To clarify Plates is the template system/library, http://platesphp.com/

Example of what I'm talking about. The admin layout has a sidebar of all the admin URLs. This comes from a config file that's loaded and it has it's own template/view file.

I call the template and pass the data from the controller

// I created a library file that extends the Plates library so it can be easily loaded from the CI loader class
$data['sidebar_data'] = array(
    'navigation'=>'Navigation',
    'assets'=>'Assets',
    'config'=>'Config'
);
$data['controller_name'] => 'Users';
$this->plates->render('admin/whatever_page', $data);

Inside of the template file/current page

<?php $this->layout('layouts/admin', ['title' => $controller_name, 'sidebar_data' => $sidebar_data])
// stuff for this page
?>

Inside of the sidebar navigation template that gets loaded in the admin layout file

<ul class="menu vertical">
    <?php foreach($sidebar_data as $url => $label):?>
    <li>
        <a href="<?= '/admin/' . $url ?>"><?= $label ?></a>
    </li>
    <?php endforeach; ?>
</ul>

Then the layout

// there's a HEAD method in here that has the $title variable
<header class="row expanded collapse">
        <div class="column">
            <div class="top-bar">
                <div class="top-bar-left">
                    <ul class="menu">
                        <li>
                            <a href="/"><strong>Site Name</strong></a>
                        </li>
                    </ul>
                </div>
                <div class="top-bar-right">
                    <ul class="menu dropdown align-right" data-dropdown-menu>
                        <li>
                            <?= $current_user ?></a>
                        </li>
                        <li class="divider"> | </li>
                        <li>
                            <?= $this->insert('auth/logout_form') ?>
                        </li>
                    </ul>
                </div>
            </div>
        </div>
    </header>
    <section id="content">
        <div class="left-panel">
            <?= $this->insert('adminnav', ['sidebar_data' => $sidebar_data])?>
        </div>
        <div class="main-panel">
            <?= $this->section('content')?>
        </div>
    </section>
    <div id="adminmodal" class="reveal" data-reveal>
        <button class="close-button" type="button" data-close aria-label="Close modal">
            <span aria-hidden="true">&times;</span>
        </button>
        <div class="modal-body"></div>
    </div>
    <script type="text/javascript">$(document).foundation()</script>
    <?= $this->insert('_blocks/googleanalytics')?>

Having to pass the same data from every controller method to the controller's view/template then from that to the layout/other templates is very redundant.

1

There are 1 best solutions below

1
Vinicius Pereira de Oliveira On

Convert the array to json

for example: $this->plates->render('admin/whatever_page', ["data" => json_encode($data)]);