pok4 0 Newbie Poster

Hello guys,
I'm using mustache php template engine with classes. My system is extendable via extensions which is injected in basecontroller construct method...
The extensions is working correctly, but in them i have html and js... I want to move html/js in external files but i need to works with the extension. (ajax/post/get etc...)

Can you give me a advice ?
I have event dispatcher too (for the extensions)

https://github.com/igorw/evenement
https://github.com/bobthecow/mustache.php

Here is example of one of my extensions:

<?php
/**
 * ../../ext/pok4/email_subscribe/ext.php
 *
 * @package default
 */


if (count(get_included_files()) == 1) exit("Direct access not permitted."); //Don't edit
//Your Extension Script
class Email_Subscribe extends \App\Controllers\BaseController  {



    /**
     *
     */
    public function __construct() {
        parent::__construct();

        $this->ext_html_position = 'up'; //up or down is valid value for this, you can place it div on top or bottom of your site

        //LANGUAGE SETTER
        if (get_current_language() == 'bg') {
            $this->lang= array_merge($this->lang, [
                    'ext_email_subscribe_welcome'=>'Здравей! Можеш да се абонираш за нашите новини като напишеш емейла си по-долу',
                    'ext_email_subscribe_button'=>'Абонирам се',
                    'ext_email_subscribe_enter'=>'Въведи емейл',
                    'ext_email_subscribe_denied'=>'Отказвам се',
                ]);
        }
        if (get_current_language() == 'en') {
            $this->lang= array_merge($this->lang, [
                    'ext_email_subscribe_welcome'=>'Hello! You can subscribe for our news, just type your email below',
                    'ext_email_subscribe_button'=>'I subscribe',
                    'ext_email_subscribe_enter'=>'Write your email',
                    'ext_email_subscribe_denied'=>'Refuse',
                ]);
        }
        if (get_current_language() == 'ru') {
            $this->lang= array_merge($this->lang, [
                    'ext_email_subscribe_welcome'=>'Привет! Вы можете подписаться на наши новости, написав свой адрес электронной почты ниже',
                    'ext_email_subscribe_button'=>'Подписаться',
                    'ext_email_subscribe_enter'=>'Введите адрес электронной почты',
                    'ext_email_subscribe_denied'=>'Я отказываюсь',
                ]);
        }
        if (get_current_language() == 'es') {
            $this->lang= array_merge($this->lang, [
                    'ext_email_subscribe_welcome'=>'¡Hola! Puede suscribirse a nuestras noticias, simplemente escriba su correo electrónico a continuación',
                    'ext_email_subscribe_button'=>'Yo suscribo',
                    'ext_email_subscribe_enter'=>'Escribe tu correo electrónico',
                    'ext_email_subscribe_denied'=>'Rehusar',
                ]);
        }
        //debug
        //var_dump($this->lang['ext_email_subscribe_welcome']);
        //var_dump($this->lang['ext_email_subscribe_button']);



    }



    /**
     *
     */
    public function load() {


        if ($_SERVER['REQUEST_URI'] == '/' && (!isset($_COOKIE['argos_mail_subscribe_denied']) || !isset($_COOKIE['argos_email_subscribe_ok']))) {
            $this->dispatcher->emit('core_event_head_append', ['<link rel="stylesheet" href="ext/pok4/email_subscribe/css/style.css">']);

            if(get_style() == 'orizon' || get_style()== 'revelio') {
                $this->dispatcher->emit('core_event_head_append', ['<link rel="stylesheet" href="ext/pok4/email_subscribe/css/past.css">']);
            }

            $format_html_for_js = preg_replace('~>\s+<~', '><', addslashes(
                    '<section class="home-newsletter">
                    <div class="container" style="margin-top:0 !important">
                    <div class="row">
                    <div class="col-sm-12">
                        <div class="single">
                        <h2>'.$this->lang['ext_email_subscribe_welcome'].'</h2>
                        <form method="post">
                        <div class="input-group">
                             <input type="email"name="subscriber_email" class="form-control get_email_for_sub" placeholder="'.$this->lang['ext_email_subscribe_enter'].'">
                             <span class="input-group-btn">
                             <button class="btn btn-theme" name="send_email_sub" type="submit">'.$this->lang['ext_email_subscribe_button'].'</button>
                             </span>
                        </div>
                        </form>
                        </div>
                    </div>
                    </div>
                    </div>
                    <div class="denied_email_sub center-block text-center"><a href="#" style="text-decoration: none;"><button type="button" class="btn-close float-button-close" aria-label="Close"></button>&nbsp;<span style="color:red">'.$this->lang['ext_email_subscribe_denied'].'</span></a></div>
                    </section>'
                ));


            if ($this->ext_html_position =='up') {
                $send_div = 'prependTo("body");';
            } else {
                $send_div = 'appendTo("body");';
            }

            $this->dispatcher->emit('core_event_inside_head_ready_front', ['

             var cookie_check = getCookie("argos_email_subscribe_ok");
             var cookie_check2 = getCookie("argos_mail_subscribe_denied");
             if(cookie_check == null  && cookie_check2 == null) {
                 $("'.$format_html_for_js.'").'.$send_div.'

             } else {
                $(".home-newsletter").remove();
             }


             //Detect on click
             $( ".denied_email_sub" ).click(function() {
                  document.cookie=\'argos_mail_subscribe_denied=1;expires=Wed, 31 Oct 3099 08:50:17 GMT;path=/\';
                  $(".home-newsletter").remove();
             });

            ']);

            //submit form
            if (isset($_POST['send_email_sub'])) {
                $user_ip = $this->user_ip;
                $email = $_POST['subscriber_email'];

                //check if email exists
                $checker =  $this->db->prepare("SELECT * FROM ".$this->argos_db_prefix."emails WHERE email=?");
                $checker->bindParam(1, $email, PDO::PARAM_STR);
                $checker->execute();
                if ($checker->rowCount() < 1) {
                    //send data to db
                    $go = $this->db->prepare("INSERT INTO ".$this->argos_db_prefix."emails (`email`,`user_ip`) VALUES(?,?)");
                    $go->bindParam(1, $email, PDO::PARAM_STR);
                    $go->bindParam(2, $user_ip, PDO::PARAM_STR);
                    $go->execute();
                }
                //send cookie to user to prevent box showing
                setcookie('argos_email_subscribe_ok', '1', time()+60*60*24*365, '/');
            }


        }
    }


};




$load_ext = new Email_Subscribe;
$load_ext->load();