thephpleague / plates

Native PHP template system
https://platesphp.com
MIT License
1.48k stars 180 forks source link

Unable to call a portion of the template using platesphp #155

Closed manishbathla closed 7 years ago

manishbathla commented 7 years ago

Using platesphp template engine I need to call a portion(search box) of the template in a controller.

If you have worked with Codeigniter then what I am looking is for an equivalent of $this->load->view() like the one below in phpplates

$this->load->view('common/header');
$this->load->view('nav/top_nav');
$this->load->view('create/create', $page_data);
$this->load->view('common/footer');

I have tried a few syntax of the platesphp but nothing seems to work.

If you have used plates, can you tell me.

roshinc commented 7 years ago

You are probably looking for sections

manishbathla commented 7 years ago

I need to call the search bar in the HomeController somewhat like this if this was codeigniter

class HomeController extends \system\core\BaseController
{
    public function Index()
    {
        $data['page_title'] = 'xyz.com';
        $data['page_description'] = 'page description here';
        $data['page_keywords'] = 'keyword1, keyword2';

        /***** This is where I call the Search ******/
        $this->load->view('common/Search');

        // And then pass $data and render template
        echo $this->template->render('common/home', $data);
    }
}

How do i do that with section in plates. Can you come with a small example?

roshinc commented 7 years ago

Have not tried the code below,

class HomeController extends \system\core\BaseController
{
    public function Index()
    {
        echo $templates->render('page-content::index_content',
            [
                'page_title' => 'xyz.com',
                'page_description' => 'page description here',
                'page_keywords' => 'keyword1, keyword2'
            ]);
    }
}

index_content.php

<?php $this->start('main-content') ?>
<p>
//Content 
</p>
<?php $this->stop() ?>

// I only show follow-thru with page title, you can do the others the same 
// Since they seem like static content you probably can introduce them in 
// this file and can avoid passing thru data  
<?php $this->layout('main-template', ['page_title' => $this->e($page_title)]) ?>

main-template.php

<head>
  <title><?=$this->e($page_title)?></title>
</head>
<body>
  <div id="main">

    <!--INSERT Search -->
    <?php $this->insert('common::common-search') ?>
    <div id="content">
    <?php if ($this->section('main-content')): ?>
    <?=$this->section('main-content')?>
    <?php endif ?>
    </div>
  </div>
</body>