1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
<?php
namespace Elementor\Modules\AtomicWidgets\Elements;
use Elementor\Modules\AtomicWidgets\TemplateRenderer\Template_Renderer; use Elementor\Utils;
if ( ! defined( 'ABSPATH' ) ) { exit; // Exit if accessed directly. }
/** * @mixin Has_Atomic_Base */ trait Has_Template {
public function get_initial_config() { $config = parent::get_initial_config();
$config['twig_main_template'] = $this->get_main_template(); $config['twig_templates'] = $this->get_templates_contents();
return $config; }
protected function render() { try { $renderer = Template_Renderer::instance();
foreach ( $this->get_templates() as $name => $path ) { if ( $renderer->is_registered( $name ) ) { continue; }
$renderer->register( $name, $path ); }
$context = [ 'id' => $this->get_id(), 'type' => $this->get_name(), 'settings' => $this->get_atomic_settings(), 'base_styles' => $this->get_base_styles_dictionary(), ];
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped echo $renderer->render( $this->get_main_template(), $context ); } catch ( \Exception $e ) { if ( Utils::is_elementor_debug() ) { throw $e; } } }
protected function get_templates_contents() { return array_map( fn ( $path ) => Utils::file_get_contents( $path ), $this->get_templates() ); }
protected function get_main_template() { $templates = $this->get_templates();
if ( count( $templates ) > 1 ) { Utils::safe_throw( 'When having more than one template, you should override this method to return the main template.' );
return null; }
foreach ( $templates as $key => $path ) { // Returns first key in the array. return $key; }
return null; }
abstract protected function get_templates(): array; }
|