Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sections in inner most template #143

Closed
ornellast opened this issue Feb 3, 2017 · 6 comments
Closed

Add sections in inner most template #143

ornellast opened this issue Feb 3, 2017 · 6 comments

Comments

@ornellast
Copy link

I'm starting with Plates. I tried to add a content to a section in second level template and it was lost when the view was rendered. I "fixed" this passing the first level template ($this) as a variable to de next one ($parentTemplate).
list.phtml

<?php $this->layout('default::layout', ['pageTitle' => 'Home']); ?>
<div class="ui vertical container segment">
    ...
</div>
<?= $this->insert("default::modal", [
   'id' => 'form-filter',
   'header' => 'Filtar Rela&ccedil;&atilde;o',
   'content' => $content,
   'actions' => $actions,
   'parentTemplate' => $this
]); ?>

modal.phtml

<div id="<?= isset($id) ? $id : 'default-modal' ?>" class="ui modal">
...
</div>
<?php $parentTemplate->push('inlinejs') ?>
<script type="text/javascript">
    var modalId = <?= isset($id) ? "'" . $id . "'" : 'default-modal' ?>;
    $('#' + modalId).modal({
        inverted: true,
        onApprove: function () {
            alert($('#' + modalId + ' form').serialize());
        },
    });
</script>
<?php $parentTemplate->end(); ?>

Is there a better way to do this? This is a limitation?

@rogerroelofs
Copy link

Change
<?= $this->insert("default::modal", [
to
<?php $this->insert("default::modal", [

You don't need to echo the results of insert.

@ornellast
Copy link
Author

Thank you, @rogerroelofs, but didn't work, echoing or not. I got the same result: the section content, added in a inner template, is lost.

Looking in Template.php, I found:.

            if (isset($this->layoutName)) {
                $layout = $this->engine->make($this->layoutName);
                $layout->sections = array_merge($this->sections, array('content' => $content));
                $content = $layout->render($this->layoutData);
            }

Here, the $this->layoutName is empty, when in inner level template, then the $sections aren't merged. Would be a bug?

@rogerroelofs
Copy link

So are you not using a layout?

I do this kind of thing all the time
<?php $this->insert('partials/box', array( 'title' => 'Company/Campus', 'body' => $this->section('box') )) ?>
but all my templates start with something like this:
<?php $this->layout('admin-bootstrap', array( 'section' => 'section-2', 'title' => $client->Name )); ?>

@ornellast
Copy link
Author

ornellast commented Feb 3, 2017

Once again, thank you @rogerroelofs
Let me show you my layout:

 __________________________________________
|   Master template (layout.phtml)         |
|   _____________________________________  |
|  |   Content template (list.phtml)    |  |
|  |   ______________________________   |  |
|  |  | Modal template (modal.phtml) |  |  |
|  |  |                              |  |  |
|  |  |______________________________|  |  |
|  |____________________________________|  |
|__________________________________________|

Master template excerpt:

<!DOCTYPE html>
<html>
    <head>
        <?= $this->section('stylesheets') ?>
    </head>
    <body>
        <?= $this->section('content') ?>
        <?= $this->section('jsfiles') ?>
        <?= $this->section('inlinejs') ?>
    </body>
</html>

Content template excerpt

<?php $this->layout('default::layout', ['pageTitle' => 'Home']); ?>
<div class="ui vertical container segment">
    ...
</div>
<?php $this->insert("default::modal", [
   'id' => 'form-filter',
   'header' => 'Filtar Rela&ccedil;&atilde;o',
   'content' => $content,
   'actions' => $actions
]); ?>

Modal template excerpt

<div id="<?= isset($id) ? $id : 'default-modal' ?>" class="ui modal">
...
</div>
<?php $this->push('inlinejs') ?>
<script type="text/javascript">
    var modalId = <?= isset($id) ? "'" . $id . "'" : 'default-modal' ?>;
    $('#' + modalId).modal({
        inverted: true,
        onApprove: function () {
            alert($('#' + modalId + ' form').serialize());
        },
    });
</script>
<?php $this->end(); ?>

In this case, why do I need to write <?php $this->layout('default::layout', ['pageTitle' => 'Home']); ?> in modal template again?
IMHO, it seems like a repetitive task. Is there no other way?

@rogerroelofs
Copy link

Sorry for the less than helpful reply. It appears that you can't use start() or push() in an inserted template without some kind of work around. See #47

In your case I would have done the js in a more generic way so you don't have to insert special script for each modal. Instead you can do something like this to operate on all modals of that type.

$('.modal.some_class').modal({
  inverted: true,
  onApprove: function() {
      alert($(this).find('form').serialize();
  }
});

@ragboyjr
Copy link
Contributor

Closing in favor of #169. Thanks!

@ragboyjr ragboyjr mentioned this issue Nov 29, 2017
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants