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 a default view with layout that can be extends by view in module #58

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

Frankva
Copy link
Contributor

@Frankva Frankva commented Jul 13, 2023

Add View Layout

Instead of editing display_view that cause incompatibility with other site (#57), add a default view that use layout (https://www.codeigniter.com/user_guide/outgoing/view_layouts.html) and must be extended by the views in modules. This change permits to use the view function of CodeIgniter that return a string and permits to have a view with a different header without rewrite all the head.

Example

default.php

The default view with renderSection that will be replaced by what you want when you extend this view. The default view is like an abstract class, it is not supposed to be used without extends

<!DOCTYPE html>
<html lang="fr">
<head>
    <?= view('Common\Views\head_content', $this->data) ?>
    <?= $this->renderSection('head') ?>
</head>
<body>
    <?php if (ENVIRONMENT != 'production'): ?>
        <div class="alert alert-warning text-center">CodeIgniter environment variable is set to
        <?=esc(strtoupper(ENVIRONMENT))?> You can change it in .env file.</div>
    <?php endif ?>
    <?= view('Common\Views\login_bar', $this->data) ?>
    <?php foreach (config('Common\Config\AdminPanelConfig')->tabs as $tab) {
            if (strstr(current_url(),$tab['pageLink'])) {
                $textView .= view('\Common\adminMenu');
            }
        }
    ?>
    <?= $this->renderSection('content') ?>
<?= $this->renderSection('javascript') ?>
</body>
</html>

Note

I put the content of the head in head_content.php to have the same thing between the current header.php and this default view without duplicate the code.

edit_planning.php

The edit planning view extends the default view and defines a section that will be inserted in the default view. There are no error if all sections are not be defined, it is just replaced by nothing.

<?= $this->extend('Common\Views\default') ?>
<?= $this->section('head') ?>
    <?= view('Timbreuse\Views\planning\edit_planning_style') ?>
<?= $this->endSection() ?>
<?= $this->section('content') ?>
<section class="container">
    <h3><?= esc($h3title) ?></h3>
    <?php if (! empty(service('validation')->getErrors())) : ?>
        <div class="alert alert-danger" role="alert">
            <?= service('validation')->listErrors() ?>
        </div>
    <?php endif ?>
    <form class="grid-container" method="post">
        <!-- cut because not important for example -->
    </form>
    <details>
         <!-- cut because not important for example -->
    </details>
</section>
<?= $this->endSection() ?>
<?= $this->section('javascript') ?>
    <?= view('Timbreuse\Views\planning\edit_planning_script') ?>
<?= $this->endSection() ?>

Note

  • I put the content of CSS style in edit_planning_style.php. I don’t use link and CSS file because CSS file must be in the public directory and that avoid having an independent module. This file begins with <style> and ends with </style>.
  • I put the content of JavaScript script in edit_planning_script.php. I don’t use link and JS file because JS file must be in the public directory and that avoid having an independent module. This file begins with <script> and ends with </script>.

Call

Use the view function of CodeIgniter.

        return view('Timbreuse\Views\planning\edit_planning.php', $data);

All part will be renderer.
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant