L
L
lihtenshtein2014-06-07 17:09:05
css
lihtenshtein, 2014-06-07 17:09:05

Are there such implementations in PHP when js, html, css, php are written in one file, and the engine separates them and spits them out to the user?

In general, this is the need. Imagine that we need to write a component for a project in php. This component renders, for example, a small div with information about the football team. This div contains static information, some interactive elements, for example, the "Subscribe to team news" button.
Accordingly, we would like to insert this component into the project in any of the project templates (suppose Smarty template engine is used) like this:
{SomeCMS::include_component('footbal_command_info', 'command_id:17');}
And the component itself is only one file somewhere in the project tree, which describes css, js, html layout, and php logic. When connecting this component, CMS extracts css, js, html from it, receives data from the database according to the logic described in php and gives the page to the client. And for the client, everything looks as if I gave him, among other things, three files:
footbal_command.js
footbal_command.css
index.html - in which somewhere deep there are blocks with football commands.
Are there such CMS, maybe libraries, or maybe someone knows what this approach is called. Any help will do!
MVC - ok, I know. But there is a need for such an approach.
Thank you.

Answer the question

In order to leave comments, you need to log in

4 answer(s)
S
Sergey, 2014-06-07
Protko @Fesor

This approach is called "shitcode".
Here's an example for you:
you write this in a php file (suddenly) css styles for example. Problems that may arise:
- extensibility. To change the styles, you need to go to some file and figure it out. At the same time, this file contains logic, and templates, and everything in the world.
- autocomplete - all IDEs will gladly substitute values ​​for you and autocomplete selectors.
- build tools like autoprefixer, uglifycss, etc. cutting will be problematic. You will also have to wedge the same livereload into huge crutches.
You can come up with a lot of options for how to solve these problems, but in fact it remains the same shit code.
You can implement it in twig and add mega complex logic to the template compilation stage that will track the output of variables, cache requests for selections and use them. All inline styles and scripts will be torn out (unless explicitly asked to leave them) and stored in the cache. This is the only interesting option that is more or less acceptable. But it is extremely difficult to implement.
Excuse me, but the fact that you do not use an IDE and you are too lazy to keep everything in different files is your problem.

K
KOLANICH, 2014-06-07
@KOLANICH

And the component itself is just one file

What's stopping you from using the folder? If you need to distribute in one file, what prevents you from using archives?

V
Viktor Vsk, 2014-06-07
@viktorvsk

1) What is the meaning of such conspiracy? Why not write both style and js inline? So, by the way, it will be easier with element loading (no onload in js).
2) Why not describe such a block with three files: js, html, css. and connect them accordingly when called in the right places?
3) Ruby's yield method matches your description ( www.tutorialspoint.com/ruby/ruby_blocks.htm). In Rails you can do something like:

<head>
<%= yeild :head %>
</head>
...
<body>
...
<div id="myBlock">
<%=  content_for :head { '<style>...</style>' } %>
</div>
</body>

A
Andrey Matsovkin, 2014-08-07
@macik_spb

Firstly, as they rightly said here, mixing everything into one file is very bad from the point of view of subsequent code support.
Secondly, how much is it necessary for the code to be given in separate files? After all, it is possible, as @victorvsk correctly wrote , to include all the code in the "inline" page. This will be faster for loading.
In principle, both of these approaches are implemented in Cotonti CMS plugins . And although its plugins are usually several separate js / css / php files included for loading, it is also possible to implement an “inline” approach.
I will give a piece of code for both options:
1. connection of separate resources

cot_rc_add_file($cot_bootstrap['css_file']); // подключаем CSS файл в <head>
cot_rc_link_footer($cot_bootstrap['js_file']); // подключаем JS файл в конец страницы

2. use include directly in the code:
// вставляем CSS код в текущее место выполнения скрипта
cot_rc_embed(".my_red{ color:red; }", false, 'css');
// подключаем JS код в конец страницы
cot_rc_embed_footer("javascript_var = $current_timestamp; console.log(javascript_var);");

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question