xeoncross / PHP-Template

Simple PHP template inheritance and view management class using pure PHP
28 stars 4 forks source link

Handle minified CSS/JS #4

Open raxan opened 9 years ago

raxan commented 9 years ago

Hi

I hope you can give me some tips to solve this issue.

Currently I have the main layout something like this:


<!DOCTYPE html>
<html class="no-js" lang="<?= $lang ?>">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title><?= $_meta['title'] ?></title>
  <meta name="description" content="<?= $_meta['description'] ?>">
  <meta name="csrf-token" content="<?= $_csrfToken ?>">
  <link rel="icon" type="image/png" href="<?= $base_root ?>/assets/img/favicon.png">
  <link rel="shortcut icon" href="<?= $base_root ?>/favicon.ico">
  <?= $this->styles(['main']) ?>
  <?= $this->block('css') ?>
</head>
<body>
<?= $this->load('part/header') ?>
<div id="main">
  <?= $this->block('content') ?>
</div>
<?= $this->load('part/footer') ?>
<?= $this->javascripts("jquery") ?>
<?= $this->block('javascript') ?>
</body>
</html>

And an example of a view which extend that layout is:

<?php $this->start() ?>
<section>
This is content
</section>
<?php $this->end('content') ?>

<?php $this->start() ?>
<!-- Add custom.css style -->
<?= $this->styles('custom') ?>
<?php $this->end('css') ?>

<?php $this->start() ?>
<!-- Add application.js script -->
<?= $this->javascripts("application") ?>
<script type="text/javascript">
jQuery(document).ready(function() {
  Application.run();
});
</script>
<?php $this->end('javascript') ?>

<?php $this->extend('layout') ?>

Note $this->styles() and $this->javascripts() are custom method added to Template class, which receive an array of css or javascript filename without extension, and return a string of html tag link for css or script for js.

Everything is ok and work fine.

I have create this method, because I think to easy create 1 minified css / js file using 1 method to add css/js, because I can add an option on that methods and check if enabled compress css/js then don't return html tag for each file, but only 1 file and cache this on file system.

But since the nature of theme, what happen?

Firstly the template seem render header of layout, and then main content where there is more css/js file which require to be load, but I can't add anymore to 1 file because is already rendered.

Please let me know if maybe is not clear my explaination.

Thanks!!

xeoncross commented 9 years ago

I'm not sure I understand.

The child view is the first to set the value of the javascript block which means, (unless you set the $append flag to true) the parent values will be overwritten. Change it to this:

<?php $this->end('javascript', true) ?>