Who faced with a similar need(or just a task), please help.Thank you.

p.s.
The task: to write an analog to the standard classes and functions for connecting external files(js, css etc).

Here's the code.
<?php

namespace app \ components;

use yii \ di \ Container;

class FA {
    public static $jss=[];
    public static $styles=[];
    public static $raw_styles=[];
    public static $raw_jss=[];

    /* public function __construct() {
        $this->jss=new Container();
    } * /

    public static function index($array=[]) {
        if(empty($array)) {
            return false;
        }
        if($array[0] ==='js') {
            static :: $jss[]=$array;
        } elseif($array[0] ==='css') {
            static :: $styles[]=$array;
        } else {
            return false;
        }
        return true;
    }

    public static function index2($array=[]) {
        if(empty($array)) {
            return false;
        }

        $count=count(static :: $jss);
        $count ++;
        foreach($array as $a) {
            if($a[0] ==='js') {
                static :: $jss[$count][]=$a;
            } elseif($a[0] ==='css') {
                static :: $styles[]=$a;
            } else {
                return false;
            }
        }

        return true;
    }

    public static function head() {
        foreach(static :: $styles as $style) {
            if($style[1] ==='head') {
                echo"<link type='text/css'rel='stylesheet'href='{$style[2]}'>\ r \ n \ t";
            }
        }
        foreach(static :: $jss as $js) {
            if($js[1] ==='head') {
                echo"<script type='text/javascript'src='{$js[2]}'></script>\ r \ n \ t";
            }
        }
    }

    public static function body() {
        foreach(static :: $jss as $js) {
            if($js[1] ==='body') {
                echo"<script type='text/javascript'src='{$js[2]}'></script>\ r \ n \ t";
            }
        }
        $code=";
        $code_ready="';
        foreach(static :: $raw_jss as $raw_js) {
            if($raw_js[1] ==='body') {
                if($raw_js[2] === true) {
                    $code_ready.=$raw_js[0]."\ r \ n";
                } else {
                    $code.=$raw_js[0]."\ r \ n";
                }
            }
        }
        if($code_ready! =='') {
            echo"<script type='text/javascript'>$(document).ready(function() {\ r \ n \ t";
            echo $code_ready;
            echo"\ t});\ r \ n \ t</script>\ r \ n";
        }
        if($code! =='') {
            echo"<script type='text/javascript'>\ r \ n";
            echo $code;
            echo"\ t</script>\ r \ n";
        }
    }

    public static function body2() {
        $jss=static :: $jss;
        $jss=array_reverse($jss);
        foreach($jss as $js) {
            foreach($js as $j) {
                if($j[1] ==='body') {
                    echo"<script type='text/javascript'src='{$j[2]}'></script>\ r \ n \ t";
                }
            }
        }
        $code=";
        $code_ready="';
        foreach(static :: $raw_jss as $raw_js) {
            if($raw_js[1] ==='body') {
                if($raw_js[2] === true) {
                    $code_ready.=$raw_js[0]."\ r \ n";
                } else {
                    $code.=$raw_js[0]."\ r \ n";
                }
            }
        }
        if($code_ready! =='') {
            echo"<script type='text/javascript'>$(document).ready(function() {\ r \ n \ t";
            echo $code_ready;
            echo"\ t});\ r \ n \ t</script>\ r \ n";
        }
        if($code! =='') {
            echo"<script type='text/javascript'>\ r \ n";
            echo $code;
            echo"\ t</script>\ r \ n";
        }
    }

    public static function jscode($code, $place='body', $ready=true) {
        if($code! == null&&gettype($code) ==='string'&&$code! =='') {
            static :: $raw_jss[]=[$code, $place, $ready];
        } else {
            return false;
        }
        return true;
    }

    public static function csscode($code) {
        if($code! == null&&gettype($code) ==='string'&&$code! =='') {
            static :: $raw_styles[]=$code;
        } else {
            return false;
        }
        return true;
    }
}
  • Good morning. You would normally explain why you need it and what you are trying to achieve. There, you see, and there will be a solution. – Fabulous60 Oct 30 '17 at 12:08
  • Fabulous60, Take it as a task. I understand that if you explain the original problem, then there will be a better solution. But I want to solve it as planned (by defining view this or layout). – Fantastic Finch Oct 30 '17 at 12:22
  • I understand that if you explain the original problem, then there is a better solution.

    Fantastic Finch, do you want to invent another"bicycle"? Maybe you still need to explain the original problem and apply the only correct solution?
    – Fabulous60 Oct 30 '17 at 13:02
  • Fabulous60, Ok. The task is to write an analogue to the standard classes and functions for connecting external files (js, css etc) – Fantastic Finch Oct 30 '17 at 13:34
  • Fantastic Finch from the point of view of yii2, layout is also a view. A view in which other views are rendered, a parent view, if you like. Why do you need to distinguish them then:) – Dwarven Trailer Oct 30 '17 at 13:37
  • Fabulous60, Already have a class that does everything. But there are problems with the order of output connected from different sources (view, layout). And while the solution seems only with a multidimensional array – Fantastic Finch Oct 30 '17 at 13:38
  • Dwarven Trailer, I know (read the question). distinguish required because it is still different files – Fantastic Finch Oct 30 '17 at 13:39
  • Dwarven Trailer, I think I’ll just have to take it and put up with the fact that the usual view is first rendered, and then the layout (which is the same view, but we don’t need to repeat it, ok?). – Fantastic Finch Oct 30 '17 at 13:40
  • Fantastic Finch files are different, but of the same type. If you want to process them differently, just make another stack for rendering, and you will have separate views separately of the layouts. What you put on the stack of layouts will be considered layouts. – Dwarven Trailer Oct 30 '17 at 13:41
  • Dwarven Trailer, in principle, as an alternative, I will also consider. Thank you for offering practical items. – Fantastic Finch Oct 30 '17 at 13:43
  • here is my indexing method
    public static function index2 ($array=[]) {if (empty ($array)) {return false;} $count=count (static :: $jss);$count ++;foreach ($array as $a) {if ($a [0] ==='js') {static :: $jss [$count] []=$a;} elseif ($a [0] ==='css') {static :: $styles []=$a;} else {return false;}} return true;}
    – Fantastic Finch Oct 30 '17 at 13:44
  • so i use it
    FA :: index2 ([['js','body','/somepath/somescript.js'], ['js','body','/somepath /somescript.js'],]);
    – Fantastic Finch Oct 30 '17 at 13:46
  • Here is the output method
    public static function body2() {$jss=static :: $jss;$jss=array_reverse ($jss);foreach ($jss as $js) {foreach ($js as $j) {if ($j [1] ==='body') {echo"<script type='text/javascript'src='{$j [2]}'></script>\ r \ n \ t";}}} $code='';$code_ready="';foreach (static :: $raw_jss as $raw_js) {if ($raw_js [1] ==='body') {if ($raw_js [2] === true) {$code_ready.=$raw_js [0]."\ r \ n";} else {$code.=$raw_js [0]."\ r \ n";}}} if ($code_ready! =='') {echo"<script type='text/javascript'> $(document) .ready (function() {\ r \ n \ t";echo $code_ready ;echo"\ t});\ r \ n \ t </script>\ r \ n";} if ($code! =='') {echo"<script type='text/javascript'>\ r \ n";echo $code;echo"\ t </script>\ r \ n";}}
    – Fantastic Finch Oct 30 '17 at 13:46
  • Here is the whole gov-bike

    <?phpnamespace app \ components;use yii \ di \ Container;class FA {public static $jss=[];public static $styles=[];public static $raw_styles=[];public static $raw_jss=[];/* public function __construct() {$this->jss=new Container();} */public static function index ($array=[]) {if (empty ($array)) {return false;} if ($array [0] ==='js') {static :: $jss []=$array;} elseif ($array [0] ==='css') {static :: $styles []=$array;} else {return false;} return true;} public static function index2 ($array=[]) {if (empty ($array)) {return false;} $count=count (static :: $jss);$count ++;foreach ($array as $a) {if ($a [0] ==='js') {static :: $jss [$count] []=$a;} elseif ($a [0] ==='css') {static :: $styles []=$a;} else {return false;}} return true;} public static function head() {foreach (static :: $styles as $style) {if ($style [1] ==='head') {echo"<link type='text/css'rel ='stylesheet'href ='{$style [2]}'>\ r \ n \ t";}} foreach (static :: $jss as $js) {if ($js [1] ==='head') {echo"<script type='text/javascript'src='{$js [2] }'></script>\ r \ n \ t";}}} public static function body() {foreach (static :: $jss as $js) {if ($js [1] ==='body') {echo"<script type='text/javascript'src='{$js [2]}'></script>\ r \ n \ t";}} $code='';$code_ready="';foreach (static :: $raw_jss as $raw_js) {if ($raw_js [1] ==='body') {if ($raw_js [2] === true) {$code_ready.=$raw_js [0]."\ r \ n";} else {$code.=$raw_js [0]."\ r \ n";}}} if ($code_ready! =='') {echo"<script type='text/javascript'>$(document) .ready (function() {\ r \ n \ t";echo $code_ready ;echo"\ t});\ r \ n \ t </script>\ r \ n";} if ($code! =='') {echo"<script type='text/javascript'>\ r \ n";echo $code;echo"\ t </script>\ r \ n";}} public static function body2() {$jss=static :: $jss;$jss=array_reverse ($jss);foreach ($jss as $js) {foreach ($js as $j) {if ($j [1] ==='body') {echo"<script type='text/javascript'src='{$j [2]}'></script>\ r \ n \ t";}}} $code='';$code_ready="';foreach (static :: $raw_jss as $raw_js) {if ($raw_js [1] ==='body') {if ($raw_js [2] === true) {$code_ready.=$raw_js [0]."\ r \ n";} else {$code.=$raw_js [0]."\ r \ n";}}} if ($code_ready! =='') {echo"<script type='text/javascript'> $(document) .ready (function() {\ r \ n \ t";echo $code_ready ;echo"\ t});\ r \ n \ t </script>\ r \ n";} if ($code! =='') {echo"<script type='text/javascript'>\ r \ n";echo $code;echo"\ t </script>\ r \ n";}} public static function jscode ($code, $place='body', $ready=true) {if ($code! == null &gettype ($code) ==='string'&&$code! =='') {static :: $raw_jss []=[$code, $place, $ready];} else {return false;} return true;} public static function csscode ($code) {if ($code! == null &gettype ($code) ==='string'&$code! =='') {static :: $raw_styles []=$code;} else {return false;} return true;}}
    – Fantastic Finch Oct 30 '17 at 13:47
  • Already have a class that does everything.

    Fantastic Finch, so where is the code for this class? Where is the description of your task in your own question?
    Do you suggest guessing at the tea leaves?
    – Fabulous60 Oct 30 '17 at 13:49
  • Fabulous60, code laid out above – Fantastic Finch Oct 30 '17 at 13:51
  • Fantastic Finch, the question should be added, and not in response to the comment !!!
    And to add your own question with the description of the task !!!
    – Fabulous60 Oct 30 '17 at 14:16
  • Fabulous60, I will consider for the future (or in the future) – Fantastic Finch Oct 30 '17 at 14:16
  • Fantastic Finch, in the controller you specify which layout you want to use, and in the layout itself you just plug in the required set via AssetBundle.
    Why invent something?
    – Fabulous60 Oct 30 '17 at 14:24
  • Fabulous60, the challenge is to invent – Fantastic Finch Oct 30 '17 at 15:34
  • Fabulous60 when I hear from someone"Why invent something?", I want to say"Seriously ?!". – Fantastic Finch Oct 30 '17 at 15:35
  • But there are problems with the order of output connected from different sources

    Fantastic Finch, what sources do you mean?
    – Fabulous60 Oct 30 '17 at 15:50
  • Fabulous60, I mean different files – Fantastic Finch Oct 30 '17 at 17:42
  • Fantastic Finch, different js, css files? – Fabulous60 Oct 30 '17 at 19:11
  • Fabulous60, different files view (layout, view) – Fantastic Finch Oct 30 '17 at 19:34
  • Fantastic Finch, but what's the point? – Fabulous60 Oct 30 '17 at 20:00

1 Answers 1

if you're talking about Yii2, then the additional variable $content is passed to the default layout.
In yii1, in view $this refers to the controller that renders this view, but not to the layout.