Front-end code snippet dynamic rendering method and system applicable to multiple framework languages
By initializing the front-end framework template and code input box, the server generates and compiles and converts HTML documents, cross-frame front-end code rendering is realized, solving the problem of code rendering in multi-frame environments, and improving development flexibility and efficiency.
Patent Information
- Application Number
- CN202111059431.1
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-09-10
- Publication Date
- 2024-11-01
- Estimated Expiration
- 2041-09-10
AI Technical Summary
It is difficult for the prior art to implement dynamic rendering of front-end code in a multi-frame locale, especially when multiple front-end frameworks need to be supported. There are differences in the compilation environment and code conversion, which limits the flexibility of developers to use different technology stacks.
By initializing the templates of each front-end framework, generating code input boxes, filling in language types and code snippets, the server generates the code files of the corresponding framework based on the language types, and packages and compiles them, converts them into native HTML documents, and transmits them to the client for parsing and Rendering, realize cross-frame technology connection.
It realizes cross-frame technical compatibility, allowing developers to choose different languages according to their preferences, automatically compile and convert, solves the differences in the compilation environments of different frameworks, and improves development efficiency and flexibility.
Smart Images

Figure CN113805874B_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of front-end code rendering, and in particular to a method and system for dynamic rendering of front-end code snippets applicable to multiple framework languages. Background Art
[0002] The development of front-end is changing with each passing day. With the development of front-end technology, various front-end frameworks have emerged, including native Web (JavaScript, CSS, HTML), Web implementation based on jQuery, Vue, Angular, AngularJS, React, etc. Each framework implements a page / component and has a different writing method.
[0003] When working on a front-end project, the implementation of pages / components is generally based on the same framework. However, if the project is a development platform that allows others to do some development on it and implement some displays on the page, it is difficult to restrict others to using only the same framework language.
[0004] For example, the main framework of the platform is Vue. To develop some pages / components on this platform, you need to be familiar with the main framework of Vue. However, since everyone has different technology stacks, it is more desirable to use the technical framework that everyone is good at, and then integrate it into the main platform. Summary of the invention
[0005] The purpose of the present invention is to overcome the defects of the above-mentioned prior art and provide a method and system for dynamic rendering of front-end code snippets suitable for multiple framework languages, which allows front-end technology to be developed using multiple framework languages.
[0006] The purpose of the present invention can be achieved by the following technical solutions:
[0007] A front-end code snippet dynamic rendering method applicable to multiple framework languages comprises the following steps:
[0008] Initialize the templates of each front-end framework;
[0009] Fill in the code snippet in the preset code input box, which includes: a language field for inputting the language type of the code snippet; a code field for inputting executable code data;
[0010] According to the language type, the filled content corresponding to the code field is used to generate a code file of the corresponding framework language, and the code file is inserted into the template of the corresponding front-end framework to generate a rendering file;
[0011] Packaging and compiling the rendering files, and performing language conversion to generate native HTML documents;
[0012] Parse and render the native HTML document in the client.
[0013] Furthermore, the code input box also includes:
[0014] Div container field, used to configure the div container;
[0015] The Dependency Package field is used to enter the dependency packages used in the code snippet;
[0016] Configure the div container according to the filled content corresponding to the div container field; download the dependency package according to the filled content corresponding to the dependency package field; insert the code file, the div container configuration and the dependency package into the template of the corresponding front-end framework to generate the rendering file.
[0017] Furthermore, the div container field includes:
[0018] The attribute field is used to configure the HTML id attribute of the div container;
[0019] The style field is used to configure the inline style of the div container;
[0020] List field, used to configure the class list of the div container;
[0021] Configure a div container based on its HTML id attribute, inline styles, and class list.
[0022] Furthermore, through the scaffolding of each front-end framework, the project corresponding to each front-end framework is initialized as a template.
[0023] Furthermore, parsing and rendering are performed in the client according to the native HTML document as follows:
[0024] Extract script tags, link tags and style tags from the native HTML document and add them to the header tag of the Web application in the client. Extract div tags from the native HTML document and insert the content of the code snippet into the view of the client according to the domId field in the executable code data.
[0025] This embodiment also provides a front-end code snippet dynamic rendering system applicable to multiple framework languages, including:
[0026] The server is configured to initialize the templates of each front-end framework and pre-set a code input box, which includes: a language field for inputting the language type of the code snippet; a code field for inputting executable code data;
[0027] Obtain the code snippet filled in the code input box, generate a code file of the corresponding framework language according to the language type, and insert it into the template of the corresponding front-end framework to generate a rendering file;
[0028] Packaging and compiling the rendering files, and performing language conversion to generate native HTML documents;
[0029] The back end is configured to display the code input box and fill in the code snippet in the code input box;
[0030] The client is configured to obtain native HTML documents for parsing and rendering.
[0031] Furthermore, the code input box also includes:
[0032] Div container field, used to configure the div container;
[0033] The Dependency Package field is used to enter the dependency packages used in the code snippet;
[0034] Configure the div container according to the filled content corresponding to the div container field; download the dependency package according to the filled content corresponding to the dependency package field; insert the code file, the div container configuration and the dependency package into the template of the corresponding front-end framework to generate the rendering file.
[0035] Furthermore, the div container field includes:
[0036] The attribute field is used to configure the HTML id attribute of the div container;
[0037] The style field is used to configure the inline style of the div container;
[0038] List field, used to configure the class list of the div container;
[0039] Configure a div container based on its HTML id attribute, inline styles, and class list.
[0040] Furthermore, through the scaffolding of each front-end framework, the project corresponding to each front-end framework is initialized as a template.
[0041] Furthermore, obtaining the native HTML document for parsing and rendering is as follows:
[0042] Extract script tags, link tags and style tags from the native HTML document and add them to the header tag of the Web application in the client. Extract div tags from the native HTML document and insert the content of the code snippet into the view of the client according to the domId field in the executable code data.
[0043] Compared with the prior art, the present invention first initializes the templates of different front-end frameworks, displays the code input box to the developer, and requires the developer to fill in the language type and code snippet. The developer can choose different languages according to his or her preferences, and then convert the code snippet into a code file of the corresponding language according to the language type on the server side, and insert it into the front-end framework template of the corresponding language to obtain a rendering file. The rendering file is packaged and compiled on the server side, converted into a native HTML document, and then transmitted to the client for parsing and rendering; thus, it has the following advantages:
[0044] (1) The server initializes the template project of the corresponding framework and inserts the code snippets filled in by the developer. The developer can fill in the code according to the language of his or her preference. The server automatically inserts the code into the corresponding template project for mutation according to the language type. This solves the problem of different compilation environments required by different frameworks and enables automatic compilation and conversion.
[0045] (2) The rendering files are packaged and compiled on the server side, converted into native HTML documents, and then transmitted to the client for parsing and rendering, thereby achieving cross-frame technology integration.
[0046] (3) The configured code input box includes a language field, a code field, a div container field and a dependency package field, which is more readable. Since the format and code are different when configuring the div container in different languages, the present invention sets the div container field to include an attribute field, a style field and a list field, and stipulates a fixed configuration format to facilitate identification and conversion. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] Figure 1 The present invention provides a flowchart of a method for dynamic rendering of front-end code snippets applicable to multiple framework languages provided in an embodiment of the present invention. DETAILED DESCRIPTION
[0048] In order to make the purpose, technical solutions and advantages of the embodiments of the present invention clearer, the technical solutions in the embodiments of the present invention will be clearly and completely described below in conjunction with the drawings in the embodiments of the present invention. Obviously, the described embodiments are part of the embodiments of the present invention, not all of the embodiments. Generally, the components of the embodiments of the present invention described and shown in the drawings here can be arranged and designed in various different configurations.
[0049] Therefore, the following detailed description of the embodiments of the present invention provided in the accompanying drawings is not intended to limit the scope of the invention claimed for protection, but merely represents selected embodiments of the present invention. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0050] Example 1
[0051] This embodiment provides a method for dynamic rendering of front-end code snippets applicable to multiple framework languages, comprising the following steps:
[0052] S1: Initialize the templates of each front-end framework. In this embodiment, the projects corresponding to each front-end framework are initialized through the scaffolding of each front-end framework as templates;
[0053] S2: Fill in the code snippet in a preset code input box, where the code input box includes: a language field for inputting the language type of the code snippet; and a code field for inputting executable code data;
[0054] S3: Generate a code file of the corresponding framework language based on the content of the code field according to the language type, and insert it into the template of the corresponding front-end framework to generate a rendering file;
[0055] S4: Package and compile the rendered files, perform language conversion, and generate native HTML documents. For example, the rendered files of Vue language can be converted into native HTML documents using Nuxt.js, and the rendered files of React language can be converted into native HTML documents using ReactDOMServer API, etc.
[0056] S5: Parse and render according to the native HTML document in the client.
[0057] The present invention first initializes the templates of different front-end frameworks, displays the code input box to the developer, and requires the developer to fill in the language type and code snippet. The developer can choose different languages according to his or her preferences, and then convert the code snippet into a code file of the corresponding language according to the language type, and insert it into the front-end framework template of the corresponding language to obtain a rendering file. The rendering file is packaged and compiled on the server, converted into a native HTML document, and then transmitted to the client for parsing and rendering, thereby realizing cross-framework technology.
[0058] As a preferred implementation, the code input box also includes:
[0059] Div container field, used to configure the div container;
[0060] The Dependency Package field is used to enter the dependency packages used in the code snippet;
[0061] Configure the div container according to the corresponding content filled in the div container field; download the dependent package according to the corresponding content filled in the dependent package field; insert the code file, div container configuration and dependent package into the template of the corresponding front-end framework to generate a rendering file.
[0062] As a preferred implementation, the div container field includes:
[0063] The attribute field is used to configure the HTML id attribute of the div container;
[0064] The style field is used to configure the inline style of the div container;
[0065] List field, used to configure the class list of the div container;
[0066] Configures a div container based on its HTML id attribute, inline styles, and class list.
[0067] In this embodiment, parsing and rendering according to the native HTML document in the client are specifically as follows:
[0068] Extract script tags, link tags, and style tags from the native HTML document and add them to the header tag of the Web application in the client. Extract div tags from the native HTML document and insert the content of the code snippet into the client's view according to the domId field in the executable code data.
[0069] This embodiment also provides a front-end code snippet dynamic rendering system suitable for multiple framework languages, including a server, a back-end and a client, the server is configured to execute steps S1, S3 and S4 of the method, the back-end is configured to execute steps S2 of the method, and the client is configured to execute steps S5 of the method.
[0070] refer to Figure 1 As shown, the specific implementation process of this embodiment is provided below.
[0071] In step 001, you need to initialize the corresponding project as a template in the server-side directory through the scaffolding of various front-end frameworks, such as vuecli, react cli, etc., and download the corresponding dependencies;
[0072] In step 002, the developer needs to define a json to describe the context of the code snippet. In the interface, the developer can fill in the json using a visual input box. The following is an explanation of this json:
[0073]
[0074] In the above json:
[0075] language: the language of the code snippet, such as Vue, React, HTML Native, etc.
[0076] domId: The HTML id attribute of the div container used for wrapping;
[0077] codeSnippet: executable code snippet written by the developer;
[0078] style: inline style of the div container used for wrapping;
[0079] class: the class list of the div container used for wrapping;
[0080] dependencies: The npm dependency packages used in this code snippet conform to the dependencies specification in npm package.json;
[0081] In step 003, when rendering the code snippet, the client first sends the definition of the code snippet, that is, the json in step 002, to the backend service;
[0082] In step 004, the data processing process of the server:
[0083] 1) According to the dependencis field in the json described in the code snippet, use npm to download the corresponding required dependencies;
[0084] 2) According to the language field in the json described by the code snippet, generate a code file of the corresponding framework language from codeSnippet and insert it into the corresponding project template generated by 001;
[0085] 3) Set the package entry of the project to the file generated in the above steps;
[0086] 4) Execute package compilation to generate a single HTML document of native HTML+CSS+JavaScript from the components of the framework language. This part needs to refer to the technical solutions of server-side rendering of each framework. For example, Vue can be implemented using Nuxt.js, React can be implemented using ReactDOMServer API, etc.
[0087] 5) The generated HTML document should be able to be opened directly in the browser and the effect can be seen.
[0088] In step 005, the content of the single HTML file generated in step 004 is output and returned to the client, which is responsible for parsing and rendering.
[0089] In step 006, the client obtains the content of the single HTML file given in 005, extracts the script, link, style and other tags, inserts them into the header tag of the client's Web, finds the corresponding DOM for the div and other view tags according to the domId field in the json described in the code snippet, and uses the dom's innerHTML API to insert the content into the client's view.
[0090] Since script, link, style, etc. are inserted into the header in 006, these contents will take effect immediately, and the rendered view will also operate according to the defined style and interactive behavior.
[0091] The preferred specific embodiments of the present invention are described in detail above. It should be understood that a person skilled in the art can make many modifications and changes based on the concept of the present invention without creative work. Therefore, any technical solution that can be obtained by a person skilled in the art through logical analysis, reasoning or limited experiments based on the concept of the present invention on the basis of the prior art should be within the scope of protection determined by the claims.
Claims
1. A front-end code snippet dynamic rendering method applicable to multiple framework languages, characterized in that: The following steps are involved: Initialize the templates of each front-end framework; Fill in the code snippet in the preset code input box, which includes: a language field for inputting the language type of the code snippet; a code field for inputting executable code data; According to the language type, the filled content corresponding to the code field is used to generate a code file of the corresponding framework language, and the code file is inserted into the template of the corresponding front-end framework to generate a rendering file; Packaging and compiling the rendering files, and performing language conversion to generate native HTML documents; Parsing and rendering according to the native HTML document in the client; The code input box also includes: Div container field, used to configure the div container; The Dependency Package field is used to enter the dependency packages used in the code snippet; According to the content filled in the div container field, configure the div container; according to the content filled in the dependency package field, download the dependency package; insert the code file, the configuration of the div container and the dependency package into the template of the corresponding front-end framework to generate the rendering file; The div container field includes: The attribute field is used to configure the HTML id attribute of the div container; The style field is used to configure the inline style of the div container; List field, used to configure the class list of the div container; Configure the div container according to the HTML id attribute, inline style and class list of the div container; Initialize the projects corresponding to each front-end framework through the scaffolding of each front-end framework as a template; Parsing and rendering in the client according to the native HTML document are specifically as follows: Extract script tags, link tags and style tags from the native HTML document and add them to the header tag of the Web application in the client. Extract div tags from the native HTML document and insert the content of the code snippet into the view of the client according to the domId field in the executable code data.
2. A front-end code snippet dynamic rendering system suitable for multiple framework languages, characterized in that: include: The server is configured to initialize the templates of each front-end framework and pre-set a code input box, which includes: a language field for inputting the language type of the code snippet; a code field for inputting executable code data; Obtain the code snippet filled in the code input box, generate a code file of the corresponding framework language according to the language type, and insert it into the template of the corresponding front-end framework to generate a rendering file; Packaging and compiling the rendering files, and performing language conversion to generate native HTML documents; The back end is configured to display the code input box and fill in the code snippet in the code input box; The client is configured to obtain native HTML documents for parsing and rendering; The code input box also includes: Div container field, used to configure the div container; The Dependency Package field is used to enter the dependency packages used in the code snippet; According to the content filled in the div container field, configure the div container; according to the content filled in the dependency package field, download the dependency package; insert the code file, the configuration of the div container and the dependency package into the template of the corresponding front-end framework to generate the rendering file; The div container field includes: The attribute field is used to configure the HTML id attribute of the div container; The style field is used to configure the inline style of the div container; List field, used to configure the class list of the div container; Configure the div container according to the HTML id attribute, inline style and class list of the div container; Initialize the projects corresponding to each front-end framework through the scaffolding of each front-end framework as a template; Get the native HTML document for parsing and rendering: Extract script tags, link tags and style tags from the native HTML document and add them to the header tag of the Web application in the client. Extract div tags from the native HTML document and insert the content of the code snippet into the view of the client according to the domId field in the executable code data.
