PluginProbe
Elementor Website Builder – more than just a page builder / 4.3.0-beta3
Elementor Website Builder – more than just a page builder v4.3.0-beta3
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / modules / atomic-widgets / elements / base / has-element-template.php

has-element-template.php in Elementor Website Builder – more than just a page builder 4.3.0-beta3, at modules/atomic-widgets/elements/base/has-element-template.php

149 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\Modules\AtomicWidgets\Elements\Base;
4
5 use Elementor\Modules\AtomicWidgets\Elements\TemplateRenderer\Template_Renderer;
6 use Elementor\Utils;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit;
10 }
11
12 /**
13 * Trait for nested elements that render using Twig templates.
14 * Provides Twig-based rendering with children support for nested elements.
15 *
16 * @mixin Has_Atomic_Base
17 * @mixin Atomic_Element_Base
18 */
19 trait Has_Element_Template {
20
21 private static function get_macros_template_key(): string {
22 return 'elementor/macros';
23 }
24
25 protected function transform_link_for_render( array $parsed ): array {
26 return $parsed;
27 }
28
29 public function get_initial_config() {
30 $config = parent::get_initial_config();
31
32 $config['support_nesting'] = true;
33 $config['twig_main_template'] = $this->get_main_template();
34 $config['twig_templates'] = $this->get_templates_contents();
35 $config['base_styles_dictionary'] = $this->get_base_styles_dictionary();
36
37 return $config;
38 }
39
40 protected function get_shared_templates(): array {
41 return [
42 self::get_macros_template_key() => __DIR__ . '/_macros.html.twig',
43 ];
44 }
45
46 protected function get_templates_contents() {
47 return array_map(
48 fn ( $path ) => Utils::file_get_contents( $path ),
49 array_merge( $this->get_shared_templates(), $this->get_templates() )
50 );
51 }
52
53 protected function render() {
54 try {
55 $renderer = Template_Renderer::instance();
56
57 $all_templates = array_merge( $this->get_shared_templates(), $this->get_templates() );
58
59 foreach ( $all_templates as $name => $path ) {
60 if ( $renderer->is_registered( $name ) ) {
61 continue;
62 }
63
64 $renderer->register( $name, $path );
65 }
66
67 $context = $this->build_template_context();
68 $template_html = $renderer->render( $this->get_main_template(), $context );
69 $children_html = $this->render_children_to_html();
70 $output = str_replace( $this->get_children_placeholder(), $children_html, $template_html );
71
72 // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
73 echo $output;
74 } catch ( \Exception $e ) {
75 if ( Utils::is_elementor_debug() ) {
76 throw $e;
77 }
78 }
79 }
80
81 protected function render_children_to_html(): string {
82 $html = '';
83
84 foreach ( $this->get_children() as $child ) {
85 ob_start();
86 $child->print_element();
87 $html .= ob_get_clean();
88 }
89
90 return $html;
91 }
92
93 protected function get_children_placeholder(): string {
94 return '<!-- elementor-children-placeholder -->';
95 }
96
97 protected function build_base_template_context(): array {
98 $settings = $this->get_atomic_settings();
99
100 return [
101 'id' => $this->get_id(),
102 'interaction_id' => $this->get_interaction_id(),
103 'type' => $this->get_name(),
104 'settings' => $settings,
105 'tag' => static::get_computed_html_tag( $settings ),
106 'base_styles' => $this->get_base_styles_dictionary(),
107 'children_placeholder' => $this->get_children_placeholder(),
108 ];
109 }
110
111 public function before_render() {
112 // Intentionally empty - Twig template handles full rendering
113 }
114
115 public function after_render() {
116 // Intentionally empty - Twig template handles full rendering
117 }
118
119 public function print_content() {
120 $defined_context = $this->define_render_context();
121
122 if ( empty( $defined_context ) ) {
123 return $this->render();
124 }
125
126 $this->set_render_context( $defined_context );
127
128 $this->render();
129
130 $this->clear_render_context( $defined_context );
131 }
132
133 protected function get_main_template(): string {
134 $templates = $this->get_templates();
135
136 foreach ( $templates as $key => $path ) {
137 return $key;
138 }
139
140 return '';
141 }
142
143 abstract protected function get_templates(): array;
144
145 protected function build_template_context(): array {
146 return $this->build_base_template_context();
147 }
148 }
149