-
-
Notifications
You must be signed in to change notification settings - Fork 400
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
Use a templating system for the HTML code #2416
Comments
Note: doing this is quite complex. There is a lot of logic in the CGI that generate the HTML code. Most of the logic probably needs to remain there (otherwise if we move all the if / for loops etc. to the templating system, we won't gain much). We need a way to start small. e.g. before going through the CGI scripts, it may be easier and more useful to first use a template for the code that generates the structure and navigation of all HTML pages, which is in Display.pm display_new() function. |
We also need to discuss which template system to use. See also bug #2361 from @zigouras . |
Other maintained candidates that I found during research include https://metacpan.org/pod/Text::Xslate and https://metacpan.org/pod/Template::Toolkit |
@stephanegigandet @hangy I would like to start working on this issue. Need your guidance on which template to use and which files to start with |
@areeshatariq : @teolemon is suggesting to start with Made Near Me, which is separate from the main product opener code, and is already a bit templated: https://github.com/openfoodfacts/openfoodfacts-server/tree/master/madenearme The corresponding code is in /cgi/madenearme.pl |
@stephanegigandet I'll start working on it. Thanks |
@stephanegigandet Can't find this file Is it /scripts/generate_madenearme_page.pl ? |
I also love this one, with a great separation between design and logic : https://metacpan.org/pod/distribution/Template-Semantic/lib/Template/Semantic/Cookbook.pod |
Hi @areeshatariq , sorry for the late reply, you are right, it's this file. |
It's a bit weird to have the selectors in the Perl code though. A designer cannot deeply change the template just by looking at the template. The Template::Semantic module last commit was in 2012 and there's no maintainer and no reply to any of the issues on github. I suggest we use a more known / used module. |
I think the contrary: the designer could be totally unaware of what logic exist or not on the backend; he just have to design the UI and tell what he wants in a user perspective. HTML ids help designer, javascript frontend and perl backend developpers to share common objects. This is one of the problem I'm facing in Power User Script dev: the lack of ids to create UI features without asking perl devs. For example, for the moment there is no simple way to play with the user id. I had to write very bad code to handle it: var editor_id = $(".side-nav > li > a").attr("href"); // /editor/charlesnepote
var user_id = (/\/(.*?)\/(.*)/).exec(editor_id)[2]; With Template-Semantic, the designer writes: The frontend developer then can writes: And the backend dev fill the different values needed in the HTML content and attributes corresponding to each id.
Of course, I understand it's a very important point, that probably disqualifies this solution. |
@stephanegigandet @hangy
The html can be fragmented in includable, reusable bits (product.html, header.html…). It should be as dumb as possible, with the objects, computations, and mapping happening in the view (eg the CGI) |
How can we go further? Would it help if I start benchmarking templating systems? |
Yup, you can start with the most popular one. HTML::Template. 😄 |
I looked at both HTML::Template and Template::Toolkit. I think Template::Toolkit would be easier to implement, as we can bind it more easily to the Perl code and data. It would be in particular very useful for the internationalization, as we can include subroutine calls to our lang() function. e.g. [% lang("some_field_description") %], without having to first declare all needed values from the Perl code. If the designer wants to use another string from the .po file, that's fine, there's no need to change the Perl code, just the template. |
I've done some investigations also, from a front-end dev perspective, and want to share it here, but I need more time. Could you wait a little bit more? |
Disclaimer: I'm not a professional dev and I don't pretend to suggest the best solution. I would only want to let you think about the impacts of the chosen templating system and the opportunities that we can open or not. Sorry for the long post (with probably many mistakes), at least if I'm dreaming of something that is impossible. So, why would we like to use templates? I think it's important that we agree with the reasons why.
Then, we should ask ourselves WHO will work on these templates.
Having a very easy-to-use templating system could lead to many use cases:
If we are interested in this use cases, we have to ask ourselves what is an easy-to-use templating system for all these cases. In these cases, classical template engines has major downsides:
I know it can be hard to follow, but I would like to ask you all to (re-)consider pure HTML templates. It's not commonly used but there are many projects working with this idea. Outside of Perl (you can directly jump to Perl section):
Etc. You can find more! What about Perl? I understand a good and maintained implementation is important for Perl. I only identified two projects. They are a bit old (2011-2012), but some major perl templating systems major release are even old. Template::Semantic PROs:
CONs:
HTML::Seamstress PROs:
CONs:
I would really appreciate your feedbacks about all that, and your ideas about opportunities to have other devs joining thanks to templates. That said, I would perfectly understand if we choose something more classical. I think Template::Toolkit is the best one, though, in particular because it is also available in Python and Javascript. |
I like the usage of a templating system. I have been using Expression Engine for a while. |
Thank you very much @CharlesNepote for the detailed writeup! I think we clearly share the goals you outline, and you have a good point that it would be great that designers are able to test templates without having to install an instance of Product Opener. On the other hand, (it's of course a matter of personal taste), I'm very unconfortable with solutions that do the rendering on the client side with Javascript. If we do the rendering on the server side, we could also try to implement a way for designers to test templates (e.g. by allowing them to store personal templates). |
Thanks @CharlesNepote! I agree that JS based templating probably a good option, but would shy away from jQuery if possible. An API based website #80 based on Vue, react, or angular (or other popular libs) would likely attract more frontend developers, but is a larger overhaul. |
I totally support the usage of a templating system for Product Opener, for all the reasons detailed by @CharlesNepote. |
I'm inclined to agree. The logic needs to be somewhere, and really small partial templates can be difficult to understand when doing frontend development and l10n. However, designer support of special templating languages can be tricky. But that can work, if we look at the attribute based logic that angular uses. One advantage of pure HTML though could be that they could be translated in crowdin "as-is". |
I agree with you. I was only giving examples. My point was not about client side vs server side but pure HTML vs template language/tags.
Interesting :-) 👍 |
Hi Everyone! Thanks a lot for all the ideas and feedback. It shows there's clearly a big need and big interest. @areeshatariq is going to work on the templating system, as part of an internship through Outreachy, and thanks to the Perl Foundation. There are many different ways that have been suggested, all with very interesting reasons. And there are some that we definitely should explore for the longer term (like what is suggested by @hangy and @teolemon in #80 ). Those have huge potential, but they are also very risky (it's always difficult to know which technology will be still hot 5 or 10 years from now), and require a lot of work (e.g. if we want to do API based rendering, we should probably overhaul the API as well, to have better handling of taxonomies, internationalisation etc. in the API itself). So I think our safest bet, to fix or immediate problem (HTML and CSS code embedded in the Perl code), is to start simple, in an incremental way. I asked Areesha to try to do the templating for a couple of things first (the popup that contains the Nutri-Score computation details with the points etc., and then the nutrition facts table), to see how we can do it concretly (e.g. dealing with the translations of the .po files), and so that we can all see what the template code looks like for actual OFF HTML generation. We will start with Template::Toolkit first (it's popular and has the features we need), and we can share the results (how the Perl code was changed, and what the template looks like). If it works well, then we can gradually extend what code is generated by Template::Toolkit. If there are issues / concerns, we could try to experiment with a few more templating engines. Once we have done the templating support for Template::Toolkit, it should be relatively easy to try another templating engine. |
Can we consider this as fixed @stephanegigandet ? |
Closing this bug. We still have some HTML in the code, but the bulk of it has been removed. |
What
Extract all the HTML code from CGI files and put them in a separate template, using a standard templating system
Why
As a Frontend developper, I don't want to think of the underlying Perl code when I improve the OFF interface
Tasks
Part of
The text was updated successfully, but these errors were encountered: